1
0

Improved attachments management

This commit is contained in:
Richard Dern 2024-04-17 16:18:39 +02:00
parent e4ddff98f1
commit f17057cc36
4 changed files with 47 additions and 45 deletions

View File

@ -2,24 +2,40 @@
namespace App\Classes; namespace App\Classes;
use App\Classes\Traits\ManagesMetadata;
use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class AttachmentsManager class AttachmentsManager
{ {
/** use ManagesMetadata;
* Original metadata content
*/
private array $originalContent = [];
/** /**
* 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); $reference = $this->add($data);
$this->content['history'][$name][] = [ $this->manager['history'][$name][] = [
'reference' => $reference, 'reference' => $reference,
'date' => now(), 'date' => now(),
]; ];
@ -49,14 +65,14 @@ public function add(array $data): string
unset($data['contents']); unset($data['contents']);
$fullPath = sprintf('%s/%s', $this->root, $filename); $fullPath = sprintf('%s/%s', $this->targetForFiles, $filename);
$relativePath = Str::remove(dirname($this->root), $fullPath); $relativePath = sprintf('%s/%s', $this->kind, $filename);
$data['filename'] = $relativePath; $data['filename'] = $relativePath;
$this->disk->put($fullPath, $contents); $this->disk->put($fullPath, $contents);
$this->content['files'][$reference] = $data; $this->manager['files'][$reference] = $data;
return $reference; return $reference;
} }
@ -66,18 +82,7 @@ public function add(array $data): string
*/ */
public function exists() public function exists()
{ {
return $this->disk->exists($this->root . '.json'); return $this->disk->exists($this->metadataFilePath);
}
/**
* 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();
} }
/** /**
@ -85,17 +90,7 @@ public function isDirty(): bool
*/ */
public function save(): bool public function save(): bool
{ {
if (!$this->isDirty()) { return $this->manager->save();
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;
} }
/** /**

View File

@ -12,6 +12,14 @@ class Bundle
{ {
use ManagesAttachments, ManagesMarkdown, ManagesMetadata; use ManagesAttachments, ManagesMarkdown, ManagesMetadata;
/**
* Return bundle's path
*/
public function getPath(): string
{
return $this->path;
}
public function __construct(protected string $path, protected FilesystemAdapter $disk) public function __construct(protected string $path, protected FilesystemAdapter $disk)
{ {
$this->path = Str::start(Str::finish($this->path, '/'), '/'); $this->path = Str::start(Str::finish($this->path, '/'), '/');

View File

@ -11,15 +11,13 @@ trait ManagesAttachments
/** /**
* Register an attachments manager for specified filename * 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($kind, $this->attachmentsManagers)) {
$this->attachmentsManagers[$kind] = new AttachmentsManager($kind, $this->path, $this->disk);
if (!array_key_exists($root, $this->attachmentsManagers)) {
$this->attachmentsManagers[$root] = new AttachmentsManager($root, $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);
} }
} }

View File

@ -2,6 +2,7 @@
namespace App\Services\BundleCreator\Creators; namespace App\Services\BundleCreator\Creators;
use App\Classes\AttachmentsManager;
use App\Classes\Bundle; use App\Classes\Bundle;
use App\Exceptions\BundleAlreadyExists; use App\Exceptions\BundleAlreadyExists;
use App\Services\Browser; use App\Services\Browser;
@ -43,7 +44,7 @@ public function createBundle(): string
$title = $browser->getTitle(); $title = $browser->getTitle();
$description = $browser->getDescription(); $description = $browser->getDescription();
$ref = $bundle->attachments('images')->addToHistory('screenshot', [ $ref = $bundle->attachments(AttachmentsManager::Images)->addToHistory('screenshot', [
'contents' => $screenshot, 'contents' => $screenshot,
'filename' => 'screenshot.jpg', 'filename' => 'screenshot.jpg',
]); ]);