119 lines
3.6 KiB
PHP
119 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\DisplayMedia;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class DisplayMediaService
|
|
{
|
|
/**
|
|
* Store an uploaded file and create a DisplayMedia record.
|
|
*/
|
|
public function storeUpload(UploadedFile $file, ?string $collection = null): DisplayMedia
|
|
{
|
|
$filename = $file->getClientOriginalName();
|
|
$extension = strtolower($file->getClientOriginalExtension());
|
|
$storageName = Str::uuid().'.'.$extension;
|
|
$datePath = now()->format('Y/m');
|
|
$relativePath = "display-media/{$datePath}/{$storageName}";
|
|
|
|
Storage::disk('public')->putFileAs("display-media/{$datePath}", $file, $storageName);
|
|
|
|
$type = in_array($extension, ['mp4', 'webm', 'mov']) ? 'video' : 'image';
|
|
|
|
$metadata = [];
|
|
if ($type === 'image') {
|
|
$dimensions = @getimagesize($file->getRealPath());
|
|
if ($dimensions) {
|
|
$metadata['width'] = $dimensions[0];
|
|
$metadata['height'] = $dimensions[1];
|
|
}
|
|
}
|
|
|
|
return DisplayMedia::create([
|
|
'filename' => $filename,
|
|
'disk' => 'public',
|
|
'path' => $relativePath,
|
|
'source_type' => 'upload',
|
|
'type' => $type,
|
|
'mime_type' => $file->getMimeType(),
|
|
'file_size' => $file->getSize(),
|
|
'collection' => $collection,
|
|
'metadata' => ! empty($metadata) ? $metadata : null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Create a DisplayMedia record from an external URL.
|
|
*/
|
|
public function createFromUrl(string $url, string $type = 'video', ?string $title = null, ?string $collection = null): DisplayMedia
|
|
{
|
|
$filename = $title ?: $this->extractFilenameFromUrl($url);
|
|
|
|
return DisplayMedia::create([
|
|
'filename' => $filename,
|
|
'disk' => 'public',
|
|
'path' => null,
|
|
'external_url' => $url,
|
|
'source_type' => 'external',
|
|
'type' => $type,
|
|
'mime_type' => null,
|
|
'file_size' => 0,
|
|
'title' => $title,
|
|
'collection' => $collection,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Validate that an external URL is accessible.
|
|
*/
|
|
public function validateExternalUrl(string $url): bool
|
|
{
|
|
try {
|
|
$response = Http::timeout(10)
|
|
->withOptions(['allow_redirects' => true])
|
|
->head($url);
|
|
|
|
return $response->successful() || $response->status() === 302 || $response->status() === 301;
|
|
} catch (\Throwable) {
|
|
// Some services block HEAD requests, try GET with stream
|
|
try {
|
|
$response = Http::timeout(10)
|
|
->withOptions(['allow_redirects' => true, 'stream' => true])
|
|
->get($url);
|
|
|
|
return $response->successful();
|
|
} catch (\Throwable) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a DisplayMedia record and its associated files.
|
|
*/
|
|
public function delete(DisplayMedia $media): void
|
|
{
|
|
if ($media->isUpload() && $media->path) {
|
|
Storage::disk($media->disk)->delete($media->path);
|
|
}
|
|
|
|
if ($media->thumbnail_path) {
|
|
Storage::disk($media->disk)->delete($media->thumbnail_path);
|
|
}
|
|
|
|
$media->delete();
|
|
}
|
|
|
|
private function extractFilenameFromUrl(string $url): string
|
|
{
|
|
$parsed = parse_url($url, PHP_URL_PATH);
|
|
$basename = $parsed ? basename($parsed) : 'external-media';
|
|
|
|
return Str::limit($basename, 100);
|
|
}
|
|
}
|