Improved attachments management
This commit is contained in:
parent
e4ddff98f1
commit
f17057cc36
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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, '/'), '/');
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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',
|
||||
]);
|
||||
|
|
Loading…
Reference in New Issue
Block a user