1
0
cms11/app/Classes/AttachmentsManager.php

131 lines
2.9 KiB
PHP
Raw Normal View History

2024-04-17 15:29:12 +02:00
<?php
namespace App\Classes;
2024-04-17 16:18:39 +02:00
use App\Classes\Traits\ManagesMetadata;
2024-04-17 15:29:12 +02:00
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Str;
class AttachmentsManager
{
2024-04-17 16:18:39 +02:00
use ManagesMetadata;
2024-04-17 15:29:12 +02:00
/**
2024-04-17 16:18:39 +02:00
* Manages images
2024-04-17 15:29:12 +02:00
*/
2024-04-17 16:18:39 +02:00
const Images = 'images';
2024-04-17 15:29:12 +02:00
/**
2024-04-17 16:18:39 +02:00
* Manages sounds
2024-04-17 15:29:12 +02:00
*/
2024-04-17 16:18:39 +02:00
const Sounds = 'sounds';
2024-04-17 15:29:12 +02:00
2024-04-17 16:18:39 +02:00
/**
* Manages videos
*/
const Videos = 'videos';
private string $targetForFiles;
2024-04-17 15:29:12 +02:00
2024-04-17 16:18:39 +02:00
private string $metadataFilePath;
private MetadataManager $manager;
private string $attachmentsDir = 'attachments';
2024-04-17 16:18:39 +02:00
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);
2024-04-17 16:18:39 +02:00
$this->manager = new MetadataManager($this->metadataFilePath, $disk);
2024-04-17 15:29:12 +02:00
}
/**
* Add a file to a named history and return attachment's reference
*/
public function addToHistory(string $name, array $data): string
{
$reference = $this->add($data);
2024-04-17 16:18:39 +02:00
$this->manager['history'][$name][] = [
2024-04-17 15:29:12 +02:00
'reference' => $reference,
'date' => now(),
];
return $reference;
}
/**
* Provides direct access to underlying manager for better control
*/
public function manager(): MetadataManager
{
return $this->manager;
}
/**
* Add or replace attachment with specified reference. Will replace existing
* data
*/
public function upsert(string $reference, array $data)
{
$this->manager['files'][$reference] = $data;
}
2024-04-17 15:29:12 +02:00
/**
* 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']);
2024-04-17 16:18:39 +02:00
$fullPath = sprintf('%s/%s', $this->targetForFiles, $filename);
$relativePath = sprintf('%s/%s/%s', $this->attachmentsDir, $this->kind, $filename);
2024-04-17 15:29:12 +02:00
$data['filename'] = $relativePath;
$this->disk->put($fullPath, $contents);
2024-04-17 16:18:39 +02:00
$this->manager['files'][$reference] = $data;
2024-04-17 15:29:12 +02:00
return $reference;
}
/**
* Return a boolean value indicating if the file actually exists on disk
*/
public function exists()
{
2024-04-17 16:18:39 +02:00
return $this->disk->exists($this->metadataFilePath);
2024-04-17 15:29:12 +02:00
}
/**
* Load data from disk
*/
public function load()
{
$this->manager->load();
}
2024-04-17 15:29:12 +02:00
/**
* Store file on disk
*/
public function save(): bool
{
2024-04-17 16:18:39 +02:00
return $this->manager->save();
2024-04-17 15:29:12 +02:00
}
/**
* Generate new, random reference for a file
*/
private function generateNewReference(): string
{
return Str::random(6);
}
}