]> _ Git - cubist_net.git/commitdiff
wait #6869 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 18 Apr 2024 13:13:29 +0000 (15:13 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 18 Apr 2024 13:13:29 +0000 (15:13 +0200)
src/Transfer/Driver.php
src/Transfer/SFTP.php

index bbd36f3b193f42cae11b91d5e694cd8296557848..c09678360b72cdb61dbc635bcf52f85453538daf 100644 (file)
@@ -48,6 +48,8 @@ abstract class Driver
         return self::cleanInstallDir($path);
     }
 
+    abstract public function checkConnexion($data = []);
+
     abstract protected function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false);
 
         /**
index 4c3377330ad0a03bfc5da09b0306aef0083b7026..668e99d787c1d65656bd8ca8cb99a66aeda2018e 100644 (file)
@@ -4,8 +4,10 @@ namespace Cubist\Net\Transfer;
 
 use Cubist\Util\CommandLine\Rclone;
 use Cubist\Util\Files\Files;
+use Illuminate\Support\Facades\Log;
 
-class SFTP extends Driver {
+class SFTP extends Driver
+{
 
     const ERROR_MISSING_HOST = 0;
 
@@ -15,9 +17,10 @@ class SFTP extends Driver {
 
     const ERROR_SEND_FILE = 5;
 
-    protected $port = 22;
+    const DEFAULT_PORT = 22;
 
-    protected function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false) {
+    protected function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false)
+    {
         $rclone = new Rclone();
         $rclone->setServer($this->getServer());
         $rclone->setSrc($source);
@@ -27,19 +30,19 @@ class SFTP extends Driver {
         $rclone->execute();
         return $rclone;
     }
-    
-    public function checkConnexion($data = []) {
+
+    public function checkConnexion($data = [])
+    {
         $connection = null;
 
-        $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' => $data['port'] ?? $this->getServer()->getPort(),
+            'port' => $data['port'] ?? $this->getServer()->getPort() ?? self::DEFAULT_PORT,
         ];
 
-        if(!$data['host']) {
+        if (!$data['host']) {
             return self::ERROR_MISSING_HOST;
         } else {
             try {
@@ -55,30 +58,31 @@ class SFTP extends Driver {
             }
 
             try {
-                $tmp = Files::tempnam("/application"); //on créé un fichier temporaire en local
+                $tmp = Files::tempnam(); //on créé un fichier temporaire en local
                 $this->copyFile($tmp, '/');
-            } catch(\Exception $e) {
+            } catch (\Exception $e) {
                 return static::ERROR_SEND_FILE;
             }
 
             try {
                 $filename = $this->getTestFilename($tmp);
                 $sftp = ssh2_sftp($connection);
-                $basepath = '/'.trim($this->getServer()->getBasePath(), '/').'/';
-                $f = @ssh2_sftp_stat($sftp, $basepath.$filename);
-                ssh2_sftp_unlink($sftp, $basepath.$filename); //on supprime le fichier distant
+                $basepath = '/' . trim($this->getServer()->getBasePath(), '/') . '/';
+                $f = @ssh2_sftp_stat($sftp, $basepath . $filename);
+                ssh2_sftp_unlink($sftp, $basepath . $filename); //on supprime le fichier distant
                 ssh2_disconnect($connection); //on quitte la connexion
                 unlink($tmp); //on supprime le fichier temporaire en local
                 return true;
-            } catch(\Exception $e) {
+            } catch (\Exception $e) {
                 return static::ERROR_SEND_FILE;
             }
 
         }
     }
 
-    public function getTestFilename($file) {
-        $filepath = explode('/',$file);
+    public function getTestFilename($file)
+    {
+        $filepath = explode('/', $file);
         return end($filepath);
     }
 }
\ No newline at end of file