1
0
mirror of https://github.com/flarum/core.git synced 2025-10-13 07:54:25 +02:00

Use Laravel filesystem interface for assets and avatars (#2729)

* WIP: Use Laravel filesystem interface where possible
* Drop vendorFilesystem
* Support getting URL of cloud-based logo and favicon
* FilesystemAdapter should always be cloud
* Get base avatar URL from filesystem adapter
* Restore deleted getAsset method

Co-authored-by: Alexander Skvortsov <askvortsov1@users.noreply.github.com>
This commit is contained in:
Alexander Skvortsov
2021-04-19 15:11:03 -04:00
committed by GitHub
parent a2d77d7b81
commit f67149bb06
13 changed files with 82 additions and 92 deletions

View File

@@ -12,15 +12,12 @@ namespace Flarum\Extension;
use Flarum\Database\Migrator;
use Flarum\Extend\LifecycleInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemInterface;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\MountManager;
use League\Flysystem\Plugin\ListFiles;
/**
* @property string $name
@@ -428,16 +425,13 @@ class Extension implements Arrayable
return;
}
$mount = new MountManager([
'source' => $source = new Filesystem(new Local($this->getPath().'/assets')),
'target' => $target,
]);
$source = new Filesystem();
$source->addPlugin(new ListFiles);
$assetFiles = $source->listFiles('/', true);
$assetFiles = $source->allFiles("$this->path/assets");
foreach ($assetFiles as $file) {
$mount->copy("source://$file[path]", "target://extensions/$this->id/$file[path]");
foreach ($assetFiles as $fullPath) {
$relPath = substr($fullPath, strlen("$this->path/assets"));
$target->put("extensions/$this->id/$relPath", $source->get($fullPath));
}
}

View File

@@ -19,6 +19,8 @@ use Flarum\Foundation\Paths;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Filesystem\Cloud as FilesystemInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Schema\Builder;
use Illuminate\Filesystem\Filesystem;
@@ -48,6 +50,11 @@ class ExtensionManager
*/
protected $filesystem;
/**
* @var FilesystemInterface
*/
protected $assetsFilesystem;
/**
* @var Collection|null
*/
@@ -59,7 +66,8 @@ class ExtensionManager
Container $container,
Migrator $migrator,
Dispatcher $dispatcher,
Filesystem $filesystem
Filesystem $filesystem,
Factory $filesystemFactory
) {
$this->config = $config;
$this->paths = $paths;
@@ -67,6 +75,7 @@ class ExtensionManager
$this->migrator = $migrator;
$this->dispatcher = $dispatcher;
$this->filesystem = $filesystem;
$this->assetsFilesystem = $filesystemFactory->disk('flarum-assets');
}
/**
@@ -252,12 +261,7 @@ class ExtensionManager
*/
protected function publishAssets(Extension $extension)
{
if ($extension->hasAssets()) {
$this->filesystem->copyDirectory(
$extension->getPath().'/assets',
$this->paths->public.'/assets/extensions/'.$extension->getId()
);
}
$extension->copyAssetsTo($this->assetsFilesystem);
}
/**
@@ -267,7 +271,7 @@ class ExtensionManager
*/
protected function unpublishAssets(Extension $extension)
{
$this->filesystem->deleteDirectory($this->paths->public.'/assets/extensions/'.$extension->getId());
$this->assetsFilesystem->deleteDirectory('extensions/'.$extension->getId());
}
/**
@@ -279,7 +283,7 @@ class ExtensionManager
*/
public function getAsset(Extension $extension, $path)
{
return $this->paths->public.'/assets/extensions/'.$extension->getId().$path;
return $this->assetsFilesystem->url($extension->getId()."/$path");
}
/**