Skip to content

Commit

Permalink
Composer: upgrade to flysystem v2
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jan 4, 2024
1 parent f8d527b commit 37b2408
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 86 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
],
"require": {
"php": ">=8.1",
"ext-fileinfo": "*",
"nette/utils": "^4.0.3",
"league/flysystem": "^1.1.0",
"league/flysystem": "^2.5.0",
"psr/event-dispatcher": "^1.0.0"
},
"require-dev": {
Expand All @@ -30,13 +31,13 @@
"contributte/phpstan": "^0.1",
"ramsey/uuid": "^4.7.5",
"spatie/temporary-directory": "^1.3.0",
"superbalist/flysystem-google-storage": "^7.2.2",
"symfony/dependency-injection": "^6.4.0 || ^7.0.0",
"symfony/filesystem": "^6.4.0 || ^7.0.0",
"symfony/http-foundation": "^6.4.0 || ^7.0.0",
"symfony/http-kernel": "^6.4.0 || ^7.0.0",
"symfony/serializer": "^6.4.0 || ^7.0.0",
"tracy/tracy": "^2.10.5"
"tracy/tracy": "^2.10.5",
"league/flysystem-google-cloud-storage": "^2.4.4"
},
"autoload": {
"psr-4": {
Expand Down
92 changes: 40 additions & 52 deletions src/Filesystem/FilesystemAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,116 +3,104 @@
namespace Contributte\Imagist\Filesystem;

use Contributte\Imagist\Exceptions\FileException;
use Contributte\Imagist\Exceptions\FileNotFoundException;
use Contributte\Imagist\PathInfo\PathInfoInterface;
use League\Flysystem\FileNotFoundException;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface as LeagueFilesystemInterface;
use League\Flysystem\Config;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemException;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToReadFile;
use League\Flysystem\UnableToRetrieveMetadata;

abstract class FilesystemAbstract implements FilesystemInterface
{

protected LeagueFilesystemInterface $adapter;
protected FilesystemAdapter $adapter;

public function __construct(LeagueFilesystemInterface $adapter)
public function __construct(FilesystemAdapter $adapter)
{
$this->adapter = $adapter;
}

/**
* @inheritDoc
* {@inheritDoc}
*/
public function exists(PathInfoInterface $path): bool
{
return $this->adapter->has($path->toString());
return $this->adapter->fileExists($path->toString());
}

/**
* @inheritDoc
* {@inheritDoc}
*/
public function delete(PathInfoInterface $path): bool
{
return $this->withTemporaryConfig(
'disable_asserts',
true,
false,
fn (): bool => $this->adapter->delete($path->toString())
);
try {
$this->adapter->delete($path->toString());
} catch (UnableToDeleteFile $e) {
throw new FileException(sprintf('Cannot delete file %s', $path->toString()), 0, $e);
}

return true;
}

/**
* @inheritDoc
* @return array<StorageAttributes>
*/
public function listContents(string $path): array
public function listContents(string $path): iterable
{
return $this->adapter->listContents($path);
return $this->adapter->listContents($path, false);
}

/**
* @inheritDoc
* {@inheritDoc}
*/
public function put(PathInfoInterface $path, string $content, array $config = []): void
{
$this->adapter->put($path->toString(), $content, $config);
$this->adapter->write($path->toString(), $content, new Config($config));
}

/**
* @inheritDoc
* {@inheritDoc}
*/
public function putWithMkdir(PathInfoInterface $path, string $content, array $config = []): void
{
$this->adapter->createDir($path->toString($path::BUCKET | $path::SCOPE | $path::FILTER));
$this->adapter->createDirectory($path->toString($path::BUCKET | $path::SCOPE | $path::FILTER), new Config());

$this->put($path, $content, $config);
}

/**
* @inheritDoc
* {@inheritDoc}
*/
public function read(PathInfoInterface $path): string
{
try {
$content = $this->adapter->read($path->toString());
} catch (FileNotFoundException $exception) {
throw new \Contributte\Imagist\Exceptions\FileNotFoundException($exception->getMessage());
}

if ($content === false) {
throw new FileException(sprintf('Cannot read file %s', $path->toString()));
} catch (UnableToReadFile $e) {
throw new FileNotFoundException(sprintf('Cannot read file %s', $path->toString()), 0, $e);
} catch (FilesystemException $e) {
throw new FileNotFoundException($e->getMessage(), 0, $e);
}

return $content;
}

/**
* @inheritDoc
* {@inheritDoc}
*/
public function mimeType(PathInfoInterface $path): ?string
{
$mimeType = $this->adapter->getMimetype($path->toString());

return $mimeType === false ? null : $mimeType;
}

/**
* @template T
* @param mixed $value
* @param mixed $default
* @param callable(): T $callback
* @return T
*/
protected function withTemporaryConfig(string $name, $value, $default, callable $callback) // phpcs:ignore -- phpcs bug
{
if (!$this->adapter instanceof Filesystem) {
return $callback();
try {
$mimeType = $this->adapter->mimeType($path->toString());
} catch (UnableToRetrieveMetadata $e) {
throw new FileException(sprintf('Cannot get mime type of file %s', $path->toString()), 0, $e);
} catch (FilesystemException $e) {
throw new FileNotFoundException($e->getMessage(), 0, $e);
}

$default = $this->adapter->getConfig()->get($name, $default);

$this->adapter->getConfig()->set($name, $value);
$result = $callback();
$this->adapter->getConfig()->set($name, $default);

return $result;
return $mimeType->mimeType();
}

}
2 changes: 1 addition & 1 deletion src/Filesystem/FilesystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function delete(PathInfoInterface $path): mixed;
/**
* @return mixed[]
*/
public function listContents(string $path): array;
public function listContents(string $path): iterable;

/**
* @param mixed[] $config
Expand Down
29 changes: 3 additions & 26 deletions src/Filesystem/GoogleStorageFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,28 @@
namespace Contributte\Imagist\Filesystem;

use Contributte\Imagist\PathInfo\PathInfoInterface;
use Google\Cloud\Core\Exception\NotFoundException;
use Google\Cloud\Storage\StorageClient;
use League\Flysystem\Filesystem;
use LogicException;
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter;
use League\Flysystem\GoogleCloudStorage\GoogleCloudStorageAdapter;

final class GoogleStorageFilesystem extends FilesystemAbstract
{

public function __construct(string $bucket, string $keyFilePath)
{
if (!class_exists(GoogleStorageAdapter::class)) {
throw new LogicException(
sprintf(
'%s not found, install it by composer install superbalist/flysystem-google-storage',
self::class
)
);
}

$client = new StorageClient([
'keyFilePath' => $keyFilePath,
]);
$bucket = $client->bucket($bucket);

parent::__construct(new Filesystem(
new GoogleStorageAdapter($client, $bucket)
));
parent::__construct(new GoogleCloudStorageAdapter($bucket));
}

/**
* @inheritDoc
* {@inheritDoc}
*/
public function putWithMkdir(PathInfoInterface $path, string $content, array $config = []): void
{
$this->put($path, $content, $config);
}

public function delete(PathInfoInterface $path): bool
{
try {
return parent::delete($path);
} catch (NotFoundException $exception) {
return false;
}
}

}
5 changes: 2 additions & 3 deletions src/Filesystem/LocalFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace Contributte\Imagist\Filesystem;

use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;

final class LocalFilesystem extends FilesystemAbstract
{

public function __construct(string $root)
{
parent::__construct(new Filesystem(new Local($root)));
parent::__construct(new LocalFilesystemAdapter($root));
}

}
7 changes: 6 additions & 1 deletion src/Remover/PersistentImageRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Contributte\Imagist\Filter\Internal\VoidFilter;
use Contributte\Imagist\PathInfo\PathInfoFactoryInterface;
use DomainException;
use League\Flysystem\StorageAttributes;

final class PersistentImageRemover implements RemoverInterface
{
Expand Down Expand Up @@ -50,6 +51,10 @@ private function removeFiltered(PersistentImageInterface $image): void
$path = $this->pathInfoFactory->create($image->withFilter(new VoidFilter('void')));

foreach ($this->filesystem->listContents($path->toString($path::BUCKET | $path::SCOPE)) as $path) {
if ($path instanceof StorageAttributes) {
$path = $path->jsonSerialize();
}

if (!is_array($path)) {
continue;
}
Expand All @@ -60,7 +65,7 @@ private function removeFiltered(PersistentImageInterface $image): void
continue;
}

$filename = $path['filename'] ?? '';
$filename = isset($path['path']) ? basename($path['path']) : '';

if (!is_string($filename) || !$filename || $filename[0] !== '_') {
continue;
Expand Down

0 comments on commit 37b2408

Please sign in to comment.