1
0

Group all secondary files to a data folder in bundle's directory

This commit is contained in:
Richard Dern 2024-04-17 16:54:37 +02:00
parent f17057cc36
commit a1539ce35c
5 changed files with 30 additions and 16 deletions

View File

@ -31,10 +31,12 @@ class AttachmentsManager
private MetadataManager $manager;
private string $attachmentsDir = 'attachments';
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->targetForFiles = sprintf('%s%s/%s', $root, $this->attachmentsDir, $kind);
$this->manager = new MetadataManager($this->metadataFilePath, $disk);
}
@ -66,7 +68,7 @@ public function add(array $data): string
unset($data['contents']);
$fullPath = sprintf('%s/%s', $this->targetForFiles, $filename);
$relativePath = sprintf('%s/%s', $this->kind, $filename);
$relativePath = sprintf('%s/%s/%s', $this->attachmentsDir, $this->kind, $filename);
$data['filename'] = $relativePath;

View File

@ -12,17 +12,12 @@ class Bundle
{
use ManagesAttachments, ManagesMarkdown, ManagesMetadata;
/**
* Return bundle's path
*/
public function getPath(): string
{
return $this->path;
}
protected string $dataDir;
public function __construct(protected string $path, protected FilesystemAdapter $disk)
{
$this->path = Str::start(Str::finish($this->path, '/'), '/');
$this->dataDir = $this->path . 'data/';
}
/**
@ -36,17 +31,34 @@ public function save()
}
/**
* Gte a complete filename prefixed with bundle's path
* Get a complete filename prefixed with bundle's path
*/
private function getFilenameInBundle(string $filename, ?string $extension = null)
{
$filename = Str::remove($this->path, $filename);
return $this->getFullpath($this->path, $filename, $extension);
}
/**
* Get a complete filename prefixed with bundle's data dir
*/
private function getFilenameInDataBundle(string $filename, ?string $extension = null)
{
return $this->getFullpath($this->dataDir, $filename, $extension);
}
/**
* Return full path of specified filename in specified root directory, and
* optionally add specified extension
*/
private function getFullpath(string $root, string $filename, ?string $extension = null)
{
$filename = Str::remove($root, $filename);
if (!empty($extension) && !Str::endsWith($filename, $extension)) {
$filename .= $extension;
}
return sprintf('%s%s', $this->path, $filename);
return sprintf('%s%s', $root, $filename);
}
/**

View File

@ -14,7 +14,7 @@ trait ManagesAttachments
private function registerAttachmentsManager(string $kind): AttachmentsManager
{
if (!array_key_exists($kind, $this->attachmentsManagers)) {
$this->attachmentsManagers[$kind] = new AttachmentsManager($kind, $this->path, $this->disk);
$this->attachmentsManagers[$kind] = new AttachmentsManager($kind, $this->dataDir, $this->disk);
}
return $this->attachmentsManagers[$kind];

View File

@ -13,7 +13,7 @@ trait ManagesMetadata
*/
private function registerMetadataManager(string $filename): MetadataManager
{
$filename = $this->getFilenameInBundle($filename, '.json');
$filename = $this->getFilenameInDataBundle($filename, '.json');
if (!array_key_exists($filename, $this->metadataManagers)) {
$this->metadataManagers[$filename] = new MetadataManager($filename, $this->disk);

View File

@ -46,7 +46,7 @@ public function createBundle(): string
$ref = $bundle->attachments(AttachmentsManager::Images)->addToHistory('screenshot', [
'contents' => $screenshot,
'filename' => 'screenshot.jpg',
'filename' => sprintf('screenshot-%s.jpg', now()->format('Y-m-d')),
]);
$bundle->metadata()->set([