Files
SomniaRooms/src/Service/FileUploaderService.php
T
Daniel-Garmig ee058aa3b3
CI / Docker Lint (push) Failing after 23s
Photo management and various fixes.
- Added admin page to upload and manage media.
- Fixed /setup route.
- Updated dockerfiles to support admin media.
- Improved deploy docs.
2024-09-14 20:53:35 +02:00

47 lines
1.2 KiB
PHP

<?php
namespace App\Service;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\String\Slugger\SluggerInterface;
class FileUploaderService
{
public function __construct(
private string $targetDirectory,
private SluggerInterface $slugger,
) {}
public function upload(UploadedFile $file): string
{
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = $this->slugger->slug($originalFilename);
$fileName = $safeFilename . '-' . uniqid() . '.' . $file->guessExtension();
try {
$file->move($this->getTargetDirectory(), $fileName);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
return $fileName;
}
public function deleteFile(string $filepath): bool
{
if (!$filepath || !is_file($filepath)) {
return false;
}
$deleted = unlink($filepath);
return $deleted;
}
public function getTargetDirectory(): string
{
return $this->targetDirectory;
}
}