1
0
cms11/app/Classes/AttachmentsManager.php
Richard Dern 59ac3b9b93 Better metadata and attachments management
Allows for dot notation, merging and deep-replacing
2024-04-18 11:41:29 +02:00

147 lines
3.5 KiB
PHP

<?php
namespace App\Classes;
use App\Classes\Traits\ManagesMetadata;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class AttachmentsManager
{
use ManagesMetadata;
/**
* Manages images
*/
const Images = 'images';
/**
* Manages sounds
*/
const Sounds = 'sounds';
/**
* Manages videos
*/
const Videos = 'videos';
private string $targetForFiles;
private string $metadataFilePath;
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/%s', $root, $this->attachmentsDir, $kind);
$this->manager = new MetadataManager($this->metadataFilePath, $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->manager->merge([
'history' => [
$name => [
'reference' => $reference,
'date' => now(),
],
],
]);
return $reference;
}
/**
* Provides direct access to underlying manager for better control
*/
public function manager(): MetadataManager
{
return $this->manager;
}
/**
* Add a single file to the bundle
*/
public function add(array $data): string
{
$reference = $this->generateNewReference();
if (!empty($data['contents'])) {
// Adding from an image resource of which $data['contents'] is a
// representation
$contents = $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);
$relativePath = sprintf('%s/%s/%s', $this->attachmentsDir, $this->kind, $filename);
$data['filename'] = $relativePath;
$this->disk->put($fullPath, $contents);
$this->manager->set(sprintf('files.%s', $reference), $data);
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
*/
public function exists()
{
return $this->disk->exists($this->metadataFilePath);
}
/**
* Load data from disk
*/
public function load()
{
$this->manager->load();
}
/**
* Store file on disk
*/
public function save(): bool
{
return $this->manager->save();
}
/**
* Generate new, random reference for a file
*/
private function generateNewReference(): string
{
return Str::random(6);
}
}