diff --git a/front/js/svelte/files/imageserver.js b/front/js/svelte/files/imageserver.js index 53313c0..b1c87af 100644 --- a/front/js/svelte/files/imageserver.js +++ b/front/js/svelte/files/imageserver.js @@ -3,11 +3,11 @@ export function imgurl(channel,record) { if (record._file.mime === "image/svg+xml") { return fileurl(channel, record); } - return channel.filesUrl + `/thumbs/${record._file.disk}/${record._file.path}`; + return channel.disks[record._file.disk] + `/thumbs/${record._file.path}`; } export function fileurl(channel, record) { - return channel.filesUrl + `/${record._file.disk}/${record._file.path}`; + return channel.disks[record._file.disk] + `/${record._file.path}`; } export function htmlurl(channel,record, preset) { @@ -18,9 +18,8 @@ export function htmlurl(channel,record, preset) { if (record._file.width > 0) { let presetUrl = url; if (preset) { - presetUrl = channel.filesUrl + `/templates/${preset}/${record._file.disk}/${record._file.path}`; + presetUrl = channel.disks[record._file.disk] + `/templates/${preset}/${record._file.path}`; } - html = `${record._file.path}` } else if (record._file.mime === "image/svg+xml") { html = `${record._file.path}` diff --git a/src/Channel/Channel.php b/src/Channel/Channel.php index 7d51eb0..21dff29 100644 --- a/src/Channel/Channel.php +++ b/src/Channel/Channel.php @@ -4,12 +4,14 @@ namespace Lucent\Channel; use Lucent\Channel\Data\UserCommand; use Lucent\Primitive\Collection; +use Lucent\Schema\FilesSchema; use Lucent\Schema\Schema; final class Channel { public string $lucentUrl; public string $filesUrl; + public array $disks; public string $previewTargetUrl; /** @@ -28,6 +30,7 @@ final class Channel { $this->lucentUrl = $url . "/lucent"; $this->filesUrl = $this->makeFilesUrl(); + $this->disks = $this->getDisksFromSchemas(); $this->previewTargetUrl = $url . "/" . $previewTarget; } @@ -42,4 +45,11 @@ final class Channel } + private function getDisksFromSchemas() + { + return $this->schemas->filter(fn(Schema $schema) => get_class($schema) === FilesSchema::class)->reduce(function (array $carry, Schema $schema) { + $carry[$schema->disk] = config("filesystems.disks." . $schema->disk . ".url"); + return $carry; + }, []); + } } diff --git a/src/File/FileService.php b/src/File/FileService.php index 06e4cb6..676b437 100644 --- a/src/File/FileService.php +++ b/src/File/FileService.php @@ -2,12 +2,12 @@ namespace Lucent\File; -use Exception; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Http\UploadedFile; +use Illuminate\Log\Logger; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; -use Intervention\Image\ImageManagerStatic; +use Intervention\Image\ImageManager; use Lucent\Channel\ChannelService; use Lucent\Database\Database; use Lucent\LucentException; @@ -21,7 +21,9 @@ class FileService { public function __construct( - public ChannelService $channelService + public ChannelService $channelService, + public ImageManager $imageManager, + public Logger $logger ) { } @@ -65,7 +67,6 @@ class FileService isDuplicate: true ); } - $disk = $this->loadDisk($schema); $path = $schema->path . "/" . $filename; $res = $disk->put( @@ -78,7 +79,8 @@ class FileService throw new LucentException("File $filename not uploaded"); } - $this->createThumbnail($disk, $schema->path, $filename, $file); +// $this->createThumbnail($disk, $schema->path, $filename, $file); + $this->createTemplates($disk, $path, $file); list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0]; $recordFile = new RecordFile( @@ -128,29 +130,19 @@ class FileService return $record->id ?? ""; } - private function createThumbnail(Filesystem $disk, string $schemaPath, string $filename, UploadedFile $file): void + private function createTemplates(Filesystem $disk, string $path, UploadedFile $file): void { - $thumbDir = "thumbs/" . $schemaPath . "/"; -// if (!file_exists($thumbDir)) { -// make_dir_r($thumbDir); -// } - try { - ImageManagerStatic::configure(['driver' => 'imagick']); - $image = ImageManagerStatic::make($file); - } catch (Exception $e) { - logger($e->getMessage()); - return; + $originalImage = $this->imageManager->make($disk->get($path)); + foreach (config("lucent.imageFilters") as $preset => $filterClass) { + $image = $originalImage->filter(new $filterClass); + $templateUri = "/templates/" . $preset . "/" . $path; + $disk->put($templateUri, $image->encode('webp', 75)); } - $image->fit(300, 300); - try { - $disk->put($thumbDir . $filename, $image->encode('webp', 75)); -// $image->encode('webp', 75)->save($thumbDir . $filename); - } catch (Exception $e) { - logger($e->getMessage()); - } + $thumbDir = "thumbs/" . $path; + + $image = $originalImage->fit(300, 300); + $disk->put($thumbDir, $image->encode('webp', 75)); } - - } diff --git a/src/File/ImageService.php b/src/File/ImageService.php deleted file mode 100644 index 3b7461b..0000000 --- a/src/File/ImageService.php +++ /dev/null @@ -1,74 +0,0 @@ -notFoundImage; - } - - $originalPath = $record->_file->path; - $templateUri = $this->findTemplate($originalPath, $template); - - if ($templateUri === false) { - $templateUri = $this->createTemplate($originalPath, $template); - } - return $this->channelService->channel->filesUrl . "/" . $templateUri; - } - - private function findTemplate(string $originalPath, string $template): string|false - { - $templateUri = "templates/" . $template . "/" . $originalPath; - $templateFilePath = public_path("storage/" . $templateUri); - - if (file_exists($templateFilePath)) { - return $templateUri; - } - - return false; - } - - private function createTemplate(string $originalPath, string $template): string - { - - try { - $image = $this->imageManager->make( $this->fileService->loadDisk()->get($originalPath)); - } catch (Exception $e) { - $this->logger->error($e->getMessage()); - return $originalPath; - } - - $image = $image->filter(new $this->channelService->channel->imageFilters[$template]); - try { - $templateUri = "/templates/" . $template . "/" . $originalPath; - $this->fileService->loadDisk()->put($templateUri, $image->encode('webp', 75)); - } catch (Exception $e) { - $this->logger->error($e->getMessage()); - return $this->notFoundImage; - } - - return $templateUri; - } - -} diff --git a/src/Http/Controller/FileController.php b/src/Http/Controller/FileController.php index 6fe4ed3..339defc 100644 --- a/src/Http/Controller/FileController.php +++ b/src/Http/Controller/FileController.php @@ -23,25 +23,13 @@ class FileController extends Controller private readonly ChannelService $channelService, private readonly RecordService $recordService, private readonly FileService $fileService, - private readonly ImageService $imageService, private readonly Query $query ) { } - public function template(Request $request) - { - $template = $request->segment(3); - $templatePath = $request->route("any"); - $filePath = str_replace($template."/", "", $templatePath); - $record = $this->query->filter(["_file.path" => $filePath])->run()->records->first(); - $schema = $this->channelService->getSchema($record->schema); - $this->imageService->file($record,$template); - $disk = $this->fileService->loadDisk($schema->disk); - return response()->file($disk->path("templates/".$templatePath)); - } - public function original(Request $request, string $disk) + public function fromDisk(Request $request, string $disk) { $imagePath = $request->route("any"); $disk = $this->fileService->loadDisk($disk); diff --git a/src/Http/web.php b/src/Http/web.php index dceec26..0da82e4 100644 --- a/src/Http/web.php +++ b/src/Http/web.php @@ -14,9 +14,8 @@ use Lucent\Http\Controller\SetupController; Route::get('/lucent/setup', [SetupController::class, 'setup']); -Route::get('/storage/templates/{any}', [FileController::class, 'template'])->where('any', '.*'); -Route::get('/lfs-{disk}/thumbs/{any}', [FileController::class, 'thumb'])->where('any', '.*'); -Route::get('/lfs-{disk}/{disk}/{any}', [FileController::class, 'original'])->where('any', '.*'); +Route::get('/lfs-{disk}/{any}', [FileController::class, 'fromDisk'])->where('any', '.*'); + Route::group([ 'middleware' => ['web'], diff --git a/src/LucentServiceProvider.php b/src/LucentServiceProvider.php index cc00cd3..c98461e 100644 --- a/src/LucentServiceProvider.php +++ b/src/LucentServiceProvider.php @@ -83,7 +83,6 @@ class LucentServiceProvider extends ServiceProvider } View::share('manifest', $manifest); - View::share('image', app()->make(ImageService::class)); View::share('file', app()->make(FileService::class)); Blade::anonymousComponentPath(__DIR__ . '../front/views/components', "lucent");