Skip to content

Commit b675d23

Browse files
committed
Merge branch 'master' of github.com:dilab/resumable.php
2 parents 9777e22 + d98d5f2 commit b675d23

File tree

10 files changed

+38
-19
lines changed

10 files changed

+38
-19
lines changed

src/Network/SimpleResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function header($statusCode)
1616
} else if (404==$statusCode) {
1717
return header("HTTP/1.0 404 Not Found");
1818
}
19-
return header("HTTP/1.0 404 Not Found");
19+
return header("HTTP/1.0 204 No Content");
2020
}
2121

2222
}

src/Resumable.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,11 @@ public function handleTestChunk()
176176
$chunkNumber = $this->resumableParam($this->resumableOption['chunkNumber']);
177177

178178
if (!$this->isChunkUploaded($identifier, $filename, $chunkNumber)) {
179-
return $this->response->header(404);
179+
return $this->response->header(204);
180+
} else {
181+
return $this->response->header(200);
180182
}
181183

182-
return $this->response->header(200);
183184
}
184185

185186
public function handleChunk()
@@ -279,7 +280,17 @@ public function tmpChunkDir($identifier)
279280

280281
public function tmpChunkFilename($filename, $chunkNumber)
281282
{
282-
return $filename . '.part' . $chunkNumber;
283+
return $filename . '.' . str_pad($chunkNumber, 4, 0, STR_PAD_LEFT);
284+
}
285+
286+
public function getExclusiveFileHandle($name)
287+
{
288+
// if the file exists, fopen() will raise a warning
289+
$previous_error_level = error_reporting();
290+
error_reporting(E_ERROR);
291+
$handle = fopen($name, 'x');
292+
error_reporting($previous_error_level);
293+
return $handle;
283294
}
284295

285296
public function createFileFromChunks($chunkFiles, $destFile)
@@ -288,7 +299,13 @@ public function createFileFromChunks($chunkFiles, $destFile)
288299

289300
natsort($chunkFiles);
290301

291-
$destFile = new File($destFile, true);
302+
$handle = $this->getExclusiveFileHandle($destFile);
303+
if (!$handle) {
304+
return false;
305+
}
306+
307+
$destFile = new File($destFile);
308+
$destFile->handle = $handle;
292309
foreach ($chunkFiles as $chunkFile) {
293310
$file = new File($chunkFile);
294311
$destFile->append($file->read());
@@ -342,3 +359,4 @@ private function removeExtension($filename)
342359
return str_replace(sprintf('.%s', $ext), '', $filename);
343360
}
344361
}
362+
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/src/Network/SimpleRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testFile()
7676
$file = array(
7777
'name'=> 'mock.png',
7878
'type'=> 'application/octet-stream',
79-
'tmp_name'=> 'test/files/mock.png.part3',
79+
'tmp_name'=> 'test/files/mock.png.0003',
8080
'error'=> 0,
8181
'size'=> 1048576,
8282
);

test/src/Network/SimpleResponseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public function headerProvider()
2626
{
2727
return array(
2828
array(404,404),
29+
array(204,204),
2930
array(200,200),
30-
array(500,404),
31+
array(500,204),
3132
);
3233
}
3334

test/src/ResumableTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testProcessHandleChunk()
5050
$this->request->method('file')
5151
->will($this->returnValue(array(
5252
'name'=> 'mock.png',
53-
'tmp_name'=> 'test/files/mock.png.part3',
53+
'tmp_name'=> 'test/files/mock.png.003',
5454
'error'=> 0,
5555
'size'=> 27000,
5656
)));
@@ -142,7 +142,7 @@ public function testHandleChunk() {
142142
$this->request->method('file')
143143
->willReturn(array(
144144
'name'=> 'mock.png',
145-
'tmp_name'=> 'test/files/mock.png.part3',
145+
'tmp_name'=> 'test/files/mock.png.003',
146146
'error'=> 0,
147147
'size'=> 27000,
148148
));
@@ -154,7 +154,7 @@ public function testHandleChunk() {
154154
$this->resumbable->handleChunk();
155155

156156
$this->assertFileExists('test/uploads/mock.png');
157-
unlink('test/tmp/identifier/mock.png.part3');
157+
file_exists('test/tmp/identifier/mock.png.003') && unlink('test/tmp/identifier/mock.png.003');
158158
unlink('test/uploads/mock.png');
159159
}
160160

@@ -226,22 +226,22 @@ public function testTmpChunkFile()
226226
{
227227
$this->resumbable = new Resumable($this->request,$this->response);
228228
$filename = 'mock-file.png';
229-
$chunkNumber = 1;
230-
$expected = $filename.'.part'.$chunkNumber;
229+
$chunkNumber = str_pad(1, 4, 0, STR_PAD_LEFT);
230+
$expected = $filename.'.'.$chunkNumber;
231231
$this->assertEquals($expected, $this->resumbable->tmpChunkFilename($filename,$chunkNumber));
232232
}
233233

234234
public function testCreateFileFromChunks()
235235
{
236236
$files = array(
237-
'test/files/mock.png.part1',
238-
'test/files/mock.png.part2',
239-
'test/files/mock.png.part3',
237+
'test/files/mock.png.0001',
238+
'test/files/mock.png.0002',
239+
'test/files/mock.png.0003',
240240
);
241241
$totalFileSize = array_sum(array(
242-
filesize('test/files/mock.png.part1'),
243-
filesize('test/files/mock.png.part2'),
244-
filesize('test/files/mock.png.part3')
242+
filesize('test/files/mock.png.0001'),
243+
filesize('test/files/mock.png.0002'),
244+
filesize('test/files/mock.png.0003')
245245
));
246246
$destFile = 'test/files/5.png';
247247

@@ -256,7 +256,7 @@ public function testMoveUploadedFile()
256256
{
257257
$destFile = 'test/files/4.png';
258258
$this->resumbable = new Resumable($this->request,$this->response);
259-
$this->resumbable->moveUploadedFile('test/files/mock.png.part1', $destFile);
259+
$this->resumbable->moveUploadedFile('test/files/mock.png.0001', $destFile);
260260
$this->assertFileExists($destFile);
261261
unlink($destFile);
262262
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)