Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
UploadedFilesHandler | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
72 | |
0.00% |
0 / 1 |
handleUploadedFiles | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | declare(strict_types=1); |
4 | |
5 | // TODO unit test |
6 | |
7 | use Slim\Exception\HttpBadRequestException; |
8 | use Slim\Exception\HttpException; |
9 | use Slim\Http\ServerRequest as Request; |
10 | use Slim\Http\UploadedFile; |
11 | |
12 | class UploadedFilesHandler { |
13 | const errorMessages = [ |
14 | UPLOAD_ERR_INI_SIZE => [ // php.ini max_file_size |
15 | 'message' => 'The uploaded file exceeds the maximum.', |
16 | 'code' => 413 |
17 | ], |
18 | UPLOAD_ERR_FORM_SIZE => [ // html form MAX_FILE_SIZE |
19 | 'message' => 'The uploaded file exceeds the form maximum.', |
20 | 'code' => 413 |
21 | ], |
22 | UPLOAD_ERR_PARTIAL => [ |
23 | 'message' => 'The uploaded file was only partially uploaded.', |
24 | 'code' => 500 |
25 | ], |
26 | UPLOAD_ERR_NO_FILE => [ |
27 | 'message' => 'No file was uploaded.', |
28 | 'code' => 500 |
29 | ], |
30 | UPLOAD_ERR_NO_TMP_DIR => [ |
31 | 'message' => 'Missing a temporary folder.', |
32 | 'code' => 500 |
33 | ], |
34 | UPLOAD_ERR_CANT_WRITE => [ |
35 | 'message' => 'Failed to write file to disk.', |
36 | 'code' => 500 |
37 | ] |
38 | ]; |
39 | |
40 | static function handleUploadedFiles(Request $request, string $fieldName, string $workspacePath): array { |
41 | $allUploadedFiles = $request->getUploadedFiles(); |
42 | |
43 | if (!count($allUploadedFiles)) { |
44 | if (intval($_SERVER['CONTENT_LENGTH']) > 0) { |
45 | throw new HttpException($request, 'Request size exceeds the maximum for post data!', 413); // max_post_size |
46 | } |
47 | |
48 | throw new HttpBadRequestException($request, "No Upload File."); |
49 | } |
50 | |
51 | if (!isset($allUploadedFiles[$fieldName])) { |
52 | throw new HttpBadRequestException($request, "No Upload File in $fieldName"); |
53 | } |
54 | |
55 | $uploadedFiles = $allUploadedFiles[$fieldName]; |
56 | |
57 | if (!is_array($uploadedFiles)) { |
58 | $uploadedFiles = [$uploadedFiles]; |
59 | } |
60 | |
61 | $filesToImport = []; |
62 | |
63 | foreach ($uploadedFiles as $uploadedFile) { |
64 | /** @var UploadedFile $uploadedFile */ |
65 | |
66 | if ($uploadedFile->getError() !== UPLOAD_ERR_OK) { |
67 | if (isset(UploadedFilesHandler::errorMessages[$uploadedFile->getError()])) { |
68 | $error = UploadedFilesHandler::errorMessages[$uploadedFile->getError()]; |
69 | } else { |
70 | throw new HttpException($request, 'Unknown Error'); |
71 | } |
72 | |
73 | throw new HttpException($request, $error['message'], $error['code']); |
74 | } |
75 | $originalFileName = $uploadedFile->getClientFilename(); |
76 | $uploadedFile->moveTo($workspacePath . '/' . $originalFileName); |
77 | $filesToImport[] = $originalFileName; |
78 | } |
79 | |
80 | return $filesToImport; |
81 | } |
82 | } |