1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Upload\Apis; |
4
|
|
|
|
5
|
|
|
class FileAPI extends Base |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* getOriginalName. |
9
|
|
|
* |
10
|
|
|
* @return string |
11
|
|
|
*/ |
12
|
3 |
|
protected function getOriginalName($contentDisposition) |
13
|
|
|
{ |
14
|
3 |
|
$originalName = $this->request->get('name'); |
15
|
3 |
|
if (empty($originalName) === true) { |
16
|
3 |
|
list($originalName) = sscanf( |
17
|
3 |
|
$contentDisposition, |
18
|
|
|
'attachment; filename=%s' |
19
|
3 |
|
); |
20
|
3 |
|
} |
21
|
|
|
|
22
|
3 |
|
return preg_replace('/[\'"]/', '', $originalName); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* getMimeType. |
27
|
|
|
* |
28
|
|
|
* @param string $originalName |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
3 |
|
protected function getMimeType($originalName) |
32
|
|
|
{ |
33
|
3 |
|
$mimeType = $this->request->header('content-type'); |
34
|
3 |
|
if (empty($mimeType) === true) { |
35
|
|
|
$mimeType = $this->filesystem->mimeType($originalName); |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
return $mimeType; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* receive. |
43
|
|
|
* |
44
|
|
|
* @param string $inputName |
45
|
|
|
* @return \Symfony\Component\HttpFoundation\File\UploadedFile |
46
|
|
|
* |
47
|
|
|
* @throws \Recca0120\Upload\Exceptions\ChunkedResponseException |
48
|
|
|
*/ |
49
|
4 |
|
protected function doReceive($inputName) |
50
|
|
|
{ |
51
|
4 |
|
$contentDisposition = $this->request->header('content-disposition'); |
52
|
4 |
|
if (empty($contentDisposition) === true) { |
53
|
1 |
|
return $this->request->file($inputName); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
$contentRange = $this->request->header('content-range'); |
57
|
3 |
|
if (empty($contentRange) === false) { |
58
|
2 |
|
list($start, $end, $total) = sscanf($contentRange, 'bytes %d-%d/%d'); |
59
|
2 |
|
} else { |
60
|
1 |
|
$start = 0; |
61
|
1 |
|
$end = $this->request->header('content-length'); |
62
|
1 |
|
$total = $end; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
return $this->receiveChunkedFile( |
66
|
3 |
|
$originalName = $this->getOriginalName($contentDisposition), |
|
|
|
|
67
|
3 |
|
'php://input', |
68
|
3 |
|
$start, |
69
|
3 |
|
$end >= $total - 1, |
70
|
3 |
|
['mimeType' => $this->getMimeType($originalName), 'headers' => ['X-Last-Known-Byte' => $end]] |
|
|
|
|
71
|
3 |
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.