diff --git a/Command/TransferFileCommand.php b/Command/TransferFileCommand.php index 360d914..dbb2afd 100644 --- a/Command/TransferFileCommand.php +++ b/Command/TransferFileCommand.php @@ -2,12 +2,15 @@ namespace FileTransferBundle\Command; +use FileTransferBundle\Service\FileTransferService; use Pimcore\Console\AbstractCommand; use Pimcore\Console\Dumper; +use Pimcore\Log\ApplicationLogger; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Finder\Finder; class TransferFileCommand extends AbstractCommand { @@ -40,16 +43,60 @@ protected function execute(InputInterface $input, OutputInterface $output) $targetserverid = $input->getArgument('targetserverid'); $service = $this->getContainer()->get('FileTransferBundle\Service\FileTransferService'); -// $config = $this->getContainer()->getParameter('file_transfer_config'); -// var_dump($config); + if ($this->useDirectoryMode($sourcefile)) { + $this->transferDirectory($service, $targetserverid, $sourcefile, $targetfile); + } else { + $service->transferFile($targetserverid, $sourcefile, $targetfile); + } + } + + /** + * Checks if the transfer is in directory mode + * + * @param string $source + * @param string $target + * @return bool + */ + private function useDirectoryMode(string $source): bool + { + if (is_dir($source)) { + return true; + } - $service->transferFile($targetserverid, $sourcefile, $targetfile); + return false; + } + + /** + * Transfers the a complete directory to a remote server + * + * @param FileTransferService $service + * @param string $serverId + * @param string $source + * @param string $target + */ + private function transferDirectory(FileTransferService $service, string $serverId, string $source, string $target): void + { + $finder = new Finder(); + $finder->files()->in($source); - // retrieve targetserver configuration from config + /** @var ApplicationLogger $logger */ + $logger = $this->getContainer()->get('monolog.logger.admin'); + if (!$finder->hasResults()) { + $logger->notice('no files found', [ + 'component' => 'FileTransfer' + ]); + return; + } - // sftp the source file to the target file + foreach ($finder as $file) { + $destination = $target . DIRECTORY_SEPARATOR . $file->getFilename(); + $service->transferFile( + $serverId, + $file, + $destination); + } } } \ No newline at end of file diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php deleted file mode 100644 index 04ddbd7..0000000 --- a/Controller/DefaultController.php +++ /dev/null @@ -1,19 +0,0 @@ -logger->setComponent('FileTransfer'); } - + public function transferFile($serverid, $sourcefile, $targetfile) { - -// var_dump($this->config); -// var_dump($serverid); -// var_dump($sourcefile); -// var_dump($targetfile); - - if(isset($this->config['servers'][$serverid])) { + if (isset($this->config['servers'][$serverid])) { $c = $this->config['servers'][$serverid]; $address = $c['address']; $username = $c['username']; @@ -44,14 +32,8 @@ public function transferFile($serverid, $sourcefile, $targetfile) throw new \RuntimeException($e); } - // $sourcefile - // $targetfile - // $username - // $address - // $password - $sftp = new SFTP($address); - if(!$sftp->login($username, $password)) { + if (!$sftp->login($username, $password)) { $e = "Sftp login failed for user " . $username; $this->logger->error($e); throw new \RuntimeException($e); @@ -61,8 +43,8 @@ public function transferFile($serverid, $sourcefile, $targetfile) $this->logger->debug("Sftp folder name is $sftpFolderName"); - if(!$sftp->file_exists($sftpFolderName)){ - if(!$sftp->mkdir($sftpFolderName, 0777)){ + if (!$sftp->file_exists($sftpFolderName)) { + if (!$sftp->mkdir($sftpFolderName, 0777)) { $e = "Can't create $sftpFolderName directory. " . $sftp->getLastSFTPError(); $this->logger->error($e); throw new \RuntimeException($e); @@ -71,13 +53,13 @@ public function transferFile($serverid, $sourcefile, $targetfile) $this->logger->debug("Directory exists $sftpFolderName"); } - if(!$sftp->chdir($sftpFolderName)){ + if (!$sftp->chdir($sftpFolderName)) { $e = "Can't chdir to $sftpFolderName directory. " . $sftp->getLastSFTPError(); $this->logger->error($e); throw new \RuntimeException($e); } - if(!$sftp->put($targetfile, $sourcefile, SFTP::SOURCE_LOCAL_FILE)){ + if (!$sftp->put($targetfile, $sourcefile, SFTP::SOURCE_LOCAL_FILE)) { $e = "Couldn't send file to sftp. " . $sftp->getLastSFTPError(); $this->logger->error($e); throw new \RuntimeException($e);