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

Asset Publish Command #2731

Merged
merged 12 commits into from
Apr 19, 2021
13 changes: 7 additions & 6 deletions src/Api/Controller/DeleteFaviconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Laminas\Diactoros\Response\EmptyResponse;
use League\Flysystem\FilesystemInterface;
use Psr\Http\Message\ServerRequestInterface;

class DeleteFaviconController extends AbstractDeleteController
Expand All @@ -22,18 +23,18 @@ class DeleteFaviconController extends AbstractDeleteController
protected $settings;

/**
* @var FilesystemInterface
* @var Filesystem
*/
protected $uploadDir;

/**
* @param SettingsRepositoryInterface $settings
* @param FilesystemInterface $uploadDir
* @param Factory $filesystemFactory
*/
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
public function __construct(SettingsRepositoryInterface $settings, Factory $filesystemFactory)
{
$this->settings = $settings;
$this->uploadDir = $uploadDir;
$this->uploadDir = $filesystemFactory->disk('flarum-assets');
}

/**
Expand All @@ -47,7 +48,7 @@ protected function delete(ServerRequestInterface $request)

$this->settings->set('favicon_path', null);

if ($this->uploadDir->has($path)) {
if ($this->uploadDir->exists($path)) {
$this->uploadDir->delete($path);
}

Expand Down
13 changes: 7 additions & 6 deletions src/Api/Controller/DeleteLogoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Laminas\Diactoros\Response\EmptyResponse;
use League\Flysystem\FilesystemInterface;
use Psr\Http\Message\ServerRequestInterface;

class DeleteLogoController extends AbstractDeleteController
Expand All @@ -22,18 +23,18 @@ class DeleteLogoController extends AbstractDeleteController
protected $settings;

/**
* @var FilesystemInterface
* @var Filesystem
*/
protected $uploadDir;

/**
* @param SettingsRepositoryInterface $settings
* @param FilesystemInterface $uploadDir
* @param Factory $filesystemFactory
*/
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
public function __construct(SettingsRepositoryInterface $settings, Factory $filesystemFactory)
{
$this->settings = $settings;
$this->uploadDir = $uploadDir;
$this->uploadDir = $filesystemFactory->disk('flarum-assets');
}

/**
Expand All @@ -47,7 +48,7 @@ protected function delete(ServerRequestInterface $request)

$this->settings->set('logo_path', null);

if ($this->uploadDir->has($path)) {
if ($this->uploadDir->exists($path)) {
$this->uploadDir->delete($path);
}

Expand Down
13 changes: 7 additions & 6 deletions src/Api/Controller/UploadImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
namespace Flarum\Api\Controller;

use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Intervention\Image\Image;
use League\Flysystem\FilesystemInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use Tobscure\JsonApi\Document;
Expand All @@ -26,7 +27,7 @@ abstract class UploadImageController extends ShowForumController
protected $settings;

/**
* @var FilesystemInterface
* @var Filesystem
*/
protected $uploadDir;

Expand All @@ -47,12 +48,12 @@ abstract class UploadImageController extends ShowForumController

/**
* @param SettingsRepositoryInterface $settings
* @param FilesystemInterface $uploadDir
* @param Factory $filesystemFactory
*/
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
public function __construct(SettingsRepositoryInterface $settings, Factory $filesystemFactory)
{
$this->settings = $settings;
$this->uploadDir = $uploadDir;
$this->uploadDir = $filesystemFactory->disk('flarum-assets');
}

/**
Expand All @@ -72,7 +73,7 @@ public function data(ServerRequestInterface $request, Document $document)

$uploadName = $this->filenamePrefix.'-'.Str::lower(Str::random(8)).'.'.$this->fileExtension;

$this->uploadDir->write($uploadName, $encodedImage);
$this->uploadDir->put($uploadName, $encodedImage);

$this->settings->set($this->filePathSettingKey, $uploadName);

Expand Down
20 changes: 17 additions & 3 deletions src/Api/Serializer/ForumSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Flarum\Foundation\Config;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Contracts\Filesystem\Factory;

class ForumSerializer extends AbstractSerializer
{
Expand All @@ -36,14 +38,21 @@ class ForumSerializer extends AbstractSerializer
*/
protected $url;

/**
* @var Cloud
*/
protected $assetsFilesystem;

/**
* @param Config $config
* @param Factory $filesystemFactory
* @param SettingsRepositoryInterface $settings
* @param UrlGenerator $url
*/
public function __construct(Config $config, SettingsRepositoryInterface $settings, UrlGenerator $url)
public function __construct(Config $config, Factory $filesystemFactory, SettingsRepositoryInterface $settings, UrlGenerator $url)
{
$this->config = $config;
$this->assetsFilesystem = $filesystemFactory->disk('flarum-assets');
$this->settings = $settings;
$this->url = $url;
}
Expand Down Expand Up @@ -107,7 +116,7 @@ protected function getLogoUrl()
{
$logoPath = $this->settings->get('logo_path');

return $logoPath ? $this->url->to('forum')->path('assets/'.$logoPath) : null;
return $logoPath ? $this->getAssetUrl($logoPath) : null;
}

/**
Expand All @@ -117,6 +126,11 @@ protected function getFaviconUrl()
{
$faviconPath = $this->settings->get('favicon_path');

return $faviconPath ? $this->url->to('forum')->path('assets/'.$faviconPath) : null;
return $faviconPath ? $this->getAssetUrl($faviconPath) : null;
}

public function getAssetUrl($assetPath): string
{
return $this->assetsFilesystem->url($assetPath);
}
}
2 changes: 2 additions & 0 deletions src/Console/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Flarum\Database\Console\MigrateCommand;
use Flarum\Database\Console\ResetCommand;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Console\AssetsPublishCommand;
use Flarum\Foundation\Console\CacheClearCommand;
use Flarum\Foundation\Console\InfoCommand;
use Illuminate\Console\Scheduling\Schedule as LaravelSchedule;
Expand All @@ -37,6 +38,7 @@ public function register()

$this->container->singleton('flarum.console.commands', function () {
return [
AssetsPublishCommand::class,
CacheClearCommand::class,
InfoCommand::class,
MigrateCommand::class,
Expand Down
7 changes: 0 additions & 7 deletions src/Database/Console/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,5 @@ public function upgrade()
}

$this->container->make(SettingsRepositoryInterface::class)->set('version', Application::VERSION);

$this->info('Publishing assets...');

$this->container->make('files')->copyDirectory(
$this->paths->vendor.'/components/font-awesome/webfonts',
$this->paths->public.'/assets/fonts'
);
}
}
20 changes: 7 additions & 13 deletions src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
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
Expand Down Expand Up @@ -428,16 +425,13 @@ public function copyAssetsTo(FilesystemInterface $target)
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));
}
}

Expand Down
22 changes: 13 additions & 9 deletions src/Extension/ExtensionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
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;
Expand Down Expand Up @@ -48,6 +50,11 @@ class ExtensionManager
*/
protected $filesystem;

/**
* @var FilesystemInterface
*/
protected $assetsFilesystem;

/**
* @var Collection|null
*/
Expand All @@ -59,14 +66,16 @@ public function __construct(
Container $container,
Migrator $migrator,
Dispatcher $dispatcher,
Filesystem $filesystem
Filesystem $filesystem,
Factory $filesystemFactory
) {
$this->config = $config;
$this->paths = $paths;
$this->container = $container;
$this->migrator = $migrator;
$this->dispatcher = $dispatcher;
$this->filesystem = $filesystem;
$this->assetsFilesystem = $filesystemFactory->disk('flarum-assets');
}

/**
Expand Down Expand Up @@ -252,12 +261,7 @@ public function uninstall($name)
*/
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);
}

/**
Expand All @@ -267,7 +271,7 @@ protected function publishAssets(Extension $extension)
*/
protected function unpublishAssets(Extension $extension)
{
$this->filesystem->deleteDirectory($this->paths->public.'/assets/extensions/'.$extension->getId());
$this->assetsFilesystem->deleteDirectory('extensions/'.$extension->getId());
}

/**
Expand All @@ -279,7 +283,7 @@ protected function unpublishAssets(Extension $extension)
*/
public function getAsset(Extension $extension, $path)
{
return $this->paths->public.'/assets/extensions/'.$extension->getId().$path;
return $this->assetsFilesystem->url($extension->getId()."/$path");
}

/**
Expand Down
Loading