]> source.dussan.org Git - nextcloud-server.git/commitdiff
Make Sabre File exception messages translatable 30536/head
authorChristopher Ng <chrng8@gmail.com>
Fri, 4 Feb 2022 19:02:32 +0000 (19:02 +0000)
committerChristopher Ng <chrng8@gmail.com>
Tue, 1 Mar 2022 23:29:33 +0000 (23:29 +0000)
Signed-off-by: Christopher Ng <chrng8@gmail.com>
apps/dav/lib/Connector/Sabre/File.php

index 28817b93b760d993bcb5781e2204c486b4d9cb05..3a13dba5df64af6fb7ad6503947d5e77a852c0d9 100644 (file)
@@ -43,6 +43,7 @@ use OC\AppFramework\Http\Request;
 use OC\Files\Filesystem;
 use OC\Files\Stream\HashWrapper;
 use OC\Files\View;
+use OCA\DAV\AppInfo\Application;
 use OCA\DAV\Connector\Sabre\Exception\EntityTooLarge;
 use OCA\DAV\Connector\Sabre\Exception\FileLocked;
 use OCA\DAV\Connector\Sabre\Exception\Forbidden as DAVForbiddenException;
@@ -60,7 +61,9 @@ use OCP\Files\NotFoundException;
 use OCP\Files\NotPermittedException;
 use OCP\Files\Storage;
 use OCP\Files\StorageNotAvailableException;
+use OCP\IL10N;
 use OCP\ILogger;
+use OCP\L10N\IFactory as IL10NFactory;
 use OCP\Lock\ILockingProvider;
 use OCP\Lock\LockedException;
 use OCP\Share\IManager;
@@ -75,6 +78,8 @@ use Sabre\DAV\IFile;
 class File extends Node implements IFile {
        protected $request;
 
+       protected IL10N $l10n;
+
        /**
         * Sets up the node, expects a full path name
         *
@@ -86,6 +91,11 @@ class File extends Node implements IFile {
        public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) {
                parent::__construct($view, $info, $shareManager);
 
+               // Querying IL10N directly results in a dependency loop
+               /** @var IL10NFactory $l10nFactory */
+               $l10nFactory = \OC::$server->get(IL10NFactory::class);
+               $this->l10n = $l10nFactory->get(Application::APP_ID);
+
                if (isset($request)) {
                        $this->request = $request;
                } else {
@@ -128,7 +138,7 @@ class File extends Node implements IFile {
                                throw new Forbidden();
                        }
                } catch (StorageNotAvailableException $e) {
-                       throw new ServiceUnavailable("File is not updatable: " . $e->getMessage());
+                       throw new ServiceUnavailable($this->l10n->t('File is not updatable: %1$s', [$e->getMessage()]));
                }
 
                // verify path of the target
@@ -162,7 +172,7 @@ class File extends Node implements IFile {
                        $partFilePath = $this->path;
 
                        if ($view && !$this->emitPreHooks($exists)) {
-                               throw new Exception('Could not write to final file, canceled by hook');
+                               throw new Exception($this->l10n->t('Could not write to final file, canceled by hook'));
                        }
                }
 
@@ -223,7 +233,7 @@ class File extends Node implements IFile {
                                if ($target === false) {
                                        \OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']);
                                        // 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');
+                                       throw new Exception($this->l10n->t('Could not write file contents'));
                                }
                                [$count, $result] = \OC_Helper::streamCopy($data, $target);
                                fclose($target);
@@ -235,7 +245,15 @@ class File extends Node implements IFile {
                                        $expected = $_SERVER['CONTENT_LENGTH'];
                                }
                                if ($expected !== "0") {
-                                       throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )');
+                                       throw new Exception(
+                                               $this->l10n->t(
+                                                       'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)',
+                                                       [
+                                                               $this->l10n->n('%n byte', '%n bytes', $count),
+                                                               $this->l10n->n('%n byte', '%n bytes', $expected),
+                                                       ],
+                                               )
+                                       );
                                }
                        }
 
@@ -245,7 +263,15 @@ class File extends Node implements IFile {
                        if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
                                $expected = (int)$_SERVER['CONTENT_LENGTH'];
                                if ($count !== $expected) {
-                                       throw new BadRequest('Expected filesize of ' . $expected . ' bytes but read (from Nextcloud client) and wrote (to Nextcloud storage) ' . $count . ' bytes. Could either be a network problem on the sending side or a problem writing to the storage on the server side.');
+                                       throw new BadRequest(
+                                               $this->l10n->t(
+                                                       'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.',
+                                                       [
+                                                               $this->l10n->n('%n byte', '%n bytes', $expected),
+                                                               $this->l10n->n('%n byte', '%n bytes', $count),
+                                                       ],
+                                               )
+                                       );
                                }
                        }
                } catch (\Exception $e) {
@@ -266,7 +292,7 @@ class File extends Node implements IFile {
                        if ($needsPartFile) {
                                if ($view && !$this->emitPreHooks($exists)) {
                                        $partStorage->unlink($internalPartPath);
-                                       throw new Exception('Could not rename part file to final file, canceled by hook');
+                                       throw new Exception($this->l10n->t('Could not rename part file to final file, canceled by hook'));
                                }
                                try {
                                        $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE);
@@ -295,7 +321,7 @@ class File extends Node implements IFile {
                                        $fileExists = $storage->file_exists($internalPath);
                                        if ($renameOkay === false || $fileExists === false) {
                                                \OC::$server->getLogger()->error('renaming part file to final file failed $renameOkay: ' . ($renameOkay ? 'true' : 'false') . ', $fileExists: ' . ($fileExists ? 'true' : 'false') . ')', ['app' => 'webdav']);
-                                               throw new Exception('Could not rename part file to final file');
+                                               throw new Exception($this->l10n->t('Could not rename part file to final file'));
                                        }
                                } catch (ForbiddenException $ex) {
                                        if (!$ex->getRetry()) {
@@ -353,7 +379,7 @@ class File extends Node implements IFile {
                                $this->refreshInfo();
                        }
                } catch (StorageNotAvailableException $e) {
-                       throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage(), 0, $e);
+                       throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e);
                }
 
                return '"' . $this->info->getEtag() . '"';
@@ -438,14 +464,14 @@ class File extends Node implements IFile {
                                $this->convertToSabreException($e);
                        }
                        if ($res === false) {
-                               throw new ServiceUnavailable("Could not open file");
+                               throw new ServiceUnavailable($this->l10n->t('Could not open file'));
                        }
                        return $res;
                } catch (GenericEncryptionException $e) {
                        // returning 503 will allow retry of the operation at a later point in time
-                       throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage());
+                       throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]));
                } catch (StorageNotAvailableException $e) {
-                       throw new ServiceUnavailable("Failed to open file: " . $e->getMessage());
+                       throw new ServiceUnavailable($this->l10n->t('Failed to open file: %1$s', [$e->getMessage()]));
                } catch (ForbiddenException $ex) {
                        throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
                } catch (LockedException $e) {
@@ -470,7 +496,7 @@ class File extends Node implements IFile {
                                throw new Forbidden();
                        }
                } catch (StorageNotAvailableException $e) {
-                       throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage());
+                       throw new ServiceUnavailable($this->l10n->t('Failed to unlink: %1$s', [$e->getMessage()]));
                } catch (ForbiddenException $ex) {
                        throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
                } catch (LockedException $e) {
@@ -524,7 +550,7 @@ class File extends Node implements IFile {
 
                $info = \OC_FileChunking::decodeName($name);
                if (empty($info)) {
-                       throw new NotImplemented('Invalid chunk name');
+                       throw new NotImplemented($this->l10n->t('Invalid chunk name'));
                }
 
                $chunk_handler = new \OC_FileChunking($info);
@@ -536,7 +562,15 @@ class File extends Node implements IFile {
                                $expected = (int)$_SERVER['CONTENT_LENGTH'];
                                if ($bytesWritten !== $expected) {
                                        $chunk_handler->remove($info['index']);
-                                       throw new BadRequest('Expected filesize of ' . $expected . ' bytes but read (from Nextcloud client) and wrote (to Nextcloud storage) ' . $bytesWritten . ' bytes. Could either be a network problem on the sending side or a problem writing to the storage on the server side.');
+                                       throw new BadRequest(
+                                               $this->l10n->t(
+                                                       'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.',
+                                                       [
+                                                               $this->l10n->n('%n byte', '%n bytes', $expected),
+                                                               $this->l10n->n('%n byte', '%n bytes', $bytesWritten),
+                                                       ],
+                                               )
+                                       );
                                }
                        }
                }
@@ -583,7 +617,7 @@ class File extends Node implements IFile {
                                                        $targetStorage->unlink($targetInternalPath);
                                                }
                                                $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED);
-                                               throw new Exception('Could not rename part file assembled from chunks');
+                                               throw new Exception($this->l10n->t('Could not rename part file assembled from chunks'));
                                        }
                                } else {
                                        // assemble directly into the final file
@@ -667,13 +701,13 @@ class File extends Node implements IFile {
                }
                if ($e instanceof GenericEncryptionException) {
                        // returning 503 will allow retry of the operation at a later point in time
-                       throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e);
+                       throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]), 0, $e);
                }
                if ($e instanceof StorageNotAvailableException) {
-                       throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e);
+                       throw new ServiceUnavailable($this->l10n->t('Failed to write file contents: %1$s', [$e->getMessage()]), 0, $e);
                }
                if ($e instanceof NotFoundException) {
-                       throw new NotFound('File not found: ' . $e->getMessage(), 0, $e);
+                       throw new NotFound($this->l10n->t('File not found: %1$s', [$e->getMessage()]), 0, $e);
                }
 
                throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e);