From 3623f14e73046a51953872fe49853bc200ac736d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Wed, 4 Mar 2015 14:03:47 +0100 Subject: [PATCH] no translation service in common storage class --- lib/private/files/storage/common.php | 16 +++----- lib/private/files/view.php | 14 +++++-- .../files/invalidcharacterinpathexception.php | 37 +++++++++++++++++++ lib/public/files/reservedwordexception.php | 37 +++++++++++++++++++ tests/lib/files/pathverificationtest.php | 9 ++--- 5 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 lib/public/files/invalidcharacterinpathexception.php create mode 100644 lib/public/files/reservedwordexception.php diff --git a/lib/private/files/storage/common.php b/lib/private/files/storage/common.php index e948b5aa35..8549d5a1fa 100644 --- a/lib/private/files/storage/common.php +++ b/lib/private/files/storage/common.php @@ -13,7 +13,9 @@ use OC\Files\Cache\Scanner; use OC\Files\Cache\Storage; use OC\Files\Filesystem; use OC\Files\Cache\Watcher; +use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidPathException; +use OCP\Files\ReservedWordException; /** * Storage backend class for providing common filesystem operation methods @@ -476,7 +478,7 @@ abstract class Common implements \OC\Files\Storage\Storage { $this->scanForInvalidCharacters($fileName, "\\/<>:\"|?*"); $reservedNames = ['CON', 'PRN', 'AUX', 'NUL', 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9', 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9']; if (in_array(strtoupper($fileName), $reservedNames)) { - throw new InvalidPathException($this->t("File name is a reserved word")); + throw new ReservedWordException(); } } @@ -489,7 +491,7 @@ abstract class Common implements \OC\Files\Storage\Storage { $this->scanForInvalidCharacters($fileName, "\\/"); $reservedNames = ['*']; if (in_array($fileName, $reservedNames)) { - throw new InvalidPathException($this->t('File name is a reserved word')); + throw new ReservedWordException(); } } @@ -501,19 +503,13 @@ abstract class Common implements \OC\Files\Storage\Storage { private function scanForInvalidCharacters($fileName, $invalidChars) { foreach(str_split($invalidChars) as $char) { if (strpos($fileName, $char) !== false) { - throw new InvalidPathException($this->t('File name contains at least one invalid characters')); + throw new InvalidCharacterInPathException(); } } $sanitizedFileName = filter_var($fileName, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); if($sanitizedFileName !== $fileName) { - throw new InvalidPathException($this->t('File name contains at least one invalid characters')); + throw new InvalidCharacterInPathException(); } } - - private function t($string) { - $l10n = \OC::$server->getL10N('lib'); - return $l10n->t($string); - } - } diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 9d8416d233..f14209fd92 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -11,7 +11,9 @@ namespace OC\Files; use OC\Files\Cache\Updater; use OC\Files\Mount\MoveableMount; +use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidPathException; +use OCP\Files\ReservedWordException; /** * Class to provide access to ownCloud filesystem via a "view", and methods for @@ -1563,8 +1565,14 @@ class View { throw new InvalidPathException($l10n->t('4-byte characters are not supported in file names')); } - /** @type \OCP\Files\Storage $storage */ - list($storage, $internalPath) = $this->resolvePath($path); - $storage->verifyPath($internalPath, $fileName); + try { + /** @type \OCP\Files\Storage $storage */ + list($storage, $internalPath) = $this->resolvePath($path); + $storage->verifyPath($internalPath, $fileName); + } catch (ReservedWordException $ex) { + throw new InvalidPathException($l10n->t('File name is a reserved word')); + } catch (InvalidCharacterInPathException $ex) { + throw new InvalidPathException($l10n->t('File name contains at least one invalid characters')); + } } } diff --git a/lib/public/files/invalidcharacterinpathexception.php b/lib/public/files/invalidcharacterinpathexception.php new file mode 100644 index 0000000000..4ae2eb01c1 --- /dev/null +++ b/lib/public/files/invalidcharacterinpathexception.php @@ -0,0 +1,37 @@ +. + * + */ + +/** + * Public interface of ownCloud for apps to use. + * Files/InvalidCharacterInPathException class + */ + +// use OCP namespace for all classes that are considered public. +// This means that they should be used by apps instead of the internal ownCloud classes +namespace OCP\Files; + +/** + * Exception for invalid path + */ +class InvalidCharacterInPathException extends InvalidPathException { + +} diff --git a/lib/public/files/reservedwordexception.php b/lib/public/files/reservedwordexception.php new file mode 100644 index 0000000000..25c618a8de --- /dev/null +++ b/lib/public/files/reservedwordexception.php @@ -0,0 +1,37 @@ +. + * + */ + +/** + * Public interface of ownCloud for apps to use. + * Files/ReservedWordException class + */ + +// use OCP namespace for all classes that are considered public. +// This means that they should be used by apps instead of the internal ownCloud classes +namespace OCP\Files; + +/** + * Exception for invalid path + */ +class ReservedWordException extends InvalidPathException { + +} diff --git a/tests/lib/files/pathverificationtest.php b/tests/lib/files/pathverificationtest.php index fafc98cc3c..1a802a48f5 100644 --- a/tests/lib/files/pathverificationtest.php +++ b/tests/lib/files/pathverificationtest.php @@ -78,8 +78,7 @@ class PathVerification extends \Test\TestCase { /** * @dataProvider providesInvalidCharsWindows - * @expectedException \OCP\Files\InvalidPathException - * @expectedExceptionMessage File name contains at least one invalid characters + * @expectedException \OCP\Files\InvalidCharacterInPathException */ public function testPathVerificationInvalidCharsWindows($fileName) { $storage = new Local(['datadir' => '']); @@ -136,8 +135,7 @@ class PathVerification extends \Test\TestCase { /** * @dataProvider providesInvalidCharsPosix - * @expectedException \OCP\Files\InvalidPathException - * @expectedExceptionMessage File name contains at least one invalid characters + * @expectedException \OCP\Files\InvalidCharacterInPathException */ public function testPathVerificationInvalidCharsPosix($fileName) { $storage = new Local(['datadir' => '']); @@ -187,8 +185,7 @@ class PathVerification extends \Test\TestCase { /** * @dataProvider providesReservedNamesWindows - * @expectedException \OCP\Files\InvalidPathException - * @expectedExceptionMessage File name is a reserved word + * @expectedException \OCP\Files\ReservedWordException */ public function testPathVerificationReservedNamesWindows($fileName) { $storage = new Local(['datadir' => '']);