Better metadata and attachments management
Allows for dot notation, merging and deep-replacing
This commit is contained in:
parent
26df25a5e7
commit
59ac3b9b93
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
use App\Classes\Traits\ManagesMetadata;
|
use App\Classes\Traits\ManagesMetadata;
|
||||||
use Illuminate\Filesystem\FilesystemAdapter;
|
use Illuminate\Filesystem\FilesystemAdapter;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class AttachmentsManager
|
class AttachmentsManager
|
||||||
|
@ -47,10 +48,14 @@ public function addToHistory(string $name, array $data): string
|
||||||
{
|
{
|
||||||
$reference = $this->add($data);
|
$reference = $this->add($data);
|
||||||
|
|
||||||
$this->manager['history'][$name][] = [
|
$this->manager->merge([
|
||||||
'reference' => $reference,
|
'history' => [
|
||||||
'date' => now(),
|
$name => [
|
||||||
];
|
'reference' => $reference,
|
||||||
|
'date' => now(),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
return $reference;
|
return $reference;
|
||||||
}
|
}
|
||||||
|
@ -63,15 +68,6 @@ public function manager(): MetadataManager
|
||||||
return $this->manager;
|
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
|
* Add a single file to the bundle
|
||||||
*/
|
*/
|
||||||
|
@ -79,11 +75,22 @@ public function add(array $data): string
|
||||||
{
|
{
|
||||||
$reference = $this->generateNewReference();
|
$reference = $this->generateNewReference();
|
||||||
|
|
||||||
$filename = $data['filename'];
|
if (!empty($data['contents'])) {
|
||||||
$contents = $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);
|
$fullPath = sprintf('%s/%s', $this->targetForFiles, $filename);
|
||||||
$relativePath = sprintf('%s/%s/%s', $this->attachmentsDir, $this->kind, $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->disk->put($fullPath, $contents);
|
||||||
|
|
||||||
$this->manager['files'][$reference] = $data;
|
$this->manager->set(sprintf('files.%s', $reference), $data);
|
||||||
|
|
||||||
return $reference;
|
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
|
* Return a boolean value indicating if the file actually exists on disk
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -100,17 +100,32 @@ protected function writeToDisk(array $data)
|
||||||
*/
|
*/
|
||||||
public function set($key, $value)
|
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) {
|
$content = $this->content->dot();
|
||||||
$this->content->put($key, $value);
|
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user