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 18, 2021
1 parent f31285a commit 8013bea
Show file tree
Hide file tree
Showing 19 changed files with 1,943 additions and 358 deletions.
203 changes: 136 additions & 67 deletions app/code/Magento/AwsS3/Driver/AwsS3.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
namespace Magento\AwsS3\Driver;

use Generator;
use Exception;
use League\Flysystem\AdapterInterface;
use League\Flysystem\Config;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\StorageAttributes;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\Framework\Phrase;
use Psr\Log\LoggerInterface;
use Magento\RemoteStorage\Driver\DriverException;
use Magento\RemoteStorage\Driver\RemoteDriverInterface;
use Psr\Log\LoggerInterface;

/**
* Driver for AWS S3 IO operations.
Expand All @@ -33,7 +32,7 @@ class AwsS3 implements RemoteDriverInterface
private const CONFIG = ['ACL' => 'private'];

/**
* @var AdapterInterface
* @var FilesystemOperator
*/
private $adapter;

Expand All @@ -53,12 +52,12 @@ class AwsS3 implements RemoteDriverInterface
private $objectUrl;

/**
* @param AdapterInterface $adapter
* @param FilesystemOperator $adapter
* @param LoggerInterface $logger
* @param string $objectUrl
*/
public function __construct(
AdapterInterface $adapter,
FilesystemOperator $adapter,
LoggerInterface $logger,
string $objectUrl
) {
Expand Down Expand Up @@ -88,9 +87,9 @@ public function __destruct()
public function test(): void
{
try {
$this->adapter->write(self::TEST_FLAG, '', new Config(self::CONFIG));
} catch (Exception $exception) {
throw new DriverException(__($exception->getMessage()), $exception);
$this->adapter->write(self::TEST_FLAG, '', self::CONFIG);
} catch (\League\Flysystem\FilesystemException $e) {
throw new DriverException(__($e->getMessage()), $e);
}
}

Expand All @@ -106,8 +105,14 @@ public function fileGetContents($path, $flag = null, $context = null): string
return file_get_contents(stream_get_meta_data($this->streams[$path])['uri']);
//phpcs:enable
}
try {
$contents = $this->adapter->read($path);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());
return '';
}

return $this->adapter->read($path)['contents'] ?? '';
return $contents;
}

/**
Expand All @@ -125,7 +130,12 @@ public function isExists($path): bool
return true;
}

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

/**
Expand Down Expand Up @@ -166,10 +176,12 @@ private function createDirectoryRecursively(string $path): bool
}

if (!$this->isDirectory($path)) {
return (bool)$this->adapter->createDir(
$this->fixPath($path),
new Config(self::CONFIG)
);
try {
$this->adapter->createDirectory($this->fixPath($path), self::CONFIG);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());
return false;
}
}

return true;
Expand All @@ -180,30 +192,55 @@ private function createDirectoryRecursively(string $path): bool
*/
public function copy($source, $destination, DriverInterface $targetDriver = null): bool
{
return $this->adapter->copy(
$this->normalizeRelativePath($source, true),
$this->normalizeRelativePath($destination, true)
);
try {
$this->adapter->copy(
$this->normalizeRelativePath($source, true),
$this->normalizeRelativePath($destination, true),
self::CONFIG
);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());

return false;
}

return true;
}

/**
* @inheritDoc
*/
public function deleteFile($path): bool
{
return $this->adapter->delete(
$this->normalizeRelativePath($path, true)
);
try {
$this->adapter->delete(
$this->normalizeRelativePath($path, true)
);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());

return false;
}

return true;
}

/**
* @inheritDoc
*/
public function deleteDirectory($path): bool
{
return $this->adapter->deleteDir(
$this->normalizeRelativePath($path, true)
);
try {
$this->adapter->deleteDirectory(
$this->normalizeRelativePath($path, true)
);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());

return false;
}

return true;
}

/**
Expand All @@ -217,11 +254,20 @@ public function filePutContents($path, $content, $mode = null): int
if (false !== ($imageSize = @getimagesizefromstring($content))) {
$config['Metadata'] = [
'image-width' => $imageSize[0],
'image-height' => $imageSize[1]
'image-height' => $imageSize[1],
];
}

return $this->adapter->write($path, $content, new Config($config))['size'];
try {
$this->adapter->write($path, $content, $config);
$size = $this->adapter->fileSize($path);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());

return 0;
}

return $size;
}

/**
Expand Down Expand Up @@ -361,11 +407,12 @@ public function isFile($path): bool

$path = $this->normalizeRelativePath($path, true);

if ($this->adapter->has($path) && ($meta = $this->adapter->getMetadata($path))) {
return ($meta['type'] ?? null) === self::TYPE_FILE;
try {
return $this->adapter->fileExists($path);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e);
return false;
}

return false;
}

/**
Expand All @@ -383,13 +430,16 @@ public function isDirectory($path): bool
return true;
}

if ($this->adapter->has($path)) {
$meta = $this->adapter->getMetadata($path);

return !($meta && $meta['type'] === self::TYPE_FILE);
try {
$directories = $this->adapter->listContents($path)
->filter(fn(StorageAttributes $attributes) => $attributes->isDir())
->filter(fn(StorageAttributes $attributes) => $attributes->path() === $path)
->toArray();
return count($directories) > 0;
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());
return false;
}

return false;
}

/**
Expand Down Expand Up @@ -434,10 +484,19 @@ public function getRealPath($path)
*/
public function rename($oldPath, $newPath, DriverInterface $targetDriver = null): bool
{
return $this->adapter->rename(
$this->normalizeRelativePath($oldPath, true),
$this->normalizeRelativePath($newPath, true)
);
try {
$this->adapter->move(
$this->normalizeRelativePath($oldPath, true),
$this->normalizeRelativePath($newPath, true),
self::CONFIG
);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());

return false;
}

return true;
}

/**
Expand All @@ -446,9 +505,12 @@ public function rename($oldPath, $newPath, DriverInterface $targetDriver = null)
public function stat($path): array
{
$path = $this->normalizeRelativePath($path, true);
$metaInfo = $this->adapter->getMetadata($path);

if (!$metaInfo) {
try {
$size = $this->adapter->fileSize($path);
$type = $this->adapter->mimeType($path);
$mtime = $this->adapter->lastModified($path);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());
throw new FileSystemException(__('Cannot gather stats! %1', [$this->getWarningMessage()]));
}

Expand All @@ -464,10 +526,10 @@ public function stat($path): array
'ctime' => 0,
'blksize' => 0,
'blocks' => 0,
'size' => $metaInfo['size'] ?? 0,
'type' => $metaInfo['type'] ?? '',
'mtime' => $metaInfo['timestamp'] ?? 0,
'disposition' => null
'size' => $size ?? 0,
'type' => $type ?? '',
'mtime' => $mtime ?? 0,
'disposition' => null,
];
}

Expand All @@ -477,16 +539,17 @@ public function stat($path): array
public function getMetadata(string $path): array
{
$path = $this->normalizeRelativePath($path, true);
$metaInfo = $this->adapter->getMetadata($path);

if (!$metaInfo) {
try {
$mimeType = $this->adapter->mimeType($path);
$size = $this->adapter->fileSize($path);
$timestamp = $this->adapter->lastModified($path);
$metaInfo = $this->adapter->getMetadata($path);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());
throw new FileSystemException(__('Cannot gather meta info! %1', [$this->getWarningMessage()]));
}

$mimeType = $this->adapter->getMimetype($path)['mimetype'];
$size = $this->adapter->getSize($path)['size'];
$timestamp = $this->adapter->getTimestamp($path)['timestamp'];

return [
'path' => $metaInfo['path'],
'dirname' => $metaInfo['dirname'],
Expand All @@ -498,8 +561,8 @@ public function getMetadata(string $path): array
'mimetype' => $mimeType,
'extra' => [
'image-width' => $metaInfo['metadata']['image-width'] ?? 0,
'image-height' => $metaInfo['metadata']['image-height'] ?? 0
]
'image-height' => $metaInfo['metadata']['image-height'] ?? 0,
],
];
}

Expand Down Expand Up @@ -571,11 +634,16 @@ public function touch($path, $modificationTime = null): bool
{
$path = $this->normalizeRelativePath($path, true);

$content = $this->adapter->has($path) ?
$this->adapter->read($path)['contents']
: '';
$content = $this->adapter->fileExists($path) ? $this->adapter->read($path) : '';
try {
$this->adapter->write($path, $content);
} catch (\League\Flysystem\FilesystemException $e) {
$this->logger->error($e->getMessage());

return false;
}

return (bool)$this->adapter->write($path, $content, new Config([]));
return true;
}

/**
Expand Down Expand Up @@ -764,7 +832,7 @@ public function fileClose($resource): bool
foreach ($this->streams as $path => $stream) {
//phpcs:disable
if (stream_get_meta_data($stream)['uri'] === $resourcePath) {
$this->adapter->writeStream($path, $resource, new Config(self::CONFIG));
$this->adapter->writeStream($path, $resource, self::CONFIG);

// Remove path from streams after
unset($this->streams[$path]);
Expand All @@ -785,9 +853,9 @@ public function fileOpen($path, $mode)

if (!isset($this->streams[$path])) {
$this->streams[$path] = tmpfile();
if ($this->adapter->has($path)) {
if ($this->adapter->fileExists($path)) {
//phpcs:ignore Magento2.Functions.DiscouragedFunction
fwrite($this->streams[$path], $this->adapter->read($path)['contents']);
fwrite($this->streams[$path], $this->adapter->read($path));
//phpcs:ignore Magento2.Functions.DiscouragedFunction
rewind($this->streams[$path]);
}
Expand Down Expand Up @@ -835,14 +903,15 @@ private function readPath(string $path, $isRecursive = false): array
$contentsList = $this->adapter->listContents(
$this->fixPath($relativePath),
$isRecursive
);
)->toArray();

$itemsList = [];
foreach ($contentsList as $item) {
$item = $item->jsonSerialize();
if (isset($item['path'])
&& $item['path'] !== $relativePath
&& (!$relativePath || strpos($item['path'], $relativePath) === 0)) {
$itemsList[] = $this->getAbsolutePath($item['dirname'], $item['path']);
$itemsList[] = $this->getAbsolutePath($item['path'], $item['path']);
}
}

Expand Down Expand Up @@ -874,7 +943,7 @@ private function getSearchPattern(string $pattern, array $parentPattern, string
$replacement = [
'/\*/' => '.*',
'/\?/' => '.',
'/\//' => '\/'
'/\//' => '\/',
];

return preg_replace(array_keys($replacement), array_values($replacement), $searchPattern);
Expand Down
Loading

0 comments on commit 8013bea

Please sign in to comment.