CI / Docker Lint (push) Failing after 23s
- Added admin page to upload and manage media. - Fixed /setup route. - Updated dockerfiles to support admin media. - Improved deploy docs.
47 lines
1.2 KiB
PHP
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;
|
|
}
|
|
}
|