1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Upload; |
4
|
|
|
|
5
|
|
|
use Recca0120\Upload\Exceptions\ChunkedResponseException; |
6
|
|
|
|
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
|
13 |
|
public function __construct(Filesystem $files = null) |
71
|
|
|
{ |
72
|
13 |
|
$this->files = $files ?: new Filesystem(); |
73
|
13 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* setToken. |
77
|
|
|
* |
78
|
|
|
* @param string $token |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
2 |
|
public function setToken($token) |
82
|
|
|
{ |
83
|
2 |
|
$this->token = $token; |
84
|
|
|
|
85
|
2 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* setChunkPath. |
90
|
|
|
* |
91
|
|
|
* @param string $chunkPath |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
2 |
|
public function setChunkPath($chunkPath) |
95
|
|
|
{ |
96
|
2 |
|
$this->chunkPath = $chunkPath; |
97
|
|
|
|
98
|
2 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* setStoragePath. |
103
|
|
|
* |
104
|
|
|
* @param string $storagePath |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
1 |
|
public function setStoragePath($storagePath) |
108
|
|
|
{ |
109
|
1 |
|
$this->storagePath = $storagePath; |
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* setName. |
116
|
|
|
* |
117
|
|
|
* @param string $name |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
2 |
|
public function setName($name) |
121
|
|
|
{ |
122
|
2 |
|
$this->name = $name; |
123
|
|
|
|
124
|
2 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* setMimeType. |
129
|
|
|
* |
130
|
|
|
* @param string $mimeType |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
1 |
|
public function setMimeType($mimeType) |
134
|
|
|
{ |
135
|
1 |
|
$this->mimeType = $mimeType; |
136
|
|
|
|
137
|
1 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* throwException. |
142
|
|
|
* |
143
|
|
|
* @param mixed $message |
144
|
|
|
* @param array $headers |
145
|
|
|
* @throws \Recca0120\Upload\Exceptions\ChunkedResponseException |
146
|
|
|
*/ |
147
|
2 |
|
public function throwException($message = '', $headers = []) |
148
|
|
|
{ |
149
|
2 |
|
throw new ChunkedResponseException($message, $headers); |
150
|
|
|
} |
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, $index = null) |
160
|
|
|
{ |
161
|
1 |
|
$chunkFile = $this->chunkFile(); |
162
|
1 |
|
$chunkFile = is_null($index) === false ? $chunkFile .= '.'.$index : $chunkFile; |
163
|
1 |
|
$this->files->appendStream($chunkFile, $source, (int) $offset); |
164
|
|
|
|
165
|
1 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* createUploadedFile. |
170
|
|
|
* |
171
|
|
|
* @return \Illuminate\Http\UploadedFile |
172
|
|
|
*/ |
173
|
1 |
|
public function createUploadedFile($chunks = null, $storageFile = null) |
174
|
|
|
{ |
175
|
1 |
|
$chunkFile = $this->chunkFile(); |
176
|
1 |
|
$storageFile = $storageFile ?: $this->storageFile(); |
177
|
|
|
|
178
|
1 |
|
if (is_null($chunks) === false) { |
179
|
|
|
for ($i = 0; $i < $chunks; $i++) { |
180
|
|
|
$chunk = $chunkFile.'.'.$i; |
181
|
|
|
$this->files->append( |
182
|
|
|
$storageFile, |
183
|
|
|
$this->files->get($chunk) |
184
|
|
|
); |
185
|
|
|
$this->files->delete($chunk); |
186
|
|
|
} |
187
|
|
|
} else { |
188
|
1 |
|
$this->files->move($chunkFile, $storageFile); |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
return $this->files->createUploadedFile( |
192
|
1 |
|
$storageFile, $this->name, $this->mimeType |
193
|
1 |
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* tmpfilename. |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
2 |
|
protected function tmpfilename() |
202
|
|
|
{ |
203
|
2 |
|
if (is_null($this->tmpfilename) === true) { |
204
|
2 |
|
$this->tmpfilename = $this->files->tmpfilename($this->name, $this->token); |
205
|
2 |
|
} |
206
|
|
|
|
207
|
2 |
|
return $this->tmpfilename; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* chunkFile. |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
2 |
|
protected function chunkFile() |
216
|
|
|
{ |
217
|
2 |
|
return $this->chunkPath.$this->tmpfilename().static::TMPFILE_EXTENSION; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* storageFile. |
222
|
|
|
* |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
1 |
|
protected function storageFile() |
226
|
|
|
{ |
227
|
1 |
|
return $this->storagePath.$this->tmpfilename(); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|