104 lines
2.9 KiB
PHP
104 lines
2.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use App\Models\Media;
|
||
|
|
use Illuminate\Http\UploadedFile;
|
||
|
|
use Illuminate\Support\Facades\Storage;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Service to handle Media-related operations.
|
||
|
|
*/
|
||
|
|
class MediaService
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Store a newly uploaded media file.
|
||
|
|
*
|
||
|
|
* @param UploadedFile $file The uploaded file object.
|
||
|
|
* @return Media The created media model instance.
|
||
|
|
*/
|
||
|
|
public function upload(UploadedFile $file): Media
|
||
|
|
{
|
||
|
|
$filename = $file->getClientOriginalName();
|
||
|
|
|
||
|
|
// Basic sanitization
|
||
|
|
$filename = preg_replace('/[^a-zA-Z0-9.\-_]/', '_', $filename);
|
||
|
|
|
||
|
|
// Ensure unique filename in DB/storage
|
||
|
|
$originalFilename = pathinfo($filename, PATHINFO_FILENAME);
|
||
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
||
|
|
$counter = 1;
|
||
|
|
|
||
|
|
while (Storage::disk('public')->exists('media/' . $filename)) {
|
||
|
|
$filename = $originalFilename . '-' . $counter . '.' . $extension;
|
||
|
|
$counter++;
|
||
|
|
}
|
||
|
|
|
||
|
|
$path = $file->storeAs('media', $filename, 'public');
|
||
|
|
|
||
|
|
return Media::create([
|
||
|
|
'filename' => $filename,
|
||
|
|
'path' => $path,
|
||
|
|
'mime_type' => $file->getMimeType(),
|
||
|
|
'size' => $file->getSize(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the metadata or focal point of a media file.
|
||
|
|
*
|
||
|
|
* @param int $id The media record ID.
|
||
|
|
* @param array $data The data containing focal_x, focal_y, or metadata.
|
||
|
|
* @return Media|null The updated media instance or null if not found.
|
||
|
|
*/
|
||
|
|
public function update(int $id, array $data): ?Media
|
||
|
|
{
|
||
|
|
$media = Media::find($id);
|
||
|
|
|
||
|
|
if ($media) {
|
||
|
|
$updateData = [];
|
||
|
|
if (isset($data['focal_x'])) $updateData['focal_x'] = $data['focal_x'];
|
||
|
|
if (isset($data['focal_y'])) $updateData['focal_y'] = $data['focal_y'];
|
||
|
|
if (isset($data['metadata'])) $updateData['metadata'] = $data['metadata'];
|
||
|
|
|
||
|
|
$media->update($updateData);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $media;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the focal point of a media file.
|
||
|
|
*
|
||
|
|
* @param int $id The media record ID.
|
||
|
|
* @param array $data The data containing focal_x and focal_y.
|
||
|
|
* @return Media|null The updated media instance or null if not found.
|
||
|
|
* @deprecated Use update instead.
|
||
|
|
*/
|
||
|
|
public function updateFocalPoint(int $id, array $data): ?Media
|
||
|
|
{
|
||
|
|
return $this->update($id, $data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the specified media file from storage and database.
|
||
|
|
*
|
||
|
|
* @param int $id The media record ID.
|
||
|
|
* @return bool True if deleted, false if not found.
|
||
|
|
*/
|
||
|
|
public function delete(int $id): bool
|
||
|
|
{
|
||
|
|
$media = Media::find($id);
|
||
|
|
|
||
|
|
if (!$media) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Storage::disk('public')->exists($media->path)) {
|
||
|
|
Storage::disk('public')->delete($media->path);
|
||
|
|
}
|
||
|
|
|
||
|
|
return (bool) $media->delete();
|
||
|
|
}
|
||
|
|
}
|