From 4e8c167f46c5928d339b2bfa0357725c73bb196e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 5 Nov 2024 14:27:40 +0100 Subject: [PATCH] wip #7175 @1 --- .editorconfig | 15 +++++++++++++++ composer.json | 3 ++- src/Transfer/Driver.php | 9 +++++---- src/Transfer/FTP.php | 30 +++++++++++++++++------------- src/Transfer/Local.php | 5 +++-- src/Transfer/S3.php | 17 +++++++++++++++++ 6 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 .editorconfig create mode 100644 src/Transfer/S3.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6f313c6 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.yml] +indent_size = 2 diff --git a/composer.json b/composer.json index cfb3895..590a5b1 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "php": ">=7.0.0", "ext-ssh2": "*", "cubist/util": "dev-master", - "ext-ftp": "*" + "ext-ftp": "*", + "aws/aws-sdk-php": "^3.325" }, "suggest": { diff --git a/src/Transfer/Driver.php b/src/Transfer/Driver.php index c096783..0561b16 100644 --- a/src/Transfer/Driver.php +++ b/src/Transfer/Driver.php @@ -52,16 +52,17 @@ abstract class Driver abstract protected function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false); - /** + /** * @param $source string * @param $dest string * @param $mirror bool * @param $dryrun bool * @return CommandLine */ - public function copy($source, $dest, $mirror = false, $dryrun = false) { - if($this->checkConnexion() !== true) { - throw new \Exception('Unabled to connect to this server (error code : '. $this->checkConnexion() .' )'); + public function copy($source, $dest, $mirror = false, $dryrun = false) + { + if ($this->checkConnexion() !== true) { + throw new \Exception('Unabled to connect to this server (error code : ' . $this->checkConnexion() . ' )'); } return $this->synchronizeFiles($source, $dest, $mirror, $dryrun); } diff --git a/src/Transfer/FTP.php b/src/Transfer/FTP.php index ab2b385..9bfbaab 100644 --- a/src/Transfer/FTP.php +++ b/src/Transfer/FTP.php @@ -5,7 +5,8 @@ namespace Cubist\Net\Transfer; use Cubist\Util\CommandLine\LFTP; use Cubist\Util\Files\Files; -class FTP extends Driver { +class FTP extends Driver +{ const ERROR_MISSING_HOST = 0; @@ -19,7 +20,8 @@ class FTP extends Driver { protected $port = 21; - protected function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false) { + protected function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false) + { $lftp = new LFTP(); $lftp->setServer($this->getServer()); $lftp->setSrc($source); @@ -30,37 +32,38 @@ class FTP extends Driver { return $lftp; } - public function checkConnexion($data = []) { + public function checkConnexion($data = []) + { $data['port'] = $data['port'] ?? $this->port; $data = [ 'username' => $data['username'] ?? $this->getServer()->getUsername(), 'password' => $data['password'] ?? $this->getServer()->getPassword(), 'host' => $data['host'] ?? $this->getServer()->getHost(), - 'port' => array_key_exists('port',$data) ? $data['port'] : $this->getServer()->getPort(), + 'port' => array_key_exists('port', $data) ? $data['port'] : $this->getServer()->getPort(), ]; - if(!$data['host']) { + if (!$data['host']) { return self::ERROR_MISSING_HOST; } else { try { $ftp = $data['port'] ? ftp_connect($data['host'], $data['port']) : ftp_connect($data['host']); - } catch(\Exception $e) { + } catch (\Exception $e) { return static::ERROR_INVALID_PARAM; } - if($ftp) { + if ($ftp) { if (@ftp_login($ftp, $data['username'], $data['password'])) { try { $tmp = Files::tempnam(); //on créé un fichier temporaire - $this->copyFile($tmp,$this->getServer()->getBasePath()); - } catch(\Exception $e) { + $this->copyFile($tmp, $this->getServer()->getBasePath()); + } catch (\Exception $e) { return static::ERROR_SEND_FILE; } - $file = $this->getServer()->getBasePath().$this->getTestFilename($tmp); + $file = $this->getServer()->getBasePath() . $this->getTestFilename($tmp); $contents = ftp_raw($ftp, "SIZE $file"); - if(strstr($contents[0],"213")) { //on vérifie si le code 213 est présent + if (strstr($contents[0], "213")) { //on vérifie si le code 213 est présent ftp_raw($ftp, "DELE $file"); //on supprime le fichier distant unlink($tmp); //on supprime le fichier en local ftp_close($ftp); @@ -78,8 +81,9 @@ class FTP extends Driver { } - public function getTestFilename($file) { - $filepath = explode('/',$file); + public function getTestFilename($file) + { + $filepath = explode('/', $file); return end($filepath); } } \ No newline at end of file diff --git a/src/Transfer/Local.php b/src/Transfer/Local.php index 0169b95..beb00a2 100644 --- a/src/Transfer/Local.php +++ b/src/Transfer/Local.php @@ -75,8 +75,9 @@ class Local extends Driver return copy($source, $this->_basePath() . '/' . $dest); } - public function checkConnexion($data = []) { - if(file_exists($this->getRootPath().'status')) { + public function checkConnexion($data = []) + { + if (file_exists($this->getRootPath() . 'status')) { return true; } else { return static::ERROR_FOLDER_UNMOUNT; diff --git a/src/Transfer/S3.php b/src/Transfer/S3.php new file mode 100644 index 0000000..9e51851 --- /dev/null +++ b/src/Transfer/S3.php @@ -0,0 +1,17 @@ +