Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alternative SFTP transfer for some cases #1

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions Classes/Driver/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use MicrosoftAzure\Storage\Blob\Models\SetBlobPropertiesResult;
use MicrosoftAzure\Storage\Common\Internal\Utilities;
use Psr\Http\Message\StreamInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
use TYPO3\CMS\Core\Resource\Driver\AbstractHierarchicalFilesystemDriver;
Expand All @@ -26,8 +28,9 @@
use TYPO3\CMS\Core\Resource\StorageRepository;


class StorageDriver extends AbstractHierarchicalFilesystemDriver
class StorageDriver extends AbstractHierarchicalFilesystemDriver implements LoggerAwareInterface
{
use LoggerAwareTrait;

/**
* @var string
Expand Down Expand Up @@ -218,7 +221,12 @@ public function createFolder($newFolderName, $parentFolderIdentifier = '', $recu
$parentFolderIdentifier = $this->normalizeFolderName($parentFolderIdentifier);
$newFolderName = $this->normalizeFolderName($newFolderName);
$newFolderIdentifier = $this->normalizeFolderName($parentFolderIdentifier . $newFolderName);
$this->createBlockBlob($newFolderIdentifier);

if ($this->supportsSftp()) {
$this->executeSftp(sprintf('mkdir "%s"', $newFolderIdentifier));
} else {
$this->createBlockBlob($newFolderIdentifier);
}

return $newFolderIdentifier;
}
Expand Down Expand Up @@ -335,11 +343,27 @@ public function addFile($localFilePath, $targetFolderIdentifier, $newFileName =
$contentType = $fileExtensionToMimeTypeMapping[$pathInfo['extension']];
}

$options = new CreateBlockBlobOptions();
$options->setContentType($contentType);
$options->setCacheControl($this->cacheControl);
if ($this->supportsSftp()) {
$this->executeSftp(
sprintf(
'put "%s" "%s"',
$localFilePath,
$fileIdentifier
)
);

$options = new SetBlobPropertiesOptions();
$options->setContentType($contentType);
$options->setCacheControl($this->cacheControl);

$this->blobService->setBlobProperties($this->container, $fileIdentifier, $options);
} else {
$options = new CreateBlockBlobOptions();
$options->setContentType($contentType);
$options->setCacheControl($this->cacheControl);

$this->createBlockBlob($fileIdentifier, fopen($localFilePath, 'rb'), $options);
$this->createBlockBlob($fileIdentifier, fopen($localFilePath, 'rb'), $options);
}

if ($removeOriginal === true) {
@unlink($localFilePath);
Expand Down Expand Up @@ -1342,4 +1366,36 @@ protected function isSubSubFolder($folderToCheck, $parentFolderIdentifier)
{
return substr_count($folderToCheck, '/') > substr_count($parentFolderIdentifier, '/') + 1;
}

/**
* Check if an "SFTP identifier" is configured
*
* @return bool
*/
protected function supportsSftp(): bool
{
return (bool)$this->configuration['sftpIdentifier'];
}

/**
* Execute a single SFTP command
*
* @param string $sftpCommand
* @return void
* @throws \Throwable
*/
protected function executeSftp(string $sftpCommand)
{
$cmd = sprintf(
'echo %s | sftp %s',
escapeshellarg($sftpCommand),
$this->configuration['sftpIdentifier']
);

$execReturn = exec($cmd, $output, $result_code);
if ($execReturn === false || $result_code !== 0) {
$this->logger?->error('SFTP error', ['cmd' => $cmd, 'output' => $output]);
throw new \Exception('SFTP error', 1653038631);
}
}
}
11 changes: 11 additions & 0 deletions Configuration/FlexForms/AzureStorage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@
</config>
</TCEforms>
</accountKey>
<sftpIdentifier>
<TCEforms>
<label>SFTP identifier</label>
<description>Optional SFTP identifier (e.g. user@host) to use SFTP via CLI instead of the azure API for some actions</description>
<config>
<type>input</type>
<size>30</size>
<eval>trim</eval>
</config>
</TCEforms>
</sftpIdentifier>
</el>
</ROOT>
</general>
Expand Down