forked from recca0120/upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunkFile.php
229 lines (198 loc) · 4.25 KB
/
ChunkFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
namespace Recca0120\Upload;
use Recca0120\Upload\Exceptions\ChunkedResponseException;
class ChunkFile
{
/**
* TMPFILE_EXTENSION.
*
* @var string
*/
const TMPFILE_EXTENSION = '.part';
/**
* $files.
*
* @var \Recca0120\Upload\Filesystem
*/
protected $files;
/**
* $token.
*
* @var string
*/
protected $token = null;
/**
* $chunkPath.
*
* @var string
*/
protected $chunkPath = null;
/**
* $storagePath.
*
* @var string
*/
protected $storagePath = null;
/**
* $name.
*
* @var string
*/
protected $name = null;
/**
* $mimeType.
*
* @var string
*/
protected $mimeType = null;
/**
* $tmpfilename.
*
* @var string
*/
protected $tmpfilename = null;
/**
* __construct.
*
* @param \Recca0120\Upload\Filesystem $files
*/
public function __construct(Filesystem $files = null)
{
$this->files = $files ?: new Filesystem();
}
/**
* setToken.
*
* @param string $token
* @return $this
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* setChunkPath.
*
* @param string $chunkPath
* @return $this
*/
public function setChunkPath($chunkPath)
{
$this->chunkPath = $chunkPath;
return $this;
}
/**
* setStoragePath.
*
* @param string $storagePath
* @return $this
*/
public function setStoragePath($storagePath)
{
$this->storagePath = $storagePath;
return $this;
}
/**
* setName.
*
* @param string $name
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* setMimeType.
*
* @param string $mimeType
* @return $this
*/
public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
return $this;
}
/**
* throwException.
*
* @param mixed $message
* @param array $headers
* @throws \Recca0120\Upload\Exceptions\ChunkedResponseException
*/
public function throwException($message = '', $headers = [])
{
throw new ChunkedResponseException($message, $headers);
}
/**
* appendStream.
*
* @param mixed $source
* @param int $offset
* @return $this
*/
public function appendStream($source, $offset = 0, $index = null)
{
$chunkFile = $this->chunkFile();
$chunkFile = is_null($index) === false ? $chunkFile .= '.'.$index : $chunkFile;
$this->files->appendStream($chunkFile, $source, (int) $offset);
return $this;
}
/**
* createUploadedFile.
*
* @return \Illuminate\Http\UploadedFile
*/
public function createUploadedFile($chunks = null, $storageFile = null)
{
$chunkFile = $this->chunkFile();
$storageFile = $storageFile ?: $this->storageFile();
if (is_null($chunks) === false) {
for ($i = 0; $i < $chunks; $i++) {
$chunk = $chunkFile.'.'.$i;
$this->files->append(
$storageFile,
$this->files->get($chunk)
);
$this->files->delete($chunk);
}
} else {
$this->files->move($chunkFile, $storageFile);
}
return $this->files->createUploadedFile(
$storageFile, $this->name, $this->mimeType
);
}
/**
* tmpfilename.
*
* @return string
*/
protected function tmpfilename()
{
if (is_null($this->tmpfilename) === true) {
$this->tmpfilename = $this->files->tmpfilename($this->name, $this->token);
}
return $this->tmpfilename;
}
/**
* chunkFile.
*
* @return string
*/
protected function chunkFile()
{
return $this->chunkPath.$this->tmpfilename().static::TMPFILE_EXTENSION;
}
/**
* storageFile.
*
* @return string
*/
protected function storageFile()
{
return $this->storagePath.$this->tmpfilename();
}
}