Skip to content
Snippets Groups Projects
Commit a8d20d0a authored by Borut Piletic's avatar Borut Piletic
Browse files

Improve destination path resolver.

parent cfd8d226
Branches
Tags 8.x-1.0-alpha5
1 merge request!4Improve destination path resolver.
......@@ -64,7 +64,7 @@ class FTPBatch implements BatchInterface {
->setSettingsFromPlugin($batch['params'])
->connect();
$destination_path = $client->resolveRealpath($batch['params']['destination_path']);
$destination_path = $client->resolveRealpath($batch['params']['destination_path'], TRUE);
foreach ($batch['files'] as $source_file) {
$client->getFile($source_file, $destination_path . DIRECTORY_SEPARATOR . basename($source_file));
......
......@@ -3,7 +3,6 @@
namespace Drupal\file_sync;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\FileTransfer\FileTransferException;
use Drupal\Core\FileTransfer\FTPExtension;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -126,21 +125,18 @@ class FTPClient implements FTPClientInterface {
public function getFile(string $source_file, string $destination_file, $return = FALSE): ?string {
$connection = $this->getConnection();
// Make sure destination path exists.
$destination_path = pathinfo($destination_file, PATHINFO_DIRNAME);
if (!is_dir($destination_path) && !$this->fileSystem->prepareDirectory($destination_path, FileSystemInterface::CREATE_DIRECTORY)) {
throw new \Exception('Could not create the destination path: ' . $destination_path);
}
// Download the file from FTP server.
if (!@ftp_get($connection, $destination_file, $source_file, FTP_BINARY)) {
throw new FileTransferException('Cannot download @source to @destination', [
'@source' => $source_file,
'@destination' => $destination_file,
]);
throw new \Exception('Cannot download ' . $source_file . ' to ' . $destination_file);
}
if ($return) {
if (!touch($destination_file) || !is_readable($destination_file)) {
throw new \Exception('Failed to download the "%source" to path "%destination"', [
'%source' => $source_file,
'%destination' => $destination_file,
]);
}
// Open the downloaded temp file and read its contents.
$file_contents = '';
$file_resource = fopen($destination_file, 'r');
......@@ -212,13 +208,21 @@ class FTPClient implements FTPClientInterface {
*
* @param string $path
* Path to file.
* @param bool $create
* Create path if it does not exist.
*
* @return string
* Resloved realpath.
*/
public function resolveRealpath(string $path): string {
public function resolveRealpath(string $path, bool $create = FALSE): string {
// If stream wrapper is used convert scheme to realpath.
if ($this->streamWrapperManager->getScheme($path)) {
$realpath = $this->fileSystem->realpath($path);
if ($create && !is_dir($realpath)) {
if (!$this->fileSystem->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY)) {
throw new \Exeception('Failed to resolve the path');
}
}
$path = $this->fileSystem->realpath($path);
}
return $path;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment