Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEP Upgrade legue/flysystem to version 3.0 #524

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _config/asset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ SilverStripe\Core\Injector\Injector:
class: SilverStripe\Assets\Flysystem\ProtectedAssetAdapter
# Define the default filesystem
League\Flysystem\Filesystem.public:
class: League\Flysystem\Filesystem
class: SilverStripe\Assets\Flysystem\Filesystem
constructor:
FilesystemAdapter: '%$SilverStripe\Assets\Flysystem\PublicAdapter'
FilesystemConfig:
visibility: public
# Define the secondary filesystem for protected assets
League\Flysystem\Filesystem.protected:
class: League\Flysystem\Filesystem
class: SilverStripe\Assets\Flysystem\Filesystem
constructor:
FilesystemAdapter: '%$SilverStripe\Assets\Flysystem\ProtectedAdapter'
FilesystemConfig:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"silverstripe/vendor-plugin": "^2",
"symfony/filesystem": "^6.1",
"intervention/image": "^2.7.2",
"league/flysystem": "^1.1.9"
"league/flysystem": "^3.9.0"
},
"require-dev": {
"silverstripe/recipe-testing": "^3",
Expand Down
8 changes: 4 additions & 4 deletions src/Dev/Tasks/LegacyThumbnailMigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected function migrateFolder(FlysystemAssetStore $store, Folder $folder)

$foundError = false;
// Recurse through folder
foreach ($filesystem->listContents($resampledFolderPath, true) as $fileInfo) {
foreach ($filesystem->listContents($resampledFolderPath, true)->toArray() as $fileInfo) {
if ($fileInfo['type'] !== 'file') {
continue;
}
Expand Down Expand Up @@ -179,7 +179,7 @@ protected function migrateFolder(FlysystemAssetStore $store, Folder $folder)
continue;
}

$filesystem->rename($oldResampledPath, $newResampledPath);
$filesystem->move($oldResampledPath, $newResampledPath);

$this->logger->info(sprintf('Moved legacy thumbnail %s to %s', $oldResampledPath, $newResampledPath));

Expand All @@ -190,13 +190,13 @@ protected function migrateFolder(FlysystemAssetStore $store, Folder $folder)
// get migrated leave the folder where it is.
if (!$foundError) {
$files = array_filter(
$filesystem->listContents($resampledFolderPath, true) ?? [],
$filesystem->listContents($resampledFolderPath, true)->toArray() ?? [],
function ($file) {
return $file['type'] === 'file';
}
);
if (empty($files)) {
$filesystem->deleteDir($resampledFolderPath);
$filesystem->deleteDirectory($resampledFolderPath);
} else {
// This should not be possible. If it is, then there's probably a bug.
$this->logger->error(sprintf(
Expand Down
20 changes: 10 additions & 10 deletions src/Dev/Tasks/NormaliseAccessMigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Exception;
use InvalidArgumentException;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\FilesystemOperator;
use LogicException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand Down Expand Up @@ -290,7 +290,7 @@ private function cleanup()
foreach ($this->public_folders_to_truncate as $path) {
if ($this->canBeTruncated($path, $fs)) {
$this->info(sprintf('Deleting empty folder %s', $path));
$fs->deleteDir($path);
$fs->deleteDirectory($path);
$truncatedPaths[] = $path;
}
}
Expand All @@ -308,17 +308,17 @@ private function cleanup()
/**
* Check if the provided folder can be deleted on this Filesystem
* @param string $path
* @param FilesystemInterface $fs
* @param FilesystemOperator $fs
* @return bool
*/
private function canBeTruncated($path, FilesystemInterface $fs)
private function canBeTruncated($path, FilesystemOperator $fs)
{
if (!$fs->has($path)) {
if (!$fs->directoryExists($path)) {
// The folder doesn't exists
return false;
}

$contents = $fs->listContents($path);
$contents = $fs->listContents($path, true)->toArray();

foreach ($contents as $content) {
if ($content['type'] !== 'dir') {
Expand All @@ -341,14 +341,14 @@ private function canBeTruncated($path, FilesystemInterface $fs)
/**
* Delete this folder if it doesn't contain any files and parent folders if they don't contain any files either.
* @param string $path
* @param FilesystemInterface $fs
* @param FilesystemOperator $fs
*/
private function recursiveTruncate($path, FilesystemInterface $fs)
private function recursiveTruncate($path, FilesystemOperator $fs)
{
if ($path && ltrim($path ?? '', '.') && empty($fs->listContents($path))
if ($path && ltrim($path ?? '', '.') && empty($fs->listContents($path)->toArray())
) {
$this->info(sprintf('Deleting empty folder %s', $path));
$fs->deleteDir($path);
$fs->deleteDirectory($path);
$this->recursiveTruncate(dirname($path ?? ''), $fs);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/Dev/TestAssetStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace SilverStripe\Assets\Dev;

use League\Flysystem\Adapter\Local;
use League\Flysystem\AdapterInterface;
use League\Flysystem\Filesystem;
use League\Flysystem\Visibility;
use SilverStripe\Assets\FilenameParsing\FileResolutionStrategy;
use SilverStripe\Assets\Filesystem as SSFilesystem;
use SilverStripe\Assets\Flysystem\LocalFilesystemAdapter;
use SilverStripe\Assets\Flysystem\FlysystemAssetStore;
use SilverStripe\Assets\Flysystem\ProtectedAssetAdapter;
use SilverStripe\Assets\Flysystem\PublicAssetAdapter;
Expand All @@ -16,6 +15,7 @@
use SilverStripe\Assets\Storage\AssetStoreRouter;
use SilverStripe\Assets\Storage\DBFile;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Flysystem\Filesystem;
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Storage\GeneratedAssetHandler;
use SilverStripe\Control\Controller;
Expand Down Expand Up @@ -65,14 +65,14 @@ public static function activate($basedir)
$publicFilesystem = new Filesystem(
$publicAdapter,
[
'visibility' => AdapterInterface::VISIBILITY_PUBLIC
'visibility' => Visibility::PUBLIC
]
);
$protectedAdapter = new ProtectedAssetAdapter(ASSETS_PATH . '/' . $basedir . '/.protected');
$protectedFilesystem = new Filesystem(
$protectedAdapter,
[
'visibility' => AdapterInterface::VISIBILITY_PRIVATE
'visibility' => Visibility::PRIVATE
]
);

Expand Down Expand Up @@ -156,9 +156,9 @@ public static function getLocalPath(AssetContainer $asset, $forceProtected = fal
if (!$forceProtected && !$filesystem->has($fileID)) {
$filesystem = $assetStore->getPublicFilesystem();
}
/** @var Local $adapter */
/** @var LocalFilesystemAdapter $adapter */
$adapter = $filesystem->getAdapter();
return $relative ? $fileID : $adapter->applyPathPrefix($fileID);
return $relative ? $fileID : $adapter->prefixPath($fileID);
}

public function cleanFilename($filename)
Expand Down
5 changes: 3 additions & 2 deletions src/FilenameParsing/FileIDHelperResolutionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use League\Flysystem\Filesystem;
use League\Flysystem\UnableToCheckExistence;
use SilverStripe\Assets\Storage\FileHashingService;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
Expand Down Expand Up @@ -298,7 +299,7 @@ private function validateHash(FileIDHelper $helper, ParsedFileID $parsedFileID,
* @param ParsedFileID $parsedFileID
* @param Filesystem $filesystem
* @return bool|string
* @throws \League\Flysystem\FileNotFoundException
* @throws UnableToCheckExistence
*/
private function findHashOf(FileIDHelper $helper, ParsedFileID $parsedFileID, Filesystem $filesystem)
{
Expand Down Expand Up @@ -436,7 +437,7 @@ public function findVariants($tuple, Filesystem $filesystem)

// Find the correct folder to search for possible variants in
$folder = $helper->lookForVariantIn($parsedFileID);
$possibleVariants = $filesystem->listContents($folder, $helper->lookForVariantRecursive());
$possibleVariants = $filesystem->listContents($folder, $helper->lookForVariantRecursive())->toArray();

// Flysystem returns array of meta data abouch each file, we remove directories and map it down to the path
$possibleVariants = array_filter($possibleVariants ?? [], function ($possibleVariant) {
Expand Down
2 changes: 1 addition & 1 deletion src/FilenameParsing/FileResolutionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function parseFileID($fileID);
* @param array|ParsedFileID $tuple
* @param Filesystem $filesystem
* @return generator|ParsedFileID[]|null
* @throws \League\Flysystem\FileNotFoundException
* @throws \League\Flysystem\UnableToCheckExistence
*/
public function findVariants($tuple, Filesystem $filesystem);

Expand Down
29 changes: 16 additions & 13 deletions src/Flysystem/AssetAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace SilverStripe\Assets\Flysystem;

use Exception;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Config as FlysystemConfig;
use League\Flysystem\UnableToWriteFile;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Filesystem;
use SilverStripe\Core\Config\Config;
Expand All @@ -16,7 +16,7 @@
/**
* Adapter for local filesystem based on assets directory
*/
class AssetAdapter extends Local
class AssetAdapter extends LocalFilesystemAdapter
{
use Configurable;

Expand Down Expand Up @@ -61,8 +61,10 @@ public function __construct($root = null, $writeFlags = LOCK_EX, $linkHandling =
$root = realpath($root ?? '');

// Override permissions with config
$permissions = $this->normalisePermissions($this->config()->get('file_permissions'));
parent::__construct($root, $writeFlags, $linkHandling, $permissions);
$permissions = PortableVisibilityConverter::fromArray(
$this->normalisePermissions($this->config()->get('file_permissions'))
);
parent::__construct($root, $permissions, $writeFlags, $linkHandling);

// Configure server
$this->configureServer();
Expand Down Expand Up @@ -124,7 +126,7 @@ public function flush()
* Configure server files for this store
*
* @param bool $forceOverwrite Force regeneration even if files already exist
* @throws Exception
* @throws UnableToWriteFile
*/
protected function configureServer($forceOverwrite = false)
{
Expand All @@ -146,18 +148,19 @@ protected function configureServer($forceOverwrite = false)

// Apply each configuration
$config = new FlysystemConfig();
$config->set('visibility', $visibility);
$config->extend(['visibility' => $visibility]);
foreach ($configurations as $file => $template) {
// Ensure file contents
if ($forceOverwrite || !$this->has($file)) {
if ($forceOverwrite || !$this->fileExists($file)) {
// Evaluate file
$content = $this->renderTemplate($template);
$success = $this->write($file, $content, $config);
if (!$success) {
throw new Exception("Error writing server configuration file \"{$file}\"");
try {
$content = $this->renderTemplate($template);
$this->write($file, $content, $config);
} catch (UnableToWriteFile $exception) {
throw UnableToWriteFile::atLocation($file, $exception->reason(), $exception);
}
}
$perms = $this->getVisibility($file);
$perms = $this->visibility($file);
if ($perms['visibility'] !== $visibility) {
// Ensure correct permissions
$this->setVisibility($file, $visibility);
Expand Down
35 changes: 35 additions & 0 deletions src/Flysystem/Filesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace SilverStripe\Assets\Flysystem;

use League\Flysystem\Filesystem as LeagueFilesystem;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\PathNormalizer;
use League\Flysystem\WhitespacePathNormalizer;

class Filesystem extends LeagueFilesystem
{
private $adapter;

public function __construct(
FilesystemAdapter $adapter,
array $config = [],
PathNormalizer $pathNormalizer = null
) {
$this->adapter = $adapter;
$this->pathNormalizer = $pathNormalizer ?: new WhitespacePathNormalizer();
parent::__construct($adapter, $config, $pathNormalizer);
}

public function getAdapter(): FilesystemAdapter
{
return $this->adapter;
}

public function has(string $location): bool
{
$path = $this->pathNormalizer->normalizePath($location);

return strlen($path) === 0 ? false : ($this->getAdapter()->fileExists($path) || $this->getAdapter()->directoryExists($path));
}
}
Loading