diff options
-rw-r--r-- | lib/private/connector/sabre/file.php | 96 | ||||
-rw-r--r-- | tests/lib/connector/sabre/file.php | 34 |
2 files changed, 86 insertions, 44 deletions
diff --git a/lib/private/connector/sabre/file.php b/lib/private/connector/sabre/file.php index b4acaa15073..dc678c0894f 100644 --- a/lib/private/connector/sabre/file.php +++ b/lib/private/connector/sabre/file.php @@ -83,12 +83,12 @@ class File extends Node implements IFile { */ public function put($data) { try { - if ($this->info && $this->fileView->file_exists($this->path) && - !$this->info->isUpdateable()) { + $exists = $this->fileView->file_exists($this->path); + if ($this->info && $exists && !$this->info->isUpdateable()) { throw new Forbidden(); } } catch (StorageNotAvailableException $e) { - throw new ServiceUnavailable("File is not updatable: ".$e->getMessage()); + throw new ServiceUnavailable("File is not updatable: " . $e->getMessage()); } // verify path of the target @@ -110,14 +110,31 @@ class File extends Node implements IFile { $partFilePath = $this->path; } + /** @var \OC\Files\Storage\Storage $storage */ + list($storage, $internalPartPath) = $this->fileView->resolvePath($partFilePath); + list(, $internalPath) = $this->fileView->resolvePath($this->path); try { - $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); + $target = $storage->fopen($internalPartPath, 'wb'); + if ($target === false) { + \OC_Log::write('webdav', '\OC\Files\Filesystem::fopen() failed', \OC_Log::ERROR); $this->fileView->unlink($partFilePath); // because we have no clue about the cause we can only throw back a 500/Internal Server Error throw new Exception('Could not write file contents'); } + list($count, ) = \OC_Helper::streamCopy($data, $target); + fclose($target); + + // if content length is sent by client: + // double check if the file was fully received + // compare expected and actual size + if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] !== 'LOCK') { + $expected = $_SERVER['CONTENT_LENGTH']; + if ($count != $expected) { + $storage->unlink($internalPartPath); + throw new BadRequest('expected filesize ' . $expected . ' got ' . $count); + } + } + } catch (NotPermittedException $e) { // a more general case - due to whatever reason the content could not be written throw new Forbidden($e->getMessage()); @@ -138,49 +155,56 @@ class File extends Node implements IFile { // returning 503 will allow retry of the operation at a later point in time throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); } catch (StorageNotAvailableException $e) { - throw new ServiceUnavailable("Failed to write file contents: ".$e->getMessage()); + throw new ServiceUnavailable("Failed to write file contents: " . $e->getMessage()); } try { - // if content length is sent by client: - // double check if the file was fully received - // compare expected and actual size - if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] !== 'LOCK') { - $expected = $_SERVER['CONTENT_LENGTH']; - $actual = $this->fileView->filesize($partFilePath); - if ($actual != $expected) { - $this->fileView->unlink($partFilePath); - throw new BadRequest('expected filesize ' . $expected . ' got ' . $actual); - } - } - if ($needsPartFile) { // rename to correct path try { - $renameOkay = $this->fileView->rename($partFilePath, $this->path); - $fileExists = $this->fileView->file_exists($this->path); + $renameOkay = $storage->rename($internalPartPath, $internalPath); + $fileExists = $storage->file_exists($internalPath); if ($renameOkay === false || $fileExists === false) { \OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR); $this->fileView->unlink($partFilePath); throw new Exception('Could not rename part file to final file'); } - } - catch (LockNotAcquiredException $e) { + } catch (\OCP\Files\LockNotAcquiredException $e) { // the file is currently being written to by another process throw new FileLocked($e->getMessage(), $e->getCode(), $e); } } + // since we skipped the view we need to scan and emit the hooks ourselves + $storage->getScanner()->scanFile($internalPath); + + $view = \OC\Files\Filesystem::getView(); + if ($view) { + $hookPath = $view->getRelativePath($this->fileView->getAbsolutePath($this->path)); + if (!$exists) { + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, array( + \OC\Files\Filesystem::signal_param_path => $hookPath + )); + } else { + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, array( + \OC\Files\Filesystem::signal_param_path => $hookPath + )); + } + \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, array( + \OC\Files\Filesystem::signal_param_path => $hookPath + )); + } + // allow sync clients to send the mtime along in a header $request = \OC::$server->getRequest(); if (isset($request->server['HTTP_X_OC_MTIME'])) { - if($this->fileView->touch($this->path, $request->server['HTTP_X_OC_MTIME'])) { + if ($this->fileView->touch($this->path, $request->server['HTTP_X_OC_MTIME'])) { header('X-OC-MTime: accepted'); } } $this->refreshInfo(); } catch (StorageNotAvailableException $e) { - throw new ServiceUnavailable("Failed to check file size: ".$e->getMessage()); + throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); } return '"' . $this->info->getEtag() . '"'; @@ -188,6 +212,7 @@ class File extends Node implements IFile { /** * Returns the data + * * @return string|resource * @throws Forbidden * @throws ServiceUnavailable @@ -201,12 +226,13 @@ class File extends Node implements IFile { // returning 503 will allow retry of the operation at a later point in time throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); } catch (StorageNotAvailableException $e) { - throw new ServiceUnavailable("Failed to open file: ".$e->getMessage()); + throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); } } /** * Delete the current file + * * @throws Forbidden * @throws ServiceUnavailable */ @@ -221,7 +247,7 @@ class File extends Node implements IFile { throw new Forbidden(); } } catch (StorageNotAvailableException $e) { - throw new ServiceUnavailable("Failed to unlink: ".$e->getMessage()); + throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); } } @@ -236,7 +262,7 @@ class File extends Node implements IFile { $mimeType = $this->info->getMimetype(); // PROPFIND needs to return the correct mime type, for consistency with the web UI - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND' ) { + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { return $mimeType; } return \OC_Helper::getSecureMimeType($mimeType); @@ -266,8 +292,7 @@ class File extends Node implements IFile { * @throws NotImplemented * @throws ServiceUnavailable */ - private function createFileChunked($data) - { + private function createFileChunked($data) { list($path, $name) = \Sabre\HTTP\URLUtil::splitPath($this->path); $info = \OC_FileChunking::decodeName($name); @@ -278,7 +303,7 @@ class File extends Node implements IFile { $bytesWritten = $chunk_handler->store($info['index'], $data); //detect aborted upload - if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT' ) { + if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { if (isset($_SERVER['CONTENT_LENGTH'])) { $expected = $_SERVER['CONTENT_LENGTH']; if ($bytesWritten != $expected) { @@ -290,7 +315,7 @@ class File extends Node implements IFile { } if ($chunk_handler->isComplete()) { - list($storage, ) = $this->fileView->resolvePath($path); + list($storage,) = $this->fileView->resolvePath($path); $needsPartFile = $this->needsPartFile($storage); try { @@ -319,7 +344,7 @@ class File extends Node implements IFile { // allow sync clients to send the mtime along in a header $request = \OC::$server->getRequest(); if (isset($request->server['HTTP_X_OC_MTIME'])) { - if($this->fileView->touch($targetPath, $request->server['HTTP_X_OC_MTIME'])) { + if ($this->fileView->touch($targetPath, $request->server['HTTP_X_OC_MTIME'])) { header('X-OC-MTime: accepted'); } } @@ -327,7 +352,7 @@ class File extends Node implements IFile { $info = $this->fileView->getFileInfo($targetPath); return $info->getEtag(); } catch (StorageNotAvailableException $e) { - throw new ServiceUnavailable("Failed to put file: ".$e->getMessage()); + throw new ServiceUnavailable("Failed to put file: " . $e->getMessage()); } } @@ -338,6 +363,7 @@ class File extends Node implements IFile { * Returns whether a part file is needed for the given storage * or whether the file can be assembled/uploaded directly on the * target storage. + * * @param \OCP\Files\Storage $storage * @return bool true if the storage needs part file handling */ @@ -345,6 +371,6 @@ class File extends Node implements IFile { // TODO: in the future use ChunkHandler provided by storage // and/or add method on Storage called "needsPartFile()" return !$storage->instanceOfStorage('OCA\Files_Sharing\External\Storage') && - !$storage->instanceOfStorage('OC\Files\Storage\OwnCloud'); + !$storage->instanceOfStorage('OC\Files\Storage\OwnCloud'); } } diff --git a/tests/lib/connector/sabre/file.php b/tests/lib/connector/sabre/file.php index 74e289c1751..3fe5c2751f1 100644 --- a/tests/lib/connector/sabre/file.php +++ b/tests/lib/connector/sabre/file.php @@ -10,14 +10,25 @@ namespace Test\Connector\Sabre; class File extends \Test\TestCase { + private function getStream($string) { + $stream = fopen('php://temp', 'r+'); + fwrite($stream, $string); + fseek($stream, 0); + return $stream; + } + /** * @expectedException \Sabre\DAV\Exception */ public function testSimplePutFails() { // setup - $view = $this->getMock('\OC\Files\View', array('file_put_contents', 'getRelativePath'), array()); + $storage = $this->getMock('\OC\Files\Storage\Local', ['fopen'], [['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]]); + $view = $this->getMock('\OC\Files\View', array('file_put_contents', 'getRelativePath', 'resolvePath'), array()); $view->expects($this->any()) - ->method('file_put_contents') + ->method('resolvePath') + ->will($this->returnValue(array($storage, ''))); + $storage->expects($this->once()) + ->method('fopen') ->will($this->returnValue(false)); $view->expects($this->any()) @@ -25,7 +36,7 @@ class File extends \Test\TestCase { ->will($this->returnValue('/test.txt')); $info = new \OC\Files\FileInfo('/test.txt', null, null, array( - 'permissions'=>\OCP\Constants::PERMISSION_ALL + 'permissions' => \OCP\Constants::PERMISSION_ALL ), null); $file = new \OC\Connector\Sabre\File($view, $info); @@ -36,8 +47,9 @@ class File extends \Test\TestCase { public function testPutSingleFileShare() { // setup - $storage = $this->getMock('\OCP\Files\Storage'); - $view = $this->getMock('\OC\Files\View', array('file_put_contents', 'getRelativePath'), array()); + $stream = fopen('php://temp', 'w+'); + $storage = $this->getMock('\OC\Files\Storage\Local', ['fopen'], [['datadir' => \OC::$server->getTempManager()->getTemporaryFolder()]]); + $view = $this->getMock('\OC\Files\View', array('file_put_contents', 'getRelativePath', 'resolvePath'), array()); $view->expects($this->any()) ->method('resolvePath') ->with('') @@ -49,6 +61,9 @@ class File extends \Test\TestCase { ->method('file_put_contents') ->with('') ->will($this->returnValue(true)); + $storage->expects($this->once()) + ->method('fopen') + ->will($this->returnValue($stream)); $info = new \OC\Files\FileInfo('/foo.txt', null, null, array( 'permissions' => \OCP\Constants::PERMISSION_ALL @@ -56,7 +71,7 @@ class File extends \Test\TestCase { $file = new \OC\Connector\Sabre\File($view, $info); - $this->assertNotEmpty($file->put('test data')); + $this->assertNotEmpty($file->put($this->getStream('test data'))); } /** @@ -91,7 +106,7 @@ class File extends \Test\TestCase { $file = new \OC\Connector\Sabre\File($view, $info); // action - $file->put('test data'); + $file->put($this->getStream('test data')); } /** @@ -114,11 +129,12 @@ class File extends \Test\TestCase { $file = new \OC\Connector\Sabre\File($view, $info); // action - $file->put('test data'); + $file->put($this->getStream('test data')); } /** * Test setting name with setName() with invalid chars + * * @expectedException \OC\Connector\Sabre\Exception\InvalidPath */ public function testSetNameInvalidChars() { @@ -168,7 +184,7 @@ class File extends \Test\TestCase { $file = new \OC\Connector\Sabre\File($view, $info); // action - $file->put('test data'); + $file->put($this->getStream('test data')); } /** |