From 986d3420cbbb72c11dc47e8db781653be64b4183 Mon Sep 17 00:00:00 2001 From: lexx Date: Wed, 9 Oct 2024 23:36:24 +0300 Subject: [PATCH] file paths --- front/js/svelte/files/imageserver.js | 6 ++++-- src/Commands/RebuildThumbnails.php | 5 ++++- src/File/FileService.php | 12 +++++++----- src/macros.php | 4 ++-- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/front/js/svelte/files/imageserver.js b/front/js/svelte/files/imageserver.js index ec04eb1..89c5171 100644 --- a/front/js/svelte/files/imageserver.js +++ b/front/js/svelte/files/imageserver.js @@ -2,7 +2,8 @@ export function imgurl(channel, record) { if (record._file.mime === "image/svg+xml") { return fileurl(channel, record); } - return channel.disks[record._file.disk] + `/thumbs/${record._file.path}`; + const pathAr = record._file.path.split("/"); + return channel.disks[record._file.disk] + `/${pathAr[0]}/thumbs/${pathAr[1]}`; } export function fileurl(channel, record) { @@ -17,7 +18,8 @@ export function htmlurl(channel, record, preset) { if (record._file.width > 0) { let presetUrl = url; if (preset) { - presetUrl = channel.disks[record._file.disk] + `/templates/${preset}/${record._file.path}`; + const pathAr = record._file.path.split("/"); + presetUrl = channel.disks[record._file.disk] + `/${pathAr[0]}/templates/${preset}/${pathAr[1]}`; } html = `${record._file.path}` } else if (record._file.mime === "image/svg+xml") { diff --git a/src/Commands/RebuildThumbnails.php b/src/Commands/RebuildThumbnails.php index 59881fb..a561183 100644 --- a/src/Commands/RebuildThumbnails.php +++ b/src/Commands/RebuildThumbnails.php @@ -44,8 +44,11 @@ class RebuildThumbnails extends Command $this->info("Rebuilding thumbnails for ". $schema->name); $records = $this->query->filter(["schema" => $schema->name])->run()->records; $disk = $this->fileService->loadDisk($schema->disk); + $disk->deleteDirectory($schema->path."/templates"); + $disk->deleteDirectory($schema->path."/thumbs"); foreach ($records as $record) { - $this->fileService->createTemplates($disk, $record->_file->path); + $filename = substr($record->_file->path, strlen($schema->path) + 1 , strlen($record->_file->path)); + $this->fileService->createTemplates($disk, $schema->path,$filename); } } diff --git a/src/File/FileService.php b/src/File/FileService.php index 9f22825..8c798e4 100644 --- a/src/File/FileService.php +++ b/src/File/FileService.php @@ -16,6 +16,7 @@ use Lucent\Database\Database; use Lucent\LucentException; use Lucent\Record\FileData as RecordFile; use Lucent\Record\QueryRecord; +use Lucent\Record\Record; use Lucent\Schema\FilesSchema; use Lucent\Schema\Schema; use Spatie\ImageOptimizer\OptimizerChainFactory; @@ -83,7 +84,7 @@ class FileService } if ($this->isImage($mimetype)) { - $this->createTemplates($disk, $path); + $this->createTemplates($disk, $schema->path, $filename); } list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0]; @@ -136,10 +137,11 @@ class FileService return $record->id ?? ""; } - public function createTemplates(Filesystem $disk, string $path): void + + public function createTemplates(Filesystem $disk, string $path, string $filename): void { try { - $originalImage = $this->imageManager->read($disk->get($path)); + $originalImage = $this->imageManager->read($disk->get($path . "/" . $filename)); } catch (Exception $exception) { $this->logger->error($exception->getMessage()); return; @@ -156,12 +158,12 @@ class FileService ); $image = $originalImage->modify(new $filterClass); - $templateUri = "/templates/" . (new $filterClass)->name . "/" . $path; + $templateUri = "$path/templates/" . (new $filterClass)->name . "/" . $filename; $disk->put($templateUri, $image->encode(new WebpEncoder(75))); }; - $thumbDir = "thumbs/" . $path; + $thumbDir = "$path/thumbs/" . $filename; $image = $originalImage->cover(300, 300); $disk->put($thumbDir, $image->encode(new WebpEncoder(quality: 75))); diff --git a/src/macros.php b/src/macros.php index 0931762..e510110 100644 --- a/src/macros.php +++ b/src/macros.php @@ -57,8 +57,8 @@ if (!function_exists('lucent_file')) { if (!function_exists('lucent_image')) { function lucent_image(\Lucent\Record\QueryRecord $record, string $template): string { - $path = $record->_file->path; - return app()->make(\Lucent\Channel\ChannelService::class)->channel->disks[$record->_file->disk] . "/templates/$template/$path"; + $filenameAr = explode("/",$record->_file->path); + return app()->make(\Lucent\Channel\ChannelService::class)->channel->disks[$record->_file->disk] ."/". $filenameAr[0]. "/templates/$template/". $filenameAr[1]; } }