diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2014-07-08 09:44:46 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2014-07-08 11:09:59 +0200 |
commit | ea269f0067dda7cee0621443a9620607297b0387 (patch) | |
tree | 1693bb21b1e12157032ecac82881f6e923ad41a4 /lib | |
parent | 8da44f2a5be6e768fd554c9e8f5b981997e869db (diff) | |
download | nextcloud-server-ea269f0067dda7cee0621443a9620607297b0387.tar.gz nextcloud-server-ea269f0067dda7cee0621443a9620607297b0387.zip |
Upload abortion is now detected within the OC_Connector_Sabre_File::put()
OC_Connector_Sabre_AbortedUploadDetectionPlugin is pointless
Adding unit test testUploadAbort()
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/connector/sabre/aborteduploaddetectionplugin.php | 98 | ||||
-rw-r--r-- | lib/private/connector/sabre/file.php | 20 |
2 files changed, 14 insertions, 104 deletions
diff --git a/lib/private/connector/sabre/aborteduploaddetectionplugin.php b/lib/private/connector/sabre/aborteduploaddetectionplugin.php deleted file mode 100644 index b569f9a83c3..00000000000 --- a/lib/private/connector/sabre/aborteduploaddetectionplugin.php +++ /dev/null @@ -1,98 +0,0 @@ -<?php -/** - * Copyright (c) 2013 Thomas Müller <thomas.mueller@tmit.eu> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -/** - * Class OC_Connector_Sabre_AbortedUploadDetectionPlugin - * - * This plugin will verify if the uploaded data has been stored completely. - * This is done by comparing the content length of the request with the file size on storage. - */ -class OC_Connector_Sabre_AbortedUploadDetectionPlugin extends \Sabre\DAV\ServerPlugin { - - /** - * Reference to main server object - * - * @var \Sabre\DAV\Server - */ - private $server; - - /** - * @var \OC\Files\View - */ - private $fileView; - - /** - * @param \OC\Files\View $view - */ - public function __construct($view) { - $this->fileView = $view; - } - - /** - * This initializes the plugin. - * - * This function is called by \Sabre\DAV\Server, after - * addPlugin is called. - * - * This method should set up the requires event subscriptions. - * - * @param \Sabre\DAV\Server $server - */ - public function initialize(\Sabre\DAV\Server $server) { - - $this->server = $server; - - $server->subscribeEvent('afterCreateFile', array($this, 'verifyContentLength'), 10); - $server->subscribeEvent('afterWriteContent', array($this, 'verifyContentLength'), 10); - } - - /** - * @param string $filePath - * @param \Sabre\DAV\INode $node - * @throws \Sabre\DAV\Exception\BadRequest - */ - public function verifyContentLength($filePath, \Sabre\DAV\INode $node = null) { - - // we should only react on PUT which is used for upload - // e.g. with LOCK this will not work, but LOCK uses createFile() as well - if ($this->server->httpRequest->getMethod() !== 'PUT') { - return; - } - - // ownCloud chunked upload will be handled in its own plugin - $chunkHeader = $this->server->httpRequest->getHeader('OC-Chunked'); - if ($chunkHeader) { - return; - } - - // compare expected and actual size - $expected = $this->getLength(); - if (!$expected) { - return; - } - $actual = $this->fileView->filesize($filePath); - if ($actual != $expected) { - $this->fileView->unlink($filePath); - throw new \Sabre\DAV\Exception\BadRequest('expected filesize ' . $expected . ' got ' . $actual); - } - - } - - /** - * @return string - */ - public function getLength() { - $req = $this->server->httpRequest; - $length = $req->getHeader('X-Expected-Entity-Length'); - if (!$length) { - $length = $req->getHeader('Content-Length'); - } - - return $length; - } -} diff --git a/lib/private/connector/sabre/file.php b/lib/private/connector/sabre/file.php index 7591cc5c066..ece6885f24f 100644 --- a/lib/private/connector/sabre/file.php +++ b/lib/private/connector/sabre/file.php @@ -71,13 +71,13 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\ } // mark file as partial while uploading (ignored by the scanner) - $partpath = $this->path . '.ocTransferId' . rand() . '.part'; + $partFilePath = $this->path . '.ocTransferId' . rand() . '.part'; try { - $putOkay = $this->fileView->file_put_contents($partpath, $data); + $putOkay = $this->fileView->file_put_contents($partFilePath, $data); if ($putOkay === false) { \OC_Log::write('webdav', '\OC\Files\Filesystem::file_put_contents() failed', \OC_Log::ERROR); - $this->fileView->unlink($partpath); + $this->fileView->unlink($partFilePath); // because we have no clue about the cause we can only throw back a 500/Internal Server Error throw new \Sabre\DAV\Exception('Could not write file contents'); } @@ -102,13 +102,22 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\ throw new OC_Connector_Sabre_Exception_FileLocked($e->getMessage(), $e->getCode(), $e); } + // double check if the file was fully received + // compare expected and actual size + $expected = $_SERVER['CONTENT_LENGTH']; + $actual = $this->fileView->filesize($partFilePath); + if ($actual != $expected) { + $this->fileView->unlink($partFilePath); + throw new \Sabre\DAV\Exception\BadRequest('expected filesize ' . $expected . ' got ' . $actual); + } + // rename to correct path try { - $renameOkay = $this->fileView->rename($partpath, $this->path); + $renameOkay = $this->fileView->rename($partFilePath, $this->path); $fileExists = $this->fileView->file_exists($this->path); if ($renameOkay === false || $fileExists === false) { \OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR); - $this->fileView->unlink($partpath); + $this->fileView->unlink($partFilePath); throw new \Sabre\DAV\Exception('Could not rename part file to final file'); } } @@ -259,5 +268,4 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements \Sabre\ return null; } - } |