1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Upload; |
4
|
|
|
|
5
|
|
|
class FileAPI extends Api |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* receive. |
9
|
|
|
* |
10
|
|
|
* @param string $name |
11
|
|
|
* @return \Symfony\Component\HttpFoundation\File\UploadedFile |
12
|
|
|
* |
13
|
|
|
* @throws \Recca0120\Upload\Exceptions\ChunkedResponseException |
14
|
|
|
*/ |
15
|
4 |
|
public function receive($name) |
16
|
|
|
{ |
17
|
4 |
|
$contentDisposition = (string) $this->request->header('content-disposition'); |
18
|
4 |
|
if (empty($contentDisposition) === true) { |
19
|
1 |
|
return $this->request->file($name); |
20
|
|
|
} |
21
|
|
|
|
22
|
3 |
|
list($start, $end, $total) = $this->parseContentRange(); |
23
|
3 |
|
$originalName = $this->getOriginalName($contentDisposition); |
24
|
3 |
|
$mimeType = $this->getMimeType($originalName); |
25
|
3 |
|
$completed = $end >= $total - 1; |
26
|
|
|
|
27
|
3 |
|
$this->chunkFile |
28
|
3 |
|
->setToken($this->request->get('token')) |
29
|
3 |
|
->setChunkPath($this->chunkPath()) |
30
|
3 |
|
->setStoragePath($this->storagePath()) |
31
|
3 |
|
->setName($originalName) |
32
|
3 |
|
->setMimeType($mimeType) |
|
|
|
|
33
|
3 |
|
->appendStream('php://input', $start); |
34
|
|
|
|
35
|
|
|
return $completed === true |
36
|
3 |
|
? $this->chunkFile->createUploadedFile() |
37
|
3 |
|
: $this->chunkFile->throwException([ |
38
|
|
|
'files' => [ |
39
|
1 |
|
'name' => $originalName, |
40
|
1 |
|
'size' => $end, |
41
|
1 |
|
'type' => $mimeType, |
42
|
1 |
|
], |
43
|
3 |
|
], ['X-Last-Known-Byte' => $end]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* getOriginalName. |
48
|
|
|
* |
49
|
|
|
* @param string $contentDisposition |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
3 |
|
protected function getOriginalName($contentDisposition) |
53
|
|
|
{ |
54
|
3 |
|
$originalName = (string) $this->request->get('name'); |
55
|
3 |
|
if (empty($originalName) === true) { |
56
|
3 |
|
list($originalName) = sscanf( |
57
|
3 |
|
$contentDisposition, |
58
|
|
|
'attachment; filename=%s' |
59
|
3 |
|
); |
60
|
3 |
|
} |
61
|
|
|
|
62
|
3 |
|
return preg_replace('/[\'"]/', '', $originalName); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* getMimeType. |
67
|
|
|
* |
68
|
|
|
* @param string $originalName |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
3 |
|
protected function getMimeType($originalName) |
72
|
|
|
{ |
73
|
3 |
|
$mimeType = (string) $this->request->header('content-type'); |
74
|
3 |
|
if (empty($mimeType) === true) { |
75
|
|
|
$mimeType = $this->files->mimeType($originalName); |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
return $mimeType; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* parseContentRange. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
3 |
|
protected function parseContentRange() |
87
|
|
|
{ |
88
|
3 |
|
$contentRange = $this->request->header('content-range'); |
89
|
3 |
|
if (empty($contentRange) === false) { |
90
|
2 |
|
return sscanf($contentRange, 'bytes %d-%d/%d'); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$total = $end = (int) $this->request->header('content-length'); |
94
|
|
|
|
95
|
1 |
|
return [0, $end, $total]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.