|
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
|
|
|
|
|
75
|
3 |
|
if (empty($mimeType) === true) { |
|
76
|
|
|
$mimeType = $this->files->mimeType($originalName); |
|
77
|
|
|
|
|
78
|
|
|
if ($mimeType === false) { |
|
79
|
|
|
return ''; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
return $mimeType; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* parseContentRange. |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
3 |
|
protected function parseContentRange() |
|
92
|
|
|
{ |
|
93
|
3 |
|
$contentRange = $this->request->header('content-range'); |
|
94
|
3 |
|
if (empty($contentRange) === false) { |
|
95
|
2 |
|
return sscanf($contentRange, 'bytes %d-%d/%d'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
$total = $end = (int) $this->request->header('content-length'); |
|
99
|
|
|
|
|
100
|
1 |
|
return [0, $end, $total]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|