diff --git a/app/Classes/AttachmentsManager.php b/app/Classes/AttachmentsManager.php new file mode 100644 index 0000000..2879ca3 --- /dev/null +++ b/app/Classes/AttachmentsManager.php @@ -0,0 +1,108 @@ +add($data); + + $this->content['history'][$name][] = [ + 'reference' => $reference, + 'date' => now(), + ]; + + return $reference; + } + + /** + * Add a single file to the bundle + */ + public function add(array $data): string + { + $reference = $this->generateNewReference(); + + $filename = $data['filename']; + $contents = $data['contents']; + + unset($data['contents']); + + $fullPath = sprintf('%s/%s', $this->root, $filename); + $relativePath = Str::remove(dirname($this->root), $fullPath); + + $data['filename'] = $relativePath; + + $this->disk->put($fullPath, $contents); + + $this->content['files'][$reference] = $data; + + return $reference; + } + + /** + * Return a boolean value indicating if the file actually exists on disk + */ + 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(); + } + + /** + * Store file on disk + */ + 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; + } + + /** + * Generate new, random reference for a file + */ + private function generateNewReference(): string + { + return Str::random(6); + } +} diff --git a/app/Classes/Bundle.php b/app/Classes/Bundle.php index 437bf22..fd655e2 100644 --- a/app/Classes/Bundle.php +++ b/app/Classes/Bundle.php @@ -2,6 +2,7 @@ namespace App\Classes; +use App\Classes\Traits\ManagesAttachments; use App\Classes\Traits\ManagesMarkdown; use App\Classes\Traits\ManagesMetadata; use Illuminate\Filesystem\FilesystemAdapter; @@ -9,7 +10,7 @@ class Bundle { - use ManagesMarkdown, ManagesMetadata; + use ManagesAttachments, ManagesMarkdown, ManagesMetadata; public function __construct(protected string $path, protected FilesystemAdapter $disk) { @@ -21,6 +22,7 @@ public function __construct(protected string $path, protected FilesystemAdapter */ public function save() { + $this->saveAttachments(); $this->saveMetadata(); $this->saveMarkdown(); } diff --git a/app/Classes/Traits/ManagesAttachments.php b/app/Classes/Traits/ManagesAttachments.php new file mode 100644 index 0000000..c0bc38e --- /dev/null +++ b/app/Classes/Traits/ManagesAttachments.php @@ -0,0 +1,42 @@ +getFilenameInBundle($root); + + if (!array_key_exists($root, $this->attachmentsManagers)) { + $this->attachmentsManagers[$root] = new AttachmentsManager($root, $this->disk); + } + + return $this->attachmentsManagers[$root]; + } + + /** + * Save all attachments files that needs to be + */ + private function saveAttachments() + { + foreach ($this->attachmentsManagers as $manager) { + $manager->save(); + } + } + + /** + * Return an instance of attachments manager for specified filename + */ + public function attachments(string $root): AttachmentsManager + { + return $this->registerAttachmentsManager($root); + } +}