Skip to content

Commit

Permalink
Add preserveModifiedTime option
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandru Condrici committed Apr 2, 2020
1 parent 2a52eff commit f506902
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
composer.lock
vendor/
composer.phar
10 changes: 9 additions & 1 deletion src/Command/TransferFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ protected function configure()
InputOption::VALUE_OPTIONAL,
"Determine if the service retrieve files or push it to the server. Options: put,get",
self::OPERATION_UPLOAD
)
->addOption(
'preservemodifiedtime',
null,
InputOption::VALUE_OPTIONAL,
'If source is a directory, preserve the modified time of its containing files',
false
);
}

Expand All @@ -75,11 +82,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$target = $input->getArgument('targetfile');
$serverId = $input->getArgument('targetserverid');
$method = $input->getOption('method');
$preserveModifiedTime = $input->getOption('preservemodifiedtime');

$ftp = $this->ftpServiceBuilder->login($serverId);

if ($method === self::OPERATION_DOWNLOAD) {
$ftp->download($source, $target);
$ftp->download($source, $target, $preserveModifiedTime);
} elseif ($method === self::OPERATION_UPLOAD) {
$ftp->upload($source, $target);
}
Expand Down
14 changes: 12 additions & 2 deletions src/Service/FTPService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(SFTP $ftp)
$this->ftp = $ftp;
}

public function download(string $remotePath, string $localPath): void
public function download(string $remotePath, string $localPath, bool $preserveModifiedTime): void
{
$remoteFiles = (array)$remotePath;
if ($this->ftp->is_dir($remotePath)) {
Expand All @@ -30,14 +30,19 @@ public function download(string $remotePath, string $localPath): void

foreach ($remoteFiles as $remoteFile) {
if ($this->ftp->is_dir($remoteFile)) {
$this->download($remoteFile, rtrim($localPath, DIRECTORY_SEPARATOR) . $remoteFile);
$this->download(
$remoteFile,
rtrim($localPath, DIRECTORY_SEPARATOR) . $remoteFile,
$preserveModifiedTime
);
} else {
$realLocalPath = str_replace(
rtrim($remotePath, DIRECTORY_SEPARATOR),
rtrim($localPath, DIRECTORY_SEPARATOR),
$remoteFile
);
$localDirectory = dirname($realLocalPath);

if (!file_exists($localDirectory)) {
try {
ErrorHandler::start();
Expand All @@ -57,6 +62,11 @@ public function download(string $remotePath, string $localPath): void
$this->ftp->getLastError()
);
}

if ($preserveModifiedTime) {
$originalFileModifiedTime = $this->ftp->filemtime($remoteFile);
touch($realLocalPath, $originalFileModifiedTime);
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Service/FTPServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ interface FTPServiceInterface
/**
* @param string $remotePath
* @param string $localPath
* @param bool $preserveModifiedTime
* @throws FTPCommandFailed
* @throws FTPTransferFileFailed
*/
public function download(string $remotePath, string $localPath): void;
public function download(string $remotePath, string $localPath, bool $preserveModifiedTime): void;

/**
* @param string $localPath
Expand All @@ -22,4 +23,4 @@ public function download(string $remotePath, string $localPath): void;
* @throws FTPTransferFileFailed
*/
public function upload(string $localPath, string $remotePath): void;
}
}

0 comments on commit f506902

Please sign in to comment.