1 | <?php |
||
18 | abstract class BaseHandler |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * @param \Illuminate\Http\Request $request |
||
23 | * @param \CodingSocks\UploadHandler\StorageConfig $config |
||
24 | * @param \Closure|null $fileUploaded |
||
25 | * |
||
26 | * @return \Symfony\Component\HttpFoundation\Response |
||
27 | * |
||
28 | */ |
||
29 | abstract public function handle(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response; |
||
30 | |||
31 | /** |
||
32 | * @param string $filename |
||
33 | * @param \CodingSocks\UploadHandler\StorageConfig $storageConfig |
||
34 | * |
||
35 | * @return \Symfony\Component\HttpFoundation\Response |
||
36 | */ |
||
37 | public function fileResponse(string $filename, StorageConfig $storageConfig): Response |
||
38 | { |
||
39 | /** @var \Illuminate\Filesystem\FilesystemAdapter $disk */ |
||
40 | $disk = Storage::disk($storageConfig->getDisk()); |
||
41 | $prefix = $storageConfig->getMergedDirectory() . '/'; |
||
42 | |||
43 | if (! $disk->exists($prefix . $filename)) { |
||
44 | throw new NotFoundHttpException($filename . ' file not found on server'); |
||
45 | } |
||
46 | |||
47 | $path = $disk->path($prefix . $filename); |
||
48 | |||
49 | return new BinaryFileResponse($path, 200, [], true, ResponseHeaderBag::DISPOSITION_ATTACHMENT); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Check if the request type of the given request is in the specified list. |
||
54 | * |
||
55 | * @param \Illuminate\Http\Request $request |
||
56 | * @param array $methods |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | 135 | public function isRequestMethodIn(Request $request, array $methods): bool |
|
70 | |||
71 | /** |
||
72 | * Dispatch a {@link \CodingSocks\UploadHandler\Event\FileUploaded} event. |
||
73 | * Also call the given {@link \Closure} if not null. |
||
74 | * |
||
75 | * @param $disk |
||
76 | * @param $path |
||
77 | * @param \Closure|null $fileUploaded |
||
78 | */ |
||
79 | 20 | protected function triggerFileUploadedEvent($disk, $path, Closure $fileUploaded = null): void |
|
87 | |||
88 | /** |
||
89 | * Validate an uploaded file. An exception is thrown when it is invalid. |
||
90 | * |
||
91 | * @param \Illuminate\Http\UploadedFile|null $file |
||
92 | * |
||
93 | * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException when given file is null. |
||
94 | * @throws \CodingSocks\UploadHandler\Exception\InternalServerErrorHttpException when given file is invalid. |
||
95 | */ |
||
96 | 88 | protected function validateUploadedFile(UploadedFile $file = null): void |
|
106 | } |
||
107 |