improve chunked upload for Owncloud client
authorLars Kneschke <l.kneschke@metaways.de>
Mon, 12 May 2014 11:14:43 +0000 (13:14 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 4 Sep 2014 09:26:35 +0000 (11:26 +0200)
Change-Id: Idb40d883089d88b33545751816b2b19ed1a91630

Reviewed-on: http://gerrit.tine20.com/customers/627
Reviewed-by: Lars Kneschke <l.kneschke@metaways.de>
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/Frontend/WebDAV/Directory.php

index 508494f..9f79a77 100644 (file)
@@ -194,22 +194,20 @@ class Tinebase_Frontend_WebDAV_Directory extends Tinebase_Frontend_WebDAV_Node i
      */
     public static function handleOwnCloudChunkedFileUpload($name, $data)
     {
-        $matched = preg_match('/(?P<name>.*)-chunking-(?P<tempId>\d+)-(?P<totalCount>\d+)-(?P<chunkId>\d+)/', $name, $chunkInfo);
+        if (!isset($_SERVER['CONTENT_LENGTH'])) {
+            throw new \Sabre\DAV\Exception\BadRequest('CONTENT_LENGTH header missing!');
+        }
         
+        $matched = preg_match('/(?P<name>.*)-chunking-(?P<tempId>\d+)-(?P<totalCount>\d+)-(?P<chunkId>\d+)/', $name, $chunkInfo);
         if (!$matched) {
             throw new \Sabre\DAV\Exception\BadRequest('bad filename provided: ' . $name);
         }
         
-        $stat = fstat($data);
-        
-        if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] != $stat['size']) {
-            throw new \Sabre\DAV\Exception\BadRequest('uploaded part incomplete! expected size of: ' . $_SERVER['CONTENT_LENGTH'] . ' got: ' . $stat['size']);
-        }
-        
         // copy chunk to temp file
         $path = Tinebase_TempFile::getTempPath();
         
         $tempfileHandle = fopen($path, "w");
+        
         if (! $tempfileHandle) {
             throw new \Sabre\DAV\Exception\BadRequest('Could not open tempfile while uploading! ');
         }
@@ -218,6 +216,12 @@ class Tinebase_Frontend_WebDAV_Directory extends Tinebase_Frontend_WebDAV_Node i
             stream_copy_to_stream($data, $tempfileHandle);
         } while (!feof($data));
         
+        $stat = fstat($tempfileHandle);
+        
+        if ($_SERVER['CONTENT_LENGTH'] != $stat['size']) {
+            throw new \Sabre\DAV\Exception\BadRequest('uploaded part incomplete! expected size of: ' . $_SERVER['CONTENT_LENGTH'] . ' got: ' . $stat['size']);
+        }
+        
         fclose($tempfileHandle);
         
         $tempFileName = sha1(Tinebase_Core::getUser()->accountId . $chunkInfo['name'] . $chunkInfo['tempId']);