Skip to content

Commit

Permalink
Merge pull request #1 from YouweGit/feature/directory-support
Browse files Browse the repository at this point in the history
Feature/directory support
  • Loading branch information
jorisros authored Aug 20, 2019
2 parents e01b0d7 + 03c4b0d commit 7f8d1ec
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 92 deletions.
57 changes: 52 additions & 5 deletions Command/TransferFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}

}
19 changes: 0 additions & 19 deletions Controller/DefaultController.php

This file was deleted.

6 changes: 0 additions & 6 deletions FileTransferBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@

class FileTransferBundle extends AbstractPimcoreBundle
{
public function getJsPaths()
{
return [
'/bundles/filetransfer/js/pimcore/startup.js'
];
}
}
5 changes: 0 additions & 5 deletions Resources/config/pimcore/routing.yml

This file was deleted.

15 changes: 0 additions & 15 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,5 @@ services:
# if you need to do this, you can override this setting on individual services
public: false

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
FileTransferBundle\Controller\:
resource: '../../Controller'
public: true
tags: ['controller.service_arguments']


FileTransferBundle\Service\FileTransferService:
public: true

# add more services, or override services that need manual wiring
# FileTransferBundle\ExampleClass:
# arguments:
# - "@service_id"
# - "plain_value"
# - "%parameter%"
17 changes: 0 additions & 17 deletions Resources/public/js/pimcore/startup.js

This file was deleted.

32 changes: 7 additions & 25 deletions Service/FileTransferService.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: user
* Date: 21/05/2019
* Time: 11:44
*/

namespace FileTransferBundle\Service;

Expand All @@ -24,16 +18,10 @@ public function __construct(ApplicationLogger $logger, ContainerInterface $conta
$this->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'];
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 7f8d1ec

Please sign in to comment.