initial commit
This commit is contained in:
44
app/Services/FileService.php
Normal file
44
app/Services/FileService.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\MediaFile;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
final class FileService
|
||||
{
|
||||
public function uploadAsset(UploadedFile $file, string $folder, ?int $clientId = null): MediaFile
|
||||
{
|
||||
// Sanitise folder name — allow alphanumeric, dash, underscore only
|
||||
$folder = preg_replace('/[^a-zA-Z0-9\-_]/', '-', trim($folder)) ?: 'general';
|
||||
|
||||
$name = Str::uuid() . '.' . $file->getClientOriginalExtension();
|
||||
$directory = "talent-assets/{$folder}";
|
||||
|
||||
// Ensure the directory exists on the local disk
|
||||
Storage::disk('local')->makeDirectory($directory);
|
||||
|
||||
$path = $file->storeAs($directory, $name, 'local');
|
||||
|
||||
return MediaFile::create([
|
||||
'client_id' => $clientId,
|
||||
'name' => pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME),
|
||||
'original_name' => $file->getClientOriginalName(),
|
||||
'path' => $path,
|
||||
'disk' => 'local',
|
||||
'mime_type' => $file->getMimeType(),
|
||||
'size' => $file->getSize(),
|
||||
'folder' => $folder,
|
||||
]);
|
||||
}
|
||||
|
||||
public function delete(MediaFile $file): void
|
||||
{
|
||||
Storage::disk($file->disk)->delete($file->path);
|
||||
$file->delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user