1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodingSocks\UploadHandler\Driver; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use CodingSocks\UploadHandler\Helper\ChunkHelpers; |
7
|
|
|
use CodingSocks\UploadHandler\Range\DropzoneRange; |
8
|
|
|
use CodingSocks\UploadHandler\Response\PercentageJsonResponse; |
9
|
|
|
use CodingSocks\UploadHandler\StorageConfig; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Illuminate\Http\UploadedFile; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
16
|
|
|
|
17
|
|
|
class DropzoneBaseHandler extends BaseHandler |
18
|
|
|
{ |
19
|
1 |
|
use ChunkHelpers; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $fileParam; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* DropzoneDriver constructor. |
28
|
|
|
* |
29
|
|
|
* @param array $config |
30
|
|
|
*/ |
31
|
16 |
|
public function __construct($config) |
32
|
|
|
{ |
33
|
16 |
|
$this->fileParam = $config['param']; |
34
|
16 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
14 |
|
public function handle(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response |
40
|
|
|
{ |
41
|
14 |
|
if ($this->isRequestMethodIn($request, [Request::METHOD_POST])) { |
42
|
14 |
|
return $this->save($request, $config, $fileUploaded); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
throw new MethodNotAllowedHttpException([ |
46
|
|
|
Request::METHOD_POST, |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \Illuminate\Http\Request $request |
52
|
|
|
* @param \CodingSocks\UploadHandler\StorageConfig $config |
53
|
|
|
* @param \Closure|null $fileUploaded |
54
|
|
|
* |
55
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
56
|
|
|
*/ |
57
|
14 |
View Code Duplication |
public function save(Request $request, StorageConfig $config, Closure $fileUploaded = null): Response |
|
|
|
|
58
|
|
|
{ |
59
|
14 |
|
$file = $request->file($this->fileParam); |
60
|
|
|
|
61
|
14 |
|
$this->validateUploadedFile($file); |
|
|
|
|
62
|
|
|
|
63
|
12 |
|
if ($this->isMonolithRequest($request)) { |
64
|
2 |
|
return $this->saveMonolith($file, $config, $fileUploaded); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
10 |
|
$this->validateChunkRequest($request); |
68
|
|
|
|
69
|
4 |
|
return $this->saveChunk($file, $request, $config, $fileUploaded); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param \Illuminate\Http\Request $request |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
12 |
|
private function isMonolithRequest(Request $request): bool |
78
|
|
|
{ |
79
|
12 |
|
return $request->post('dzuuid') === null |
80
|
12 |
|
&& $request->post('dzchunkindex') === null |
81
|
12 |
|
&& $request->post('dztotalfilesize') === null |
82
|
12 |
|
&& $request->post('dzchunksize') === null |
83
|
12 |
|
&& $request->post('dztotalchunkcount') === null |
84
|
12 |
|
&& $request->post('dzchunkbyteoffset') === null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param \Illuminate\Http\Request $request |
89
|
|
|
*/ |
90
|
10 |
|
private function validateChunkRequest(Request $request): void |
91
|
|
|
{ |
92
|
10 |
|
$request->validate([ |
|
|
|
|
93
|
10 |
|
'dzuuid' => 'required', |
94
|
|
|
'dzchunkindex' => 'required', |
95
|
|
|
'dztotalfilesize' => 'required', |
96
|
|
|
'dzchunksize' => 'required', |
97
|
|
|
'dztotalchunkcount' => 'required', |
98
|
|
|
'dzchunkbyteoffset' => 'required', |
99
|
|
|
]); |
100
|
4 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param \Illuminate\Http\UploadedFile $file |
104
|
|
|
* @param \CodingSocks\UploadHandler\StorageConfig $config |
105
|
|
|
* @param \Closure|null $fileUploaded |
106
|
|
|
* |
107
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
108
|
|
|
*/ |
109
|
2 |
View Code Duplication |
private function saveMonolith(UploadedFile $file, StorageConfig $config, Closure $fileUploaded = null): Response |
|
|
|
|
110
|
|
|
{ |
111
|
2 |
|
$path = $file->store($config->getMergedDirectory(), [ |
112
|
2 |
|
'disk' => $config->getDisk(), |
113
|
|
|
]); |
114
|
|
|
|
115
|
2 |
|
$this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded); |
116
|
|
|
|
117
|
2 |
|
return new PercentageJsonResponse(100); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param \Illuminate\Http\UploadedFile $file |
122
|
|
|
* @param \Illuminate\Http\Request $request |
123
|
|
|
* @param \CodingSocks\UploadHandler\StorageConfig $config |
124
|
|
|
* @param \Closure|null $fileUploaded |
125
|
|
|
* |
126
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
127
|
|
|
*/ |
128
|
4 |
|
private function saveChunk(UploadedFile $file, Request $request, StorageConfig $config, Closure $fileUploaded = null): Response |
129
|
|
|
{ |
130
|
|
|
try { |
131
|
4 |
|
$range = new DropzoneRange( |
132
|
4 |
|
$request, |
133
|
4 |
|
'dzchunkindex', |
134
|
4 |
|
'dztotalchunkcount', |
135
|
4 |
|
'dzchunksize', |
136
|
4 |
|
'dztotalfilesize' |
137
|
|
|
); |
138
|
|
|
} catch (InvalidArgumentException $e) { |
139
|
|
|
throw new BadRequestHttpException($e->getMessage(), $e); |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
$uid = $request->post('dzuuid'); |
143
|
|
|
|
144
|
4 |
|
$chunks = $this->storeChunk($config, $range, $file, $uid); |
|
|
|
|
145
|
|
|
|
146
|
4 |
|
if (!$range->isFinished($chunks)) { |
147
|
2 |
|
return new PercentageJsonResponse($range->getPercentage($chunks)); |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
$targetFilename = $file->hashName(); |
151
|
|
|
|
152
|
2 |
|
$path = $this->mergeChunks($config, $chunks, $targetFilename); |
153
|
|
|
|
154
|
2 |
|
if ($config->sweep()) { |
155
|
|
|
$this->deleteChunkDirectory($config, $uid); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
$this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded); |
159
|
|
|
|
160
|
2 |
|
return new PercentageJsonResponse(100); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.