Kaynağa Gözat

Merge branch 'master' of https://gogs.tamadco.com:3000/omidrezav/resumablephp

omidrezav 5 yıl önce
ebeveyn
işleme
7c3301b1ce

+ 3 - 1
readme.md

@@ -1,4 +1,4 @@
-# PHP backend for resumable file upload
+# PHP backend for resumable.js
 
 
 ## Installation
@@ -35,10 +35,12 @@ $resumable->process();
 ```
 // custom filename (extension from original file will be magically removed and re-appended)
 $originalName = $resumable->getOriginalFilename(Resumable::WITHOUT_EXTENSION); // will gove you "original Name" instead of "original Name.png"
+
 // do some slugification or whatever you need...
 $slugifiedname = my_slugify($originalName); // this is up to you, it as ported out of the library.
 $resumable->setFilename($slugifiedname);
 
+// process upload as normal
 $resumable->process();
 
 // you can also get file information after the upload is complete

+ 1 - 1
src/Network/SimpleResponse.php

@@ -16,7 +16,7 @@ class SimpleResponse implements Response
         } else if (404==$statusCode) {
             return header("HTTP/1.0 404 Not Found");
         }
-        return header("HTTP/1.0 404 Not Found");
+        return header("HTTP/1.0 204 No Content");
     }
 
 }

+ 22 - 4
src/Resumable.php

@@ -176,10 +176,11 @@ class Resumable
         $chunkNumber = $this->resumableParam($this->resumableOption['chunkNumber']);
 
         if (!$this->isChunkUploaded($identifier, $filename, $chunkNumber)) {
-            return $this->response->header(404);
+            return $this->response->header(204);
+        } else {
+            return $this->response->header(200);
         }
 
-        return $this->response->header(200);
     }
 
     public function handleChunk()
@@ -328,7 +329,17 @@ class Resumable
 
     public function tmpChunkFilename($filename, $chunkNumber)
     {
-        return $filename . '.part' . $chunkNumber;
+        return $filename . '.' . str_pad($chunkNumber, 4, 0, STR_PAD_LEFT);
+    }
+
+    public function getExclusiveFileHandle($name)
+    {
+        // if the file exists, fopen() will raise a warning
+        $previous_error_level = error_reporting();
+        error_reporting(E_ERROR);
+        $handle = fopen($name, 'x');
+        error_reporting($previous_error_level);
+        return $handle;
     }
 
     public function createFileFromChunks($chunkFiles, $destFile)
@@ -337,7 +348,13 @@ class Resumable
 
         natsort($chunkFiles);
 
-        $destFile = new File($destFile, true);
+        $handle = $this->getExclusiveFileHandle($destFile);
+        if (!$handle) {
+            return false;
+        }
+
+        $destFile = new File($destFile);
+        $destFile->handle = $handle;
         foreach ($chunkFiles as $chunkFile) {
             $file = new File($chunkFile);
             $destFile->append($file->read());
@@ -391,3 +408,4 @@ class Resumable
         return str_replace(sprintf('.%s', $ext), '', $filename);
     }
 }
+

+ 0 - 0
test/files/mock.png.part1 → test/files/mock.png.0001


+ 0 - 0
test/files/mock.png.part2 → test/files/mock.png.0002


+ 0 - 0
test/files/mock.png.part3 → test/files/mock.png.0003


+ 1 - 1
test/src/Network/SimpleRequestTest.php

@@ -76,7 +76,7 @@ class SimpleRequestTest extends \PHPUnit_Framework_TestCase
        $file = array(
            'name'=> 'mock.png',
            'type'=> 'application/octet-stream',
-           'tmp_name'=>  'test/files/mock.png.part3',
+           'tmp_name'=>  'test/files/mock.png.0003',
            'error'=> 0,
            'size'=> 1048576,
        );

+ 2 - 1
test/src/Network/SimpleResponseTest.php

@@ -26,8 +26,9 @@ class SimpleResponseTest extends \PHPUnit_Framework_TestCase
     {
         return array(
             array(404,404),
+            array(204,204),
             array(200,200),
-            array(500,404),
+            array(500,204),
         );
     }
 

+ 12 - 12
test/src/ResumableTest.php

@@ -50,7 +50,7 @@ class ResumbableTest extends \PHPUnit_Framework_TestCase
         $this->request->method('file')
                     ->will($this->returnValue(array(
                             'name'=> 'mock.png',
-                            'tmp_name'=>  'test/files/mock.png.part3',
+                            'tmp_name'=>  'test/files/mock.png.003',
                             'error'=> 0,
                             'size'=> 27000,
                         )));
@@ -142,7 +142,7 @@ class ResumbableTest extends \PHPUnit_Framework_TestCase
         $this->request->method('file')
                 ->willReturn(array(
                     'name'=> 'mock.png',
-                    'tmp_name'=>  'test/files/mock.png.part3',
+                    'tmp_name'=>  'test/files/mock.png.003',
                     'error'=> 0,
                     'size'=> 27000,
                 ));
@@ -154,7 +154,7 @@ class ResumbableTest extends \PHPUnit_Framework_TestCase
         $this->resumbable->handleChunk();
 
         $this->assertFileExists('test/uploads/mock.png');
-        unlink('test/tmp/identifier/mock.png.part3');
+        file_exists('test/tmp/identifier/mock.png.003') && unlink('test/tmp/identifier/mock.png.003');
         unlink('test/uploads/mock.png');
     }
 
@@ -226,22 +226,22 @@ class ResumbableTest extends \PHPUnit_Framework_TestCase
     {
         $this->resumbable = new Resumable($this->request,$this->response);
         $filename = 'mock-file.png';
-        $chunkNumber = 1;
-        $expected = $filename.'.part'.$chunkNumber;
+        $chunkNumber = str_pad(1, 4, 0, STR_PAD_LEFT);
+        $expected = $filename.'.'.$chunkNumber;
         $this->assertEquals($expected, $this->resumbable->tmpChunkFilename($filename,$chunkNumber));
     }
 
     public function testCreateFileFromChunks()
     {
         $files = array(
-            'test/files/mock.png.part1',
-            'test/files/mock.png.part2',
-            'test/files/mock.png.part3',
+            'test/files/mock.png.0001',
+            'test/files/mock.png.0002',
+            'test/files/mock.png.0003',
         );
         $totalFileSize = array_sum(array(
-            filesize('test/files/mock.png.part1'),
-            filesize('test/files/mock.png.part2'),
-            filesize('test/files/mock.png.part3')
+            filesize('test/files/mock.png.0001'),
+            filesize('test/files/mock.png.0002'),
+            filesize('test/files/mock.png.0003')
         ));
         $destFile = 'test/files/5.png';
 
@@ -256,7 +256,7 @@ class ResumbableTest extends \PHPUnit_Framework_TestCase
     {
         $destFile = 'test/files/4.png';
         $this->resumbable = new Resumable($this->request,$this->response);
-        $this->resumbable->moveUploadedFile('test/files/mock.png.part1', $destFile);
+        $this->resumbable->moveUploadedFile('test/files/mock.png.0001', $destFile);
         $this->assertFileExists($destFile);
         unlink($destFile);
     }

+ 0 - 0
test/tmp/identifier/mock.png.part1 → test/tmp/identifier/mock.png.0001


+ 0 - 0
test/tmp/identifier/mock.png.part2 → test/tmp/identifier/mock.png.0002