jbtronics /
StuRa-Finanzsoftware
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * Copyright (C) 2020 Jan Böhmer |
||
| 4 | * |
||
| 5 | * This program is free software: you can redistribute it and/or modify |
||
| 6 | * it under the terms of the GNU Affero General Public License as published |
||
| 7 | * by the Free Software Foundation, either version 3 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU Affero General Public License for more details. |
||
| 14 | * |
||
| 15 | * You should have received a copy of the GNU Affero General Public License |
||
| 16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
| 17 | */ |
||
| 18 | |||
| 19 | namespace App\Services\Upload; |
||
| 20 | |||
| 21 | use App\Entity\PaymentOrder; |
||
| 22 | use InvalidArgumentException; |
||
| 23 | use function pathinfo; |
||
| 24 | use function strtolower; |
||
| 25 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
||
| 26 | use Symfony\Component\String\Slugger\AsciiSlugger; |
||
| 27 | use Vich\UploaderBundle\Mapping\PropertyMapping; |
||
| 28 | use Vich\UploaderBundle\Naming\NamerInterface; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Use a custom namer to create file names which are useful when viewing the directory structure. |
||
| 32 | * It contains the department, current date and the project name all in slugged form. |
||
| 33 | */ |
||
| 34 | class PaymentOrderFileNamer implements NamerInterface |
||
| 35 | { |
||
| 36 | public function name($object, PropertyMapping $mapping): string |
||
| 37 | { |
||
| 38 | if (!$object instanceof PaymentOrder) { |
||
| 39 | throw new InvalidArgumentException('$object must be an PaymentOrder!'); |
||
| 40 | } |
||
| 41 | |||
| 42 | $file = $mapping->getFile($object); |
||
| 43 | if ($file instanceof UploadedFile) { |
||
| 44 | $originalName = $file->getClientOriginalName(); |
||
| 45 | } else { |
||
| 46 | throw new InvalidArgumentException('$file must be an UploadedFile instance!'); |
||
| 47 | } |
||
| 48 | $originalExtension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 49 | $originalBasename = pathinfo($originalName, PATHINFO_FILENAME); |
||
|
0 ignored issues
–
show
|
|||
| 50 | |||
| 51 | $slugger = new AsciiSlugger(); |
||
| 52 | |||
| 53 | $filename = mb_strimwidth($slugger->slug($object->getDepartment()->getName() ?? 'unknown'), 0, 16); |
||
| 54 | |||
| 55 | $filename .= '_'.mb_strimwidth($slugger->slug($object->getProjectName()), 0, 16); |
||
| 56 | |||
| 57 | $filename .= '_'.date('ymd-His'); |
||
| 58 | |||
| 59 | $filename .= '_'.bin2hex(random_bytes(5)); |
||
| 60 | |||
| 61 | //Add original extension |
||
| 62 | $filename .= '.'.$originalExtension; |
||
| 63 | |||
| 64 | return $filename; |
||
| 65 | } |
||
| 66 | } |
||
| 67 |