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 App\Entity\SEPAExport; |
23
|
|
|
use InvalidArgumentException; |
24
|
|
|
use function pathinfo; |
25
|
|
|
use function strtolower; |
26
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
27
|
|
|
use Symfony\Component\String\Slugger\AsciiSlugger; |
28
|
|
|
use Vich\UploaderBundle\Mapping\PropertyMapping; |
29
|
|
|
use Vich\UploaderBundle\Naming\NamerInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Use a custom namer to create file names which are useful when viewing the directory structure. |
33
|
|
|
* It contains the department, current date and the project name all in slugged form. |
34
|
|
|
*/ |
35
|
|
|
class SEPAExportFileNamer implements NamerInterface |
36
|
|
|
{ |
37
|
|
|
public function name($object, PropertyMapping $mapping): string |
38
|
|
|
{ |
39
|
|
|
if (!$object instanceof SEPAExport) { |
40
|
|
|
throw new InvalidArgumentException('$object must be an PaymentOrder!'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$file = $mapping->getFile($object); |
44
|
|
|
if ($file instanceof UploadedFile) { |
45
|
|
|
$originalName = $file->getClientOriginalName(); |
46
|
|
|
} else { |
47
|
|
|
throw new InvalidArgumentException('$file must be an UploadedFile instance!'); |
48
|
|
|
} |
49
|
|
|
$originalExtension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION)); |
|
|
|
|
50
|
|
|
$originalBasename = pathinfo($originalName, PATHINFO_FILENAME); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$slugger = new AsciiSlugger(); |
|
|
|
|
53
|
|
|
|
54
|
|
|
//TODO: Improve this |
55
|
|
|
$filename = sprintf("XML%06d", $object->getId()); |
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
|
|
|
|