Basic attachments management
This commit is contained in:
parent
b115d165c3
commit
2100fec73f
108
app/Classes/AttachmentsManager.php
Normal file
108
app/Classes/AttachmentsManager.php
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Classes;
|
||||||
|
|
||||||
|
use Illuminate\Filesystem\FilesystemAdapter;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
class AttachmentsManager
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Original metadata content
|
||||||
|
*/
|
||||||
|
private array $originalContent = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current metadata content
|
||||||
|
*/
|
||||||
|
private array $content = [];
|
||||||
|
|
||||||
|
public function __construct(protected string $root, protected FilesystemAdapter $disk)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a file to a named history and return attachment's reference
|
||||||
|
*/
|
||||||
|
public function addToHistory(string $name, array $data): string
|
||||||
|
{
|
||||||
|
$reference = $this->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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Classes;
|
namespace App\Classes;
|
||||||
|
|
||||||
|
use App\Classes\Traits\ManagesAttachments;
|
||||||
use App\Classes\Traits\ManagesMarkdown;
|
use App\Classes\Traits\ManagesMarkdown;
|
||||||
use App\Classes\Traits\ManagesMetadata;
|
use App\Classes\Traits\ManagesMetadata;
|
||||||
use Illuminate\Filesystem\FilesystemAdapter;
|
use Illuminate\Filesystem\FilesystemAdapter;
|
||||||
|
@ -9,7 +10,7 @@
|
||||||
|
|
||||||
class Bundle
|
class Bundle
|
||||||
{
|
{
|
||||||
use ManagesMarkdown, ManagesMetadata;
|
use ManagesAttachments, ManagesMarkdown, ManagesMetadata;
|
||||||
|
|
||||||
public function __construct(protected string $path, protected FilesystemAdapter $disk)
|
public function __construct(protected string $path, protected FilesystemAdapter $disk)
|
||||||
{
|
{
|
||||||
|
@ -21,6 +22,7 @@ public function __construct(protected string $path, protected FilesystemAdapter
|
||||||
*/
|
*/
|
||||||
public function save()
|
public function save()
|
||||||
{
|
{
|
||||||
|
$this->saveAttachments();
|
||||||
$this->saveMetadata();
|
$this->saveMetadata();
|
||||||
$this->saveMarkdown();
|
$this->saveMarkdown();
|
||||||
}
|
}
|
||||||
|
|
42
app/Classes/Traits/ManagesAttachments.php
Normal file
42
app/Classes/Traits/ManagesAttachments.php
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Classes\Traits;
|
||||||
|
|
||||||
|
use App\Classes\AttachmentsManager;
|
||||||
|
|
||||||
|
trait ManagesAttachments
|
||||||
|
{
|
||||||
|
private array $attachmentsManagers = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register an attachments manager for specified filename
|
||||||
|
*/
|
||||||
|
private function registerAttachmentsManager(string $root): AttachmentsManager
|
||||||
|
{
|
||||||
|
$root = $this->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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user