Skip to content

Commit

Permalink
Merge pull request #2 from jorisros/feature/download-functionality
Browse files Browse the repository at this point in the history
Feature/download functionality
  • Loading branch information
jorisros authored Aug 26, 2019
2 parents 7f8d1ec + c31d4f2 commit b5f22e3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Command/TransferFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ protected function configure()
'targetfile',
InputArgument::REQUIRED,
'target file')
->addOption('method',
'm',
InputOption::VALUE_OPTIONAL,
"Determen if the service retreive files or push it to the server. Options: put,get",
FileTransferService::MODE_PUT)
;

}
Expand All @@ -42,7 +47,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$targetfile = $input->getArgument('targetfile');
$targetserverid = $input->getArgument('targetserverid');

/** @var FileTransferService $service */
$service = $this->getContainer()->get('FileTransferBundle\Service\FileTransferService');
$service->setMode($input->getOption('method'));

if ($this->useDirectoryMode($sourcefile)) {
$this->transferDirectory($service, $targetserverid, $sourcefile, $targetfile);
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ services:

FileTransferBundle\Service\FileTransferService:
public: true
arguments: ["%file_transfer%"]
84 changes: 80 additions & 4 deletions Service/FileTransferService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@

namespace FileTransferBundle\Service;

use http\Exception\RuntimeException;
use phpseclib\Net\SFTP;
use Pimcore\Log\ApplicationLogger;
use Symfony\Component\DependencyInjection\ContainerInterface;

class FileTransferService
{
const MODE_PUT = 'put';
const MODE_GET = 'get';

private $logger;
private $config;
private $mode;

public function __construct(ApplicationLogger $logger, ContainerInterface $container)
public function __construct(array $parameters, ApplicationLogger $logger)
{
$this->config = $container->getParameter('file_transfer_config');
$this->config = $parameters;
$this->logger = $logger;
$this->logger->setComponent('FileTransfer');

$this->mode = self::MODE_PUT;
}

/**
* Set the mode where the service should be operating
*
* @param string $mode
*/
public function setMode(string $mode = self::MODE_PUT):void
{
$this->mode = $mode;
}

public function transferFile($serverid, $sourcefile, $targetfile)
Expand All @@ -43,6 +58,31 @@ public function transferFile($serverid, $sourcefile, $targetfile)

$this->logger->debug("Sftp folder name is $sftpFolderName");

$this->checkIfDirectoryExists($sftp, $sftpFolderName);

$this->changeToDirectory($sftpFolderName);

switch ($this->mode) {
case FileTransferService::MODE_PUT:
$this->putToServer($sftp, $targetfile, $sourcefile);
break;
case FileTransferService::MODE_GET:
$this->getFromServer($sftp, $targetfile, $sourcefile);
break;
default:
throw new \RuntimeException("There is no mode selected please use -m or --method");
break;
}
}

/**
* Checks if a directory remotely exists
*
* @param SFTP $sftp
* @param string $sftpFolderName
*/
private function checkIfDirectoryExists(SFTP $sftp, string $sftpFolderName): void
{
if (!$sftp->file_exists($sftpFolderName)) {
if (!$sftp->mkdir($sftpFolderName, 0777)) {
$e = "Can't create $sftpFolderName directory. " . $sftp->getLastSFTPError();
Expand All @@ -52,14 +92,50 @@ public function transferFile($serverid, $sourcefile, $targetfile)
} else {
$this->logger->debug("Directory exists $sftpFolderName");
}
}

/**
* Change to the correct directory on the server
*
* @param SFTP $sftp
* @param string $targetFile
* @param string $sourceFile
*/
private function changeToDirectory(SFTP $sftp, string $sftpFolderName): void
{
if (!$sftp->chdir($sftpFolderName)) {
$e = "Can't chdir to $sftpFolderName directory. " . $sftp->getLastSFTPError();
$this->logger->error($e);
throw new \RuntimeException($e);
}
}

/**
* Upload the file to the server
*
* @param SFTP $sftp
* @param string $targetFile
* @param string $sourceFile
*/
private function putToServer(SFTP $sftp, string $targetFile, string $sourceFile): void
{
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);
}
}

if (!$sftp->put($targetfile, $sourcefile, SFTP::SOURCE_LOCAL_FILE)) {
/**
* Download the file from the remote server
*
* @param SFTP $sftp
* @param string $targetFile
* @param string $sourceFile
*/
private function getFromServer(SFTP $sftp, string $targetFile, string $sourceFile):void
{
if (!$sftp->get($targetFile, $sourceFile, SFTP::SOURCE_LOCAL_FILE)) {
$e = "Couldn't send file to sftp. " . $sftp->getLastSFTPError();
$this->logger->error($e);
throw new \RuntimeException($e);
Expand Down

0 comments on commit b5f22e3

Please sign in to comment.