- Added admin page to upload and manage media. - Fixed /setup route. - Updated dockerfiles to support admin media. - Improved deploy docs.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user