From 730c80ff9c83ff1b027ab804ecad599fbcaf7bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Mon, 7 Oct 2013 15:11:47 +0200 Subject: [PATCH 1/4] adding additional exceptions for special cases where creating a file might not be allowed --- .../sabre/exception/entitytoolarge.php | 23 +++++++++++++++++++ .../sabre/exception/unsupportedmediatype.php | 22 ++++++++++++++++++ lib/private/connector/sabre/file.php | 7 ++++++ lib/public/files/entitytoolargeexception.php | 11 +++++++++ lib/public/files/invalidcontentexception.php | 11 +++++++++ lib/public/files/invalidpathexception.php | 11 +++++++++ 6 files changed, 85 insertions(+) create mode 100644 lib/private/connector/sabre/exception/entitytoolarge.php create mode 100644 lib/private/connector/sabre/exception/unsupportedmediatype.php create mode 100644 lib/public/files/entitytoolargeexception.php create mode 100644 lib/public/files/invalidcontentexception.php create mode 100644 lib/public/files/invalidpathexception.php diff --git a/lib/private/connector/sabre/exception/entitytoolarge.php b/lib/private/connector/sabre/exception/entitytoolarge.php new file mode 100644 index 00000000000..aa9b37a0496 --- /dev/null +++ b/lib/private/connector/sabre/exception/entitytoolarge.php @@ -0,0 +1,23 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\Files; + +class EntityTooLargeException extends \Exception {} diff --git a/lib/public/files/invalidcontentexception.php b/lib/public/files/invalidcontentexception.php new file mode 100644 index 00000000000..184ec4d06d6 --- /dev/null +++ b/lib/public/files/invalidcontentexception.php @@ -0,0 +1,11 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\Files; + +class InvalidContentException extends \Exception {} diff --git a/lib/public/files/invalidpathexception.php b/lib/public/files/invalidpathexception.php new file mode 100644 index 00000000000..36090ae5b48 --- /dev/null +++ b/lib/public/files/invalidpathexception.php @@ -0,0 +1,11 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\Files; + +class InvalidPathException extends \Exception {} From 7c6ed6ab33e8487d6de135b5e956a82685bf4463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 8 Oct 2013 15:03:24 +0200 Subject: [PATCH 2/4] catch exceptions while uploading and pass on the error message --- apps/files/ajax/upload.php | 51 ++++++++++++++++++++---------------- apps/files/js/file-upload.js | 2 +- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/apps/files/ajax/upload.php b/apps/files/ajax/upload.php index 0920bf62109..45fb17de94a 100644 --- a/apps/files/ajax/upload.php +++ b/apps/files/ajax/upload.php @@ -110,30 +110,35 @@ if (strpos($dir, '..') === false) { || (isset($_POST['resolution']) && $_POST['resolution']==='replace') ) { // upload and overwrite file - if (is_uploaded_file($files['tmp_name'][$i]) and \OC\Files\Filesystem::fromTmpFile($files['tmp_name'][$i], $target)) { - - // updated max file size after upload - $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir); - - $meta = \OC\Files\Filesystem::getFileInfo($target); - if ($meta === false) { - $error = $l->t('Upload failed. Could not get file info.'); + try + { + if (is_uploaded_file($files['tmp_name'][$i]) and \OC\Files\Filesystem::fromTmpFile($files['tmp_name'][$i], $target)) { + + // updated max file size after upload + $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir); + + $meta = \OC\Files\Filesystem::getFileInfo($target); + if ($meta === false) { + $error = $l->t('Upload failed. Could not get file info.'); + } else { + $result[] = array('status' => 'success', + 'mime' => $meta['mimetype'], + 'mtime' => $meta['mtime'], + 'size' => $meta['size'], + 'id' => $meta['fileid'], + 'name' => basename($target), + 'originalname' => $files['tmp_name'][$i], + 'uploadMaxFilesize' => $maxUploadFileSize, + 'maxHumanFilesize' => $maxHumanFileSize, + 'permissions' => $meta['permissions'], + ); + } + } else { - $result[] = array('status' => 'success', - 'mime' => $meta['mimetype'], - 'mtime' => $meta['mtime'], - 'size' => $meta['size'], - 'id' => $meta['fileid'], - 'name' => basename($target), - 'originalname' => $files['tmp_name'][$i], - 'uploadMaxFilesize' => $maxUploadFileSize, - 'maxHumanFilesize' => $maxHumanFileSize, - 'permissions' => $meta['permissions'], - ); + $error = $l->t('Upload failed. Could not find uploaded file'); } - - } else { - $error = $l->t('Upload failed. Could not find uploaded file'); + } catch(Exception $ex) { + $error = $ex->getMessage(); } } else { @@ -164,5 +169,5 @@ if ($error === false) { OCP\JSON::encodedPrint($result); exit(); } else { - OCP\JSON::error(array('data' => array_merge(array('message' => $error), $storageStats))); + OCP\JSON::error(array(array('data' => array_merge(array('message' => $error), $storageStats)))); } diff --git a/apps/files/js/file-upload.js b/apps/files/js/file-upload.js index b52221ac1fc..88d5a05107f 100644 --- a/apps/files/js/file-upload.js +++ b/apps/files/js/file-upload.js @@ -365,7 +365,7 @@ $(document).ready(function() { } else if (result[0].status !== 'success') { //delete data.jqXHR; data.textStatus = 'servererror'; - data.errorThrown = result.data.message; // error message has been translated on server + data.errorThrown = result[0].data.message; // error message has been translated on server var fu = $(this).data('blueimp-fileupload') || $(this).data('fileupload'); fu._trigger('fail', e, data); } From caa27824a94e3af757ea33b01b5682ef963ae714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 8 Oct 2013 15:04:31 +0200 Subject: [PATCH 3/4] catch specific file exceptions and convert them to proper http status code via webdav --- lib/private/connector/sabre/file.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/private/connector/sabre/file.php b/lib/private/connector/sabre/file.php index 3c19da7c618..84154ee855a 100644 --- a/lib/private/connector/sabre/file.php +++ b/lib/private/connector/sabre/file.php @@ -87,14 +87,21 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D throw new Sabre_DAV_Exception(); } } catch (\OCP\Files\NotPermittedException $e) { - throw new Sabre_DAV_Exception_Forbidden(); + // a more general case - due to whatever reason the content could not be written + throw new Sabre_DAV_Exception_Forbidden($e->getMessage()); + } catch (\OCP\Files\EntityTooLargeException $e) { - throw new OC_Connector_Sabre_Exception_EntityTooLarge(); + // the file is too big to be stored + throw new OC_Connector_Sabre_Exception_EntityTooLarge($e->getMessage()); + } catch (\OCP\Files\InvalidContentException $e) { - throw new OC_Connector_Sabre_Exception_UnsupportedMediaType(); + // the file content is not permitted + throw new OC_Connector_Sabre_Exception_UnsupportedMediaType($e->getMessage()); + } catch (\OCP\Files\InvalidPathException $e) { - // TODO: add specific exception here - throw new Sabre_DAV_Exception_Forbidden(); + // the path for the file was not valid + // TODO: find proper http status code for this case + throw new Sabre_DAV_Exception_Forbidden($e->getMessage()); } // rename to correct path From 3f27fefdbe1342701161bf0949dd719f34dab76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 8 Oct 2013 15:40:42 +0200 Subject: [PATCH 4/4] fixing status code and formatting --- .../sabre/exception/entitytoolarge.php | 19 +++++++++---------- .../sabre/exception/unsupportedmediatype.php | 16 ++++++++-------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/private/connector/sabre/exception/entitytoolarge.php b/lib/private/connector/sabre/exception/entitytoolarge.php index aa9b37a0496..2bda51f2f3e 100644 --- a/lib/private/connector/sabre/exception/entitytoolarge.php +++ b/lib/private/connector/sabre/exception/entitytoolarge.php @@ -1,23 +1,22 @@