1 | <?php |
||
7 | class ChunkFile |
||
8 | { |
||
9 | /** |
||
10 | * TMPFILE_EXTENSION. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | const TMPFILE_EXTENSION = '.part'; |
||
15 | |||
16 | /** |
||
17 | * $files. |
||
18 | * |
||
19 | * @var \Recca0120\Upload\Filesystem |
||
20 | */ |
||
21 | protected $files; |
||
22 | |||
23 | /** |
||
24 | * $token. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $token = null; |
||
29 | |||
30 | /** |
||
31 | * $chunkPath. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $chunkPath = null; |
||
36 | |||
37 | /** |
||
38 | * $storagePath. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $storagePath = null; |
||
43 | |||
44 | /** |
||
45 | * $name. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $name = null; |
||
50 | |||
51 | /** |
||
52 | * $mimeType. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $mimeType = null; |
||
57 | |||
58 | /** |
||
59 | * $tmpfilename. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $tmpfilename = null; |
||
64 | |||
65 | /** |
||
66 | * __construct. |
||
67 | * |
||
68 | * @param \Recca0120\Upload\Filesystem $files |
||
69 | */ |
||
70 | 14 | public function __construct(Filesystem $files = null) |
|
74 | |||
75 | /** |
||
76 | * setToken. |
||
77 | * |
||
78 | * @param string $token |
||
79 | * @return $this |
||
80 | */ |
||
81 | 3 | public function setToken($token) |
|
87 | |||
88 | /** |
||
89 | * setChunkPath. |
||
90 | * |
||
91 | * @param string $chunkPath |
||
92 | * @return $this |
||
93 | */ |
||
94 | 3 | public function setChunkPath($chunkPath) |
|
100 | |||
101 | /** |
||
102 | * setStoragePath. |
||
103 | * |
||
104 | * @param string $storagePath |
||
105 | * @return $this |
||
106 | */ |
||
107 | 1 | public function setStoragePath($storagePath) |
|
113 | |||
114 | /** |
||
115 | * setName. |
||
116 | * |
||
117 | * @param string $name |
||
118 | * @return $this |
||
119 | */ |
||
120 | 3 | public function setName($name) |
|
121 | { |
||
122 | 3 | $this->name = $name; |
|
123 | |||
124 | 3 | return $this; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * setMimeType. |
||
129 | * |
||
130 | * @param string $mimeType |
||
131 | * @return $this |
||
132 | */ |
||
133 | 1 | public function setMimeType($mimeType) |
|
139 | |||
140 | /** |
||
141 | * throwException. |
||
142 | * |
||
143 | * @param mixed $message |
||
144 | * @param array $headers |
||
145 | * @throws \Recca0120\Upload\Exceptions\ChunkedResponseException |
||
146 | */ |
||
147 | 3 | public function throwException($message = '', $headers = []) |
|
151 | |||
152 | /** |
||
153 | * appendStream. |
||
154 | * |
||
155 | * @param mixed $source |
||
156 | * @param int $offset |
||
157 | * @return $this |
||
158 | */ |
||
159 | 1 | public function appendStream($source, $offset = 0) |
|
166 | |||
167 | /** |
||
168 | * appendFile. |
||
169 | * |
||
170 | * @param mixed $source |
||
171 | * @param int $index |
||
172 | * @return $this |
||
173 | */ |
||
174 | 1 | public function appendFile($source, $index = 0) |
|
181 | |||
182 | /** |
||
183 | * createUploadedFile. |
||
184 | * |
||
185 | * @return \Illuminate\Http\UploadedFile |
||
186 | */ |
||
187 | 1 | public function createUploadedFile($chunks = null, $storageFile = null) |
|
188 | { |
||
189 | 1 | $chunkFile = $this->chunkFile(); |
|
190 | 1 | $storageFile = $storageFile ?: $this->storageFile(); |
|
191 | |||
192 | 1 | if (is_null($chunks) === false) { |
|
193 | for ($i = 0; $i < $chunks; $i++) { |
||
194 | $chunk = $chunkFile.'.'.$i; |
||
195 | $this->files->append( |
||
196 | $storageFile, |
||
197 | $this->files->get($chunk) |
||
198 | ); |
||
199 | $this->files->delete($chunk); |
||
200 | } |
||
201 | } else { |
||
202 | 1 | $this->files->move($chunkFile, $storageFile); |
|
203 | } |
||
204 | |||
205 | 1 | return $this->files->createUploadedFile( |
|
206 | 1 | $storageFile, $this->name, $this->mimeType |
|
207 | 1 | ); |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * tmpfilename. |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | 3 | protected function tmpfilename() |
|
216 | { |
||
217 | 3 | if (is_null($this->tmpfilename) === true) { |
|
218 | 3 | $this->tmpfilename = $this->files->tmpfilename($this->name, $this->token); |
|
219 | 3 | } |
|
220 | |||
221 | 3 | return $this->tmpfilename; |
|
222 | } |
||
223 | |||
224 | /** |
||
225 | * chunkFile. |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 3 | protected function chunkFile() |
|
233 | |||
234 | /** |
||
235 | * storageFile. |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | 1 | protected function storageFile() |
|
243 | } |
||
244 |