1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodingSocks\UploadHandler\Driver; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use CodingSocks\UploadHandler\Event\FileUploaded; |
7
|
|
|
use CodingSocks\UploadHandler\Exception\InternalServerErrorHttpException; |
8
|
|
|
use CodingSocks\UploadHandler\StorageConfig; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
12
|
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException; |
13
|
|
|
|
14
|
|
|
abstract class BaseHandler |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param \Illuminate\Http\Request $request |
19
|
|
|
* @param \CodingSocks\UploadHandler\StorageConfig $config |
20
|
|
|
* @param \Closure|null $fileUploaded |
21
|
|
|
* |
22
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
abstract public function handle(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Check if the request type of the given request is in the specified list. |
29
|
|
|
* |
30
|
|
|
* @param \Illuminate\Http\Request $request |
31
|
|
|
* @param array $methods |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
135 |
|
public function isRequestMethodIn(Request $request, array $methods): bool |
36
|
|
|
{ |
37
|
135 |
|
foreach ($methods as $method) { |
38
|
135 |
|
if ($request->isMethod($method)) { |
39
|
94 |
|
return true; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
103 |
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Dispatch a {@link \CodingSocks\UploadHandler\Event\FileUploaded} event. |
48
|
|
|
* Also call the given {@link \Closure} if not null. |
49
|
|
|
* |
50
|
|
|
* @param $disk |
51
|
|
|
* @param $path |
52
|
|
|
* @param \Closure|null $fileUploaded |
53
|
|
|
*/ |
54
|
20 |
|
protected function triggerFileUploadedEvent($disk, $path, Closure $fileUploaded = null): void |
55
|
|
|
{ |
56
|
20 |
|
if ($fileUploaded !== null) { |
57
|
10 |
|
$fileUploaded($disk, $path); |
58
|
|
|
} |
59
|
|
|
|
60
|
20 |
|
event(new FileUploaded($disk, $path)); |
61
|
20 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Validate an uploaded file. An exception is thrown when it is invalid. |
65
|
|
|
* |
66
|
|
|
* @param \Illuminate\Http\UploadedFile|array|null $file |
67
|
|
|
* |
68
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException when given file is null. |
69
|
|
|
* @throws \CodingSocks\UploadHandler\Exception\InternalServerErrorHttpException when given file is invalid. |
70
|
|
|
*/ |
71
|
88 |
|
protected function validateUploadedFile($file): void |
72
|
|
|
{ |
73
|
88 |
|
if (null === $file) { |
74
|
8 |
|
throw new BadRequestHttpException('File not found in request body'); |
75
|
|
|
} |
76
|
|
|
|
77
|
80 |
|
if (is_array($file)) { |
78
|
|
|
throw new UnprocessableEntityHttpException('File parameter cannot be an array'); |
79
|
|
|
} |
80
|
|
|
|
81
|
80 |
|
if (! $file->isValid()) { |
82
|
8 |
|
throw new InternalServerErrorHttpException($file->getErrorMessage()); |
83
|
|
|
} |
84
|
72 |
|
} |
85
|
|
|
} |
86
|
|
|
|