Skip to content

Commit

Permalink
Upgrade flysystem to ver 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Kilo committed Mar 22, 2021
1 parent 652e020 commit 81fd686
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 225 deletions.
64 changes: 33 additions & 31 deletions app/code/Magento/AwsS3/Driver/AwsS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
use Generator;
use League\Flysystem\Config;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemException;
use League\Flysystem\Visibility;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\FileSystemException as MagentoFileSystemException;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\Framework\Phrase;
use Magento\RemoteStorage\Driver\DriverException;
Expand Down Expand Up @@ -89,7 +90,7 @@ public function test(): void
{
try {
$this->adapter->write(self::TEST_FLAG, '', new Config(self::CONFIG));
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
throw new DriverException(__($e->getMessage()), $e);
}
}
Expand All @@ -108,7 +109,7 @@ public function fileGetContents($path, $flag = null, $context = null): string
}
try {
$contents = $this->adapter->read($path);
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());
return '';
}
Expand All @@ -132,11 +133,14 @@ public function isExists($path): bool
}

try {
return $this->adapter->fileExists($path);
} catch (\League\Flysystem\FilesystemException $e) {
if ($this->adapter->fileExists($path) || $this->isDirectory($path)) {
return true;
}
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());
return false;
}

return false;
}

/**
Expand Down Expand Up @@ -164,7 +168,6 @@ public function createDirectory($path, $permissions = 0777): bool
*
* @param string $path
* @return bool
* @throws FileSystemException
*/
private function createDirectoryRecursively(string $path): bool
{
Expand All @@ -179,7 +182,7 @@ private function createDirectoryRecursively(string $path): bool
if (!$this->isDirectory($path)) {
try {
$this->adapter->createDirectory($this->fixPath($path), new Config(self::CONFIG));
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());
return false;
}
Expand All @@ -197,9 +200,9 @@ public function copy($source, $destination, DriverInterface $targetDriver = null
$this->adapter->copy(
$this->normalizeRelativePath($source, true),
$this->normalizeRelativePath($destination, true),
new Config(self::CONFIG)
new Config([])
);
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());

return false;
Expand All @@ -217,7 +220,7 @@ public function deleteFile($path): bool
$this->adapter->delete(
$this->normalizeRelativePath($path, true)
);
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());

return false;
Expand All @@ -235,7 +238,7 @@ public function deleteDirectory($path): bool
$this->adapter->deleteDirectory(
$this->normalizeRelativePath($path, true)
);
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());

return false;
Expand All @@ -262,7 +265,7 @@ public function filePutContents($path, $content, $mode = null): int
try {
$this->adapter->write($path, $content, new Config($config));
$size = $this->adapter->fileSize($path)->fileSize();
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());

return 0;
Expand Down Expand Up @@ -410,7 +413,7 @@ public function isFile($path): bool

try {
return $this->adapter->fileExists($path);
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException $e) {
$this->logger->error($e);
return false;
}
Expand Down Expand Up @@ -438,9 +441,8 @@ public function isDirectory($path): bool
return true;
}
}
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());
return false;
}

return false;
Expand Down Expand Up @@ -492,9 +494,9 @@ public function rename($oldPath, $newPath, DriverInterface $targetDriver = null)
$this->adapter->move(
$this->normalizeRelativePath($oldPath, true),
$this->normalizeRelativePath($newPath, true),
new Config(self::CONFIG)
new Config([])
);
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());

return false;
Expand All @@ -513,9 +515,9 @@ public function stat($path): array
$size = $this->adapter->fileSize($path)->fileSize();
$type = $this->adapter->mimeType($path)->mimeType();
$mtime = $this->adapter->lastModified($path)->lastModified();
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());
throw new FileSystemException(__('Cannot gather stats! %1', [$this->getWarningMessage()]));
throw new MagentoFileSystemException(__('Cannot gather stats! %1', [$this->getWarningMessage()]));
}

return [
Expand Down Expand Up @@ -549,9 +551,9 @@ public function getMetadata(string $path): array
$size = $this->adapter->fileSize($path)->fileSize();
$timestamp = $this->adapter->lastModified($path)->lastModified();
$metaInfo = $this->adapter->getMetadata($path);
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());
throw new FileSystemException(__('Cannot gather meta info! %1', [$this->getWarningMessage()]));
throw new MagentoFileSystemException(__('Cannot gather meta info! %1', [$this->getWarningMessage()]));
}

return [
Expand Down Expand Up @@ -641,7 +643,7 @@ public function touch($path, $modificationTime = null): bool
$content = $this->adapter->fileExists($path) ? $this->adapter->read($path) : '';
try {
$this->adapter->write($path, $content, new Config([]));
} catch (\League\Flysystem\FilesystemException $e) {
} catch (FilesystemException|\Exception $e) {
$this->logger->error($e->getMessage());

return false;
Expand All @@ -659,7 +661,7 @@ public function fileReadLine($resource, $length, $ending = null): string
$result = @stream_get_line($resource, $length, $ending);
// phpcs:enable
if (false === $result) {
throw new FileSystemException(
throw new MagentoFileSystemException(
new Phrase('File cannot be read %1', [$this->getWarningMessage()])
);
}
Expand All @@ -675,7 +677,7 @@ public function fileRead($resource, $length): string
//phpcs:ignore Magento2.Functions.DiscouragedFunction
$result = fread($resource, $length);
if ($result === false) {
throw new FileSystemException(__('File cannot be read %1', [$this->getWarningMessage()]));
throw new MagentoFileSystemException(__('File cannot be read %1', [$this->getWarningMessage()]));
}

return $result;
Expand All @@ -689,7 +691,7 @@ public function fileGetCsv($resource, $length = 0, $delimiter = ',', $enclosure
//phpcs:ignore Magento2.Functions.DiscouragedFunction
$result = fgetcsv($resource, $length, $delimiter, $enclosure, $escape);
if ($result === null) {
throw new FileSystemException(
throw new MagentoFileSystemException(
new Phrase(
'The "%1" CSV handle is incorrect. Verify the handle and try again.',
[$this->getWarningMessage()]
Expand All @@ -707,7 +709,7 @@ public function fileTell($resource): int
{
$result = @ftell($resource);
if ($result === null) {
throw new FileSystemException(
throw new MagentoFileSystemException(
new Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
);
}
Expand All @@ -722,7 +724,7 @@ public function fileSeek($resource, $offset, $whence = SEEK_SET): int
{
$result = @fseek($resource, $offset, $whence);
if ($result === -1) {
throw new FileSystemException(
throw new MagentoFileSystemException(
new Phrase(
'An error occurred during "%1" fileSeek execution.',
[$this->getWarningMessage()]
Expand Down Expand Up @@ -757,7 +759,7 @@ public function fileFlush($resource): bool
{
$result = @fflush($resource);
if (!$result) {
throw new FileSystemException(
throw new MagentoFileSystemException(
new Phrase(
'An error occurred during "%1" fileFlush execution.',
[$this->getWarningMessage()]
Expand All @@ -775,7 +777,7 @@ public function fileLock($resource, $lockMode = LOCK_EX): bool
{
$result = @flock($resource, $lockMode);
if (!$result) {
throw new FileSystemException(
throw new MagentoFileSystemException(
new Phrase(
'An error occurred during "%1" fileLock execution.',
[$this->getWarningMessage()]
Expand All @@ -793,7 +795,7 @@ public function fileUnlock($resource): bool
{
$result = @flock($resource, LOCK_UN);
if (!$result) {
throw new FileSystemException(
throw new MagentoFileSystemException(
new Phrase(
'An error occurred during "%1" fileUnlock execution.',
[$this->getWarningMessage()]
Expand Down
14 changes: 8 additions & 6 deletions app/code/Magento/AwsS3/Model/Cached/CachedAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ class CachedAdapter implements FilesystemAdapter
/**
* @var FilesystemAdapter
*/
private FilesystemAdapter $adapter;
private $adapter;

/**
* @var CacheInterface
*/
private CacheInterface $cache;
private $cache;

/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
private $logger;

/**
* @var GetPathInfo
*/
private GetPathInfo $getPathInfo;
private $getPathInfo;

/**
* @param FilesystemAdapter $adapter
Expand Down Expand Up @@ -289,8 +289,10 @@ public function visibility(string $path): FileAttributes
public function getMetadata(string $path): array
{
$fileAttributes = $this->adapter->fileSize($path)->jsonSerialize();
$width = (int)$fileAttributes['extra_metadata']['Metadata']['image-width'] ?? 0;
$height = (int)$fileAttributes['extra_metadata']['Metadata']['image-height'] ?? 0;
$width = isset($fileAttributes['extra_metadata']['Metadata']['image-width'])
? (int)$fileAttributes['extra_metadata']['Metadata']['image-width'] : 0;
$height = isset($fileAttributes['extra_metadata']['Metadata']['image-height'])
? (int)$fileAttributes['extra_metadata']['Metadata']['image-height'] : 0;
$pathInfo = $this->getPathInfo->execute($fileAttributes['path']);

return [
Expand Down
8 changes: 4 additions & 4 deletions app/code/Magento/RemoteStorage/Driver/Cache/CacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ class CacheFactory
/**
* @var MemoryFactory
*/
private MemoryFactory $memoryFactory;
private $memoryFactory;

/**
* @var LocalFactory
*/
private LocalFactory $localFactory;
private $localFactory;

/**
* @var PredisFactory
*/
private PredisFactory $predisFactory;
private $predisFactory;

/**
* @var CacheInterfaceFactory
*/
private CacheInterfaceFactory $cacheFactory;
private $cacheFactory;

/**
* @param CacheInterfaceFactory $cacheFactory
Expand Down
Loading

0 comments on commit 81fd686

Please sign in to comment.