Exemplo n.º 1
0
 private function _put($srcFullPath, $srcRelPath)
 {
     $destRelPath = $this->_destDir . '/' . $srcRelPath;
     $this->controller->stdout("\n - " . $srcRelPath . "\t => " . $destRelPath);
     if (!$this->controller->dryRun) {
         if (is_dir($srcFullPath)) {
             $files = FileHelper::findFiles($srcFullPath, $this->_options);
             foreach ($files as $foundPath) {
                 $relativePath = substr($foundPath, strlen($this->_srcBaseDir) + 1);
                 $this->_sftpHelper->mkdir($destRelPath);
                 $this->_put($foundPath, $relativePath, $this->_srcBaseDir, $this->_destDir, $this->_options);
             }
         } elseif (FileHelper::filterPath($srcFullPath, $this->_options)) {
             $this->_sftpHelper->mkdir(dirname($destRelPath), -1, true);
             if ($this->_overwrite || !$this->_sftpHelper->fileExists($destRelPath)) {
                 $res = $this->_sftpHelper->put($destRelPath, $srcFullPath);
                 if (!$res) {
                     Log::logger()->addError('sftpPut: error uploading file {from} to {to}', ['from' => $srcFullPath, 'to' => $destRelPath]);
                 }
             } else {
                 $this->controller->stdout(' [skipped]', Console::FG_PURPLE);
             }
         }
     } else {
         $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
 }
Exemplo n.º 2
0
 private function _get($remotePath, $destRelPathArr = [])
 {
     // Remove the first element from the $destRelPathArr since it corresponds to the destination path
     $destSubFoldersArr = $destRelPathArr;
     array_shift($destSubFoldersArr);
     $destRelPath = implode(DIRECTORY_SEPARATOR, $destSubFoldersArr);
     $localFolderName = basename($this->_destPath);
     $localFolderName .= !empty($destRelPath) ? DIRECTORY_SEPARATOR . $destRelPath : '';
     $destPath = $this->_destPath;
     $destPath .= !empty($destSubFoldersArr) ? DIRECTORY_SEPARATOR . $destRelPath : '';
     $this->controller->stdout("\n - {$localFolderName}\t <= {$remotePath}");
     if (!$this->controller->dryRun) {
         if ($this->_sftpHelper->isDir($remotePath)) {
             if (!is_dir($this->_destPath)) {
                 $this->controller->stdout("\n");
                 Log::throwException('sftpGet: remotePath is a directory, therefore destination has to be a directory too');
             }
             $list = $this->_sftpHelper->nlist($remotePath);
             $remoteDirName = basename($remotePath);
             $newSubDir = $destPath . DIRECTORY_SEPARATOR . $remoteDirName;
             $destRelPathArr[] = $remoteDirName;
             // Creating the sub-directory before recursion to allow creation of empty directories
             // (excluding the first level as it corresponds to the destination path)
             if (count($destRelPathArr) > 1 && !is_dir($newSubDir)) {
                 FileHelper::createDirectory($newSubDir);
             }
             foreach ($list as $item) {
                 if ($item !== '.' && $item !== '..') {
                     $this->_get($remotePath . '/' . $item, $destRelPathArr);
                 }
             }
         } elseif (!empty($destSubFoldersArr) || $this->_sftpHelper->fileExists($remotePath)) {
             // if $destSubFoldersArr is not empty it means that we've got the list of files
             // from the nlist command on the remote directory; therefore we don't need to check
             // if the file exists
             $destFile = $destPath;
             if (is_dir($destFile)) {
                 $destFile = $destPath . DIRECTORY_SEPARATOR . basename($remotePath);
             }
             if ($this->_overwrite || !file_exists($destFile)) {
                 $this->_sftpHelper->get($remotePath, $destFile);
             } else {
                 $this->controller->stdout(' [skipped]', Console::FG_PURPLE);
             }
         } else {
             $this->controller->stdout("\n");
             $this->controller->warn('Not found: ' . $remotePath);
         }
     } else {
         $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
 }
Exemplo n.º 3
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $res = true;
     $taskRunner = $this->taskRunner;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $pathFrom = !empty($cmdParams[1]) ? $taskRunner->parsePath($cmdParams[1]) : '';
     $pathTo = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : '';
     $overwrite = !empty($cmdParams[3]) ? $cmdParams[3] : false;
     // TODO: check if overwrite is needed after implementing the FTP support
     if (empty($pathFrom) || empty($pathTo)) {
         throw new Exception('sftpMv: Origin and destination cannot be empty');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $this->controller->getConnectionParams($connectionId);
     $this->controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $this->controller->stdout(" Move (overwrite: " . ($overwrite ? 'yes' : 'no') . ") \n " . $pathFrom . " to \t " . $pathTo);
     $insidePathTo = $pathTo . '/' . basename($pathFrom);
     if (!$this->controller->dryRun) {
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $this->controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         if (!$sftpHelper->fileExists($pathFrom)) {
             $this->controller->stdout("\n");
             $this->controller->stderr("Not found: {$pathFrom}", Console::FG_RED);
         } else {
             if ($sftpHelper->isFile($pathFrom) && $sftpHelper->isDir($pathTo)) {
                 $pathTo = $insidePathTo;
             }
             if (!$overwrite && $sftpHelper->isDir($pathFrom) && $sftpHelper->isDir($pathTo)) {
                 if (!$sftpHelper->fileExists($insidePathTo)) {
                     // not overwriting; copy the source directory into the destination folder:
                     $res = $sftpHelper->rename($pathFrom, $insidePathTo);
                 } else {
                     $this->controller->stdout("\n");
                     $this->controller->warn("Destination directory {$insidePathTo} already exists; not overwriting");
                 }
             } elseif (!$overwrite && $sftpHelper->isFile($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 $this->controller->stdout("\n");
                 $this->controller->warn("Destination file {$pathTo} already exists; not overwriting");
             } elseif ($sftpHelper->isDir($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 $this->controller->stdout("\n");
                 $this->controller->stderr("Trying to move a directory to a file: {$pathTo}", Console::FG_RED);
             } elseif (!$sftpHelper->fileExists($pathTo) || $overwrite && $sftpHelper->isDir($pathFrom) && $sftpHelper->isDir($pathTo) || $overwrite && $sftpHelper->isFile($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 // if destination exists, overwrite it with the source file/dir
                 // note: if pathTo is a directory, it has to be empty in order to be overwritten
                 try {
                     $res = $sftpHelper->rename($pathFrom, $pathTo);
                 } catch (ErrorException $e) {
                     $this->controller->stdout("\n");
                     $this->controller->warn($e->getMessage());
                 }
                 if (!$res) {
                     // Note: using sftp, renaming a directory to another does not work even if destination is empty
                     $this->controller->stdout("\n");
                     $this->controller->stderr("Failed moving {$pathFrom} to {$pathTo}", Console::FG_RED);
                 }
             }
         }
         $sftpHelper->flushCache();
     } else {
         $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     $this->controller->stdout("\n");
     return $res;
 }