From a737c2d571fc3a54820a0164f25cb9041f1b8d1d Mon Sep 17 00:00:00 2001 From: lexx Date: Fri, 23 Aug 2024 20:58:45 +0300 Subject: [PATCH] configurable disks --- src/Channel/Channel.php | 14 +++++++++-- src/Channel/ChannelService.php | 3 ++- ...2023_10_22_152122_generatedIndexColumn.php | 2 +- src/File/FileService.php | 21 ++++++++-------- src/File/ImageService.php | 25 ++++--------------- 5 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/Channel/Channel.php b/src/Channel/Channel.php index 6b75bba..05da1af 100644 --- a/src/Channel/Channel.php +++ b/src/Channel/Channel.php @@ -25,8 +25,18 @@ final class Channel ) { $this->lucentUrl = $url . "/lucent"; - $this->filesUrl = $url . "/storage"; - $this->previewTargetUrl = $url . "/". $previewTarget; + $this->filesUrl = $this->makeFilesUrl(); + $this->previewTargetUrl = $url . "/" . $previewTarget; + } + + + private function makeFilesUrl(): string + { + return match (config("filesystems.disks.lucent.driver")) { + "s3" => config("filesystems.disks.lucent.endpoint") . "/" . config("filesystems.disks.lucent.bucket"), + "local" => $this->url . "/storage" . config("filesystems.disks.lucent.endpoint"), + }; + } } diff --git a/src/Channel/ChannelService.php b/src/Channel/ChannelService.php index 12c397b..1460195 100644 --- a/src/Channel/ChannelService.php +++ b/src/Channel/ChannelService.php @@ -3,6 +3,7 @@ namespace Lucent\Channel; +use Lucent\File\FileService; use Lucent\Primitive\Collection; use Lucent\Schema\Schema; use Lucent\Schema\SchemaService; @@ -13,7 +14,7 @@ final class ChannelService public Channel $channel; private function __construct( - public SchemaService $schemaService + public SchemaService $schemaService, ) { diff --git a/src/Database/migrations/2023_10_22_152122_generatedIndexColumn.php b/src/Database/migrations/2023_10_22_152122_generatedIndexColumn.php index 6c5c80c..f3a5a48 100644 --- a/src/Database/migrations/2023_10_22_152122_generatedIndexColumn.php +++ b/src/Database/migrations/2023_10_22_152122_generatedIndexColumn.php @@ -26,7 +26,7 @@ return new class extends Migration { * * @return void */ - public function down + public function down() { Schema::table('records', function (Blueprint $table) { $table->dropColumn('search'); diff --git a/src/File/FileService.php b/src/File/FileService.php index a93160f..f53da2a 100644 --- a/src/File/FileService.php +++ b/src/File/FileService.php @@ -14,7 +14,6 @@ use Lucent\LucentException; use Lucent\Record\FileData as RecordFile; use Lucent\Record\QueryRecord; use Lucent\Schema\FilesSchema; -use Lucent\Schema\Schema; use Spatie\ImageOptimizer\OptimizerChainFactory; class FileService @@ -28,7 +27,7 @@ class FileService public function getPath(QueryRecord $file): string { - return $this->channelService->channel->url . "/storage/" . $file->_file->path; + return $this->channelService->channel->filesUrl . "/" . $file->_file->path; } public function createFromUrl(FilesSchema $schema, string $url): FileUploadResult @@ -111,15 +110,16 @@ class FileService public function loadDisk(): Filesystem { + return Storage::disk('lucent'); return Storage::build([ - 'driver' => 'local', + 'driver' => 'lucent', // 'key' => config("filesystems.disks.s3.key"), // 'secret' => config("filesystems.disks.s3.secret"), // 'region' => config("filesystems.disks.s3.region"), // 'bucket' => config("filesystems.disks.s3.bucket"), // // 'url' => $schema->objectStorageUrl, // 'endpoint' => $schema->objectStorageEndpoint, - 'use_path_style_endpoint' => false, +// 'use_path_style_endpoint' => false, 'visibility' => 'public', // now managed by aws policy 'root' => storage_path('app/public'), 'throw' => true, @@ -140,12 +140,10 @@ class FileService private function createThumbnail(Filesystem $disk, string $schemaPath, string $filename, UploadedFile $file): void { - - - $thumbDir = storage_path("app/public/thumbs/" . $schemaPath . "/"); - if (!file_exists($thumbDir)) { - make_dir_r($thumbDir); - } + $thumbDir = "thumbs/" . $schemaPath . "/"; +// if (!file_exists($thumbDir)) { +// make_dir_r($thumbDir); +// } try { ImageManagerStatic::configure(['driver' => 'imagick']); @@ -157,7 +155,8 @@ class FileService $image->fit(300, 300); try { - $image->encode('webp', 75)->save($thumbDir . $filename); + $this->loadDisk()->put($thumbDir . $filename, $image->encode('webp', 75)); +// $image->encode('webp', 75)->save($thumbDir . $filename); } catch (Exception $e) { logger($e->getMessage()); } diff --git a/src/File/ImageService.php b/src/File/ImageService.php index bcc95f9..3b7461b 100644 --- a/src/File/ImageService.php +++ b/src/File/ImageService.php @@ -15,6 +15,7 @@ class ImageService public function __construct( public ImageManager $imageManager, + public FileService $fileService, public ChannelService $channelService, public Logger $logger ) @@ -50,28 +51,18 @@ class ImageService private function createTemplate(string $originalPath, string $template): string { - $originalFilePath = public_path("storage/" . $originalPath); - $templateUri = "/templates/" . $template . "/" . $originalPath; - $templateFilePath = public_path("storage/" . $templateUri); - if (!file_exists($originalFilePath)) { - return $this->notFoundImage; - } - - if (!file_exists(pathinfo($templateFilePath, PATHINFO_DIRNAME))) { - $this->make_dir(pathinfo($templateFilePath, PATHINFO_DIRNAME)); - } try { - $image = $this->imageManager->make($originalFilePath); + $image = $this->imageManager->make( $this->fileService->loadDisk()->get($originalPath)); } catch (Exception $e) { $this->logger->error($e->getMessage()); - return $this->notFoundImage; + return $originalPath; } $image = $image->filter(new $this->channelService->channel->imageFilters[$template]); try { - $image = $this->imageManager->make((string)$image->encode('webp', 75)); - $image->save($templateFilePath); + $templateUri = "/templates/" . $template . "/" . $originalPath; + $this->fileService->loadDisk()->put($templateUri, $image->encode('webp', 75)); } catch (Exception $e) { $this->logger->error($e->getMessage()); return $this->notFoundImage; @@ -80,10 +71,4 @@ class ImageService return $templateUri; } - private function make_dir(string $path): void - { - is_dir($path) || mkdir($path, 0777, true); - } - - }