From f17057cc36bd6621b1092eedbe8c9dbb2196baf0 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Wed, 17 Apr 2024 16:18:39 +0200 Subject: [PATCH] Improved attachments management --- app/Classes/AttachmentsManager.php | 65 +++++++++---------- app/Classes/Bundle.php | 8 +++ app/Classes/Traits/ManagesAttachments.php | 16 ++--- .../Creators/LinkBundleCreator.php | 3 +- 4 files changed, 47 insertions(+), 45 deletions(-) diff --git a/app/Classes/AttachmentsManager.php b/app/Classes/AttachmentsManager.php index 2879ca3..a095177 100644 --- a/app/Classes/AttachmentsManager.php +++ b/app/Classes/AttachmentsManager.php @@ -2,24 +2,40 @@ namespace App\Classes; +use App\Classes\Traits\ManagesMetadata; use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Support\Str; class AttachmentsManager { - /** - * Original metadata content - */ - private array $originalContent = []; + use ManagesMetadata; /** - * Current metadata content + * Manages images */ - private array $content = []; + const Images = 'images'; - public function __construct(protected string $root, protected FilesystemAdapter $disk) + /** + * Manages sounds + */ + const Sounds = 'sounds'; + + /** + * Manages videos + */ + const Videos = 'videos'; + + private string $targetForFiles; + + private string $metadataFilePath; + + private MetadataManager $manager; + + public function __construct(protected string $kind, protected string $root, protected FilesystemAdapter $disk) { - + $this->metadataFilePath = sprintf('%s%s.json', $root, $kind); + $this->targetForFiles = sprintf('%s%s', $root, $kind); + $this->manager = new MetadataManager($this->metadataFilePath, $disk); } /** @@ -29,7 +45,7 @@ public function addToHistory(string $name, array $data): string { $reference = $this->add($data); - $this->content['history'][$name][] = [ + $this->manager['history'][$name][] = [ 'reference' => $reference, 'date' => now(), ]; @@ -49,14 +65,14 @@ public function add(array $data): string unset($data['contents']); - $fullPath = sprintf('%s/%s', $this->root, $filename); - $relativePath = Str::remove(dirname($this->root), $fullPath); + $fullPath = sprintf('%s/%s', $this->targetForFiles, $filename); + $relativePath = sprintf('%s/%s', $this->kind, $filename); $data['filename'] = $relativePath; $this->disk->put($fullPath, $contents); - $this->content['files'][$reference] = $data; + $this->manager['files'][$reference] = $data; return $reference; } @@ -66,18 +82,7 @@ public function add(array $data): string */ public function exists() { - return $this->disk->exists($this->root . '.json'); - } - - /** - * Return a boolean indicating if the file has changed since it was loaded - */ - public function isDirty(): bool - { - $original = collect($this->originalContent); - $current = collect($this->content); - - return !$original->diffAssoc($current)->isEmpty() || !$current->diffAssoc($original)->isEmpty(); + return $this->disk->exists($this->metadataFilePath); } /** @@ -85,17 +90,7 @@ public function isDirty(): bool */ public function save(): bool { - if (!$this->isDirty()) { - return false; - } - - $json = json_encode($this->content, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); - - $this->disk->put($this->root . '.json', $json); - - $this->originalContent = $this->content; - - return true; + return $this->manager->save(); } /** diff --git a/app/Classes/Bundle.php b/app/Classes/Bundle.php index fd655e2..cf026a0 100644 --- a/app/Classes/Bundle.php +++ b/app/Classes/Bundle.php @@ -12,6 +12,14 @@ class Bundle { use ManagesAttachments, ManagesMarkdown, ManagesMetadata; + /** + * Return bundle's path + */ + public function getPath(): string + { + return $this->path; + } + public function __construct(protected string $path, protected FilesystemAdapter $disk) { $this->path = Str::start(Str::finish($this->path, '/'), '/'); diff --git a/app/Classes/Traits/ManagesAttachments.php b/app/Classes/Traits/ManagesAttachments.php index c0bc38e..e598a53 100644 --- a/app/Classes/Traits/ManagesAttachments.php +++ b/app/Classes/Traits/ManagesAttachments.php @@ -11,15 +11,13 @@ trait ManagesAttachments /** * Register an attachments manager for specified filename */ - private function registerAttachmentsManager(string $root): AttachmentsManager + private function registerAttachmentsManager(string $kind): AttachmentsManager { - $root = $this->getFilenameInBundle($root); - - if (!array_key_exists($root, $this->attachmentsManagers)) { - $this->attachmentsManagers[$root] = new AttachmentsManager($root, $this->disk); + if (!array_key_exists($kind, $this->attachmentsManagers)) { + $this->attachmentsManagers[$kind] = new AttachmentsManager($kind, $this->path, $this->disk); } - return $this->attachmentsManagers[$root]; + return $this->attachmentsManagers[$kind]; } /** @@ -33,10 +31,10 @@ private function saveAttachments() } /** - * Return an instance of attachments manager for specified filename + * Return an instance of attachments manager for specified kind of files */ - public function attachments(string $root): AttachmentsManager + public function attachments(string $kind): AttachmentsManager { - return $this->registerAttachmentsManager($root); + return $this->registerAttachmentsManager($kind); } } diff --git a/app/Services/BundleCreator/Creators/LinkBundleCreator.php b/app/Services/BundleCreator/Creators/LinkBundleCreator.php index b0e513b..5b9347b 100644 --- a/app/Services/BundleCreator/Creators/LinkBundleCreator.php +++ b/app/Services/BundleCreator/Creators/LinkBundleCreator.php @@ -2,6 +2,7 @@ namespace App\Services\BundleCreator\Creators; +use App\Classes\AttachmentsManager; use App\Classes\Bundle; use App\Exceptions\BundleAlreadyExists; use App\Services\Browser; @@ -43,7 +44,7 @@ public function createBundle(): string $title = $browser->getTitle(); $description = $browser->getDescription(); - $ref = $bundle->attachments('images')->addToHistory('screenshot', [ + $ref = $bundle->attachments(AttachmentsManager::Images)->addToHistory('screenshot', [ 'contents' => $screenshot, 'filename' => 'screenshot.jpg', ]);