diff --git a/app/Classes/AttachmentsManager.php b/app/Classes/AttachmentsManager.php index a3f0d6d..a3cff24 100644 --- a/app/Classes/AttachmentsManager.php +++ b/app/Classes/AttachmentsManager.php @@ -4,6 +4,7 @@ use App\Classes\Traits\ManagesMetadata; use Illuminate\Filesystem\FilesystemAdapter; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Str; class AttachmentsManager @@ -47,10 +48,14 @@ public function addToHistory(string $name, array $data): string { $reference = $this->add($data); - $this->manager['history'][$name][] = [ - 'reference' => $reference, - 'date' => now(), - ]; + $this->manager->merge([ + 'history' => [ + $name => [ + 'reference' => $reference, + 'date' => now(), + ], + ], + ]); return $reference; } @@ -63,15 +68,6 @@ public function manager(): MetadataManager return $this->manager; } - /** - * Add or replace attachment with specified reference. Will replace existing - * data - */ - public function upsert(string $reference, array $data) - { - $this->manager['files'][$reference] = $data; - } - /** * Add a single file to the bundle */ @@ -79,11 +75,22 @@ public function add(array $data): string { $reference = $this->generateNewReference(); - $filename = $data['filename']; - $contents = $data['contents']; + if (!empty($data['contents'])) { + // Adding from an image resource of which $data['contents'] is a + // representation + $contents = $data['contents']; - unset($data['contents']); + unset($data['contents']); + } elseif (!empty($data['url'])) { + // Adding from a URL which implies downloading the resource + $contents = Http::throw()->get($data['url'])->body(); + $data['filename'] = basename($data['url']); + + unset($data['url']); + } + + $filename = $this->getFormatedFilename($data['filename']); $fullPath = sprintf('%s/%s', $this->targetForFiles, $filename); $relativePath = sprintf('%s/%s/%s', $this->attachmentsDir, $this->kind, $filename); @@ -91,11 +98,20 @@ public function add(array $data): string $this->disk->put($fullPath, $contents); - $this->manager['files'][$reference] = $data; + $this->manager->set(sprintf('files.%s', $reference), $data); return $reference; } + private function getFormatedFilename(string $path): string + { + $filename = pathinfo($path, PATHINFO_FILENAME); + $extension = pathinfo($path, PATHINFO_EXTENSION); + $slug = Str::slug($filename); + + return sprintf('%s.%s', $slug, $extension); + } + /** * Return a boolean value indicating if the file actually exists on disk */ diff --git a/app/Classes/MetadataManager.php b/app/Classes/MetadataManager.php index 2ab28f4..fbed3d0 100644 --- a/app/Classes/MetadataManager.php +++ b/app/Classes/MetadataManager.php @@ -100,17 +100,32 @@ protected function writeToDisk(array $data) */ public function set($key, $value) { - $this->setMany(collect([$key => $value])->undot()); + $content = $this->content->dot(); + $newValue = collect([$key => $value])->dot(); + $newContent = collect($content)->replaceRecursive($newValue)->undot(); + + $this->content = $newContent; } /** - * Sets multiple metadata at once. + * Set many values at once */ - public function setMany($array) + public function setMany(array $array) { - foreach ($array as $key => $value) { - $this->content->put($key, $value); - } + $content = $this->content->dot(); + $newValue = collect($array)->dot(); + $newContent = collect($content)->replaceRecursive($newValue)->undot(); + + $this->content = $newContent; + } + + public function merge(array $array) + { + $content = $this->content->dot(); + $newValue = collect($array)->dot(); + $newContent = collect($content)->mergeRecursive($newValue)->undot(); + + $this->content = $newContent; } /**