monsieurbiz /
SyliusRichEditorPlugin
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of Monsieur Biz' Rich Editor plugin for Sylius. |
||
| 5 | * |
||
| 6 | * (c) Monsieur Biz <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | declare(strict_types=1); |
||
| 13 | |||
| 14 | namespace MonsieurBiz\SyliusRichEditorPlugin\Uploader; |
||
| 15 | |||
| 16 | use Symfony\Component\HttpFoundation\File\Exception\FileException; |
||
| 17 | use Symfony\Component\HttpFoundation\File\File; |
||
| 18 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
||
| 19 | |||
| 20 | class FileUploader |
||
| 21 | { |
||
| 22 | public const FILE_TYPE_DOCUMENT = 'document'; |
||
| 23 | public const FILE_TYPE_IMAGE = 'image'; |
||
| 24 | public const FILE_TYPE_VIDEO = 'video'; |
||
| 25 | |||
| 26 | private string $fileTargetPath; |
||
| 27 | private string $imageTargetPath; |
||
| 28 | private string $publicDirectory; |
||
| 29 | |||
| 30 | public function __construct(string $fileTargetPath, string $imageTargetPath, string $publicDirectory) |
||
| 31 | { |
||
| 32 | $this->fileTargetPath = $fileTargetPath; |
||
| 33 | $this->imageTargetPath = $imageTargetPath; |
||
| 34 | $this->publicDirectory = $publicDirectory; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Upload a file and return the path to it. |
||
| 39 | * |
||
| 40 | * @param UploadedFile $file |
||
| 41 | * @param string|null $type |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public function upload(UploadedFile $file, ?string $type = null): string |
||
| 46 | { |
||
| 47 | $type = $type ?? self::FILE_TYPE_DOCUMENT; |
||
| 48 | |||
| 49 | // See @https://symfony.com/doc/4.4/controller/upload_file.html |
||
| 50 | $originalFilename = pathinfo($file->getClientOriginalName(), \PATHINFO_FILENAME); |
||
| 51 | $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 52 | $fileName = $safeFilename . '-' . uniqid() . '.' . $file->guessExtension(); |
||
| 53 | |||
| 54 | try { |
||
| 55 | $file = $file->move($this->getTargetDirectory($type), $fileName); |
||
| 56 | } catch (FileException $e) { |
||
| 57 | return ''; |
||
| 58 | } |
||
| 59 | |||
| 60 | // Generate path from public folder |
||
| 61 | return $this->getFinalPath($file, $type); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The directory to write the file. |
||
| 66 | * |
||
| 67 | * @param string $type |
||
| 68 | * |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | private function getTargetDirectory(string $type): string |
||
| 72 | { |
||
| 73 | switch ($type) { |
||
| 74 | case self::FILE_TYPE_IMAGE: |
||
| 75 | return $this->publicDirectory . $this->imageTargetPath; |
||
| 76 | default: |
||
| 77 | return $this->publicDirectory . $this->fileTargetPath; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param File $file |
||
| 83 | * @param string $type |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | private function getFinalPath(File $file, string $type): string |
||
| 88 | { |
||
| 89 | switch ($type) { |
||
| 90 | case self::FILE_TYPE_IMAGE: |
||
| 91 | return $file->getBasename(); |
||
| 92 | default: |
||
| 93 | return str_replace($this->publicDirectory, '', $file->getPathname()); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 |