1
0
cms11/app/Classes/AttachmentsManager.php

147 lines
3.5 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\Facades\Http;
2024-04-17 15:29:12 +02:00
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);
$this->manager->merge([
'history' => [
$name => [
'reference' => $reference,
'date' => now(),
],
],
]);
2024-04-17 15:29:12 +02:00
return $reference;
}
/**
* Provides direct access to underlying manager for better control
*/
public function manager(): MetadataManager
{
return $this->manager;
}
2024-04-17 15:29:12 +02:00
/**
* 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();
2024-04-17 15:29:12 +02:00
$data['filename'] = basename($data['url']);
2024-04-17 15:29:12 +02:00
unset($data['url']);
}
$filename = $this->getFormatedFilename($data['filename']);
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);
$this->manager->set(sprintf('files.%s', $reference), $data);
2024-04-17 15:29:12 +02:00
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);
}
2024-04-17 15:29:12 +02:00
/**
* 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);
}
}