Skip to content

Commit c53b587

Browse files
committed
Make Sabre File exception messages translatable
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 88ded73 commit c53b587

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

apps/dav/lib/Connector/Sabre/File.php

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use OC\Files\Filesystem;
4444
use OC\Files\Stream\HashWrapper;
4545
use OC\Files\View;
46+
use OCA\DAV\AppInfo\Application;
4647
use OCA\DAV\Connector\Sabre\Exception\EntityTooLarge;
4748
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
4849
use OCA\DAV\Connector\Sabre\Exception\Forbidden as DAVForbiddenException;
@@ -59,7 +60,9 @@
5960
use OCP\Files\NotPermittedException;
6061
use OCP\Files\Storage;
6162
use OCP\Files\StorageNotAvailableException;
63+
use OCP\IL10N;
6264
use OCP\ILogger;
65+
use OCP\L10N\IFactory as IL10NFactory;
6366
use OCP\Lock\ILockingProvider;
6467
use OCP\Lock\LockedException;
6568
use OCP\Share\IManager;
@@ -74,6 +77,8 @@
7477
class File extends Node implements IFile {
7578
protected $request;
7679

80+
protected IL10N $l10n;
81+
7782
/**
7883
* Sets up the node, expects a full path name
7984
*
@@ -85,6 +90,11 @@ class File extends Node implements IFile {
8590
public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) {
8691
parent::__construct($view, $info, $shareManager);
8792

93+
// Querying IL10N directly results in a dependency loop
94+
/** @var IL10NFactory $l10nFactory */
95+
$l10nFactory = \OC::$server->get(IL10NFactory::class);
96+
$this->l10n = $l10nFactory->get(Application::APP_ID);
97+
8898
if (isset($request)) {
8999
$this->request = $request;
90100
} else {
@@ -127,7 +137,7 @@ public function put($data) {
127137
throw new Forbidden();
128138
}
129139
} catch (StorageNotAvailableException $e) {
130-
throw new ServiceUnavailable("File is not updatable: " . $e->getMessage());
140+
throw new ServiceUnavailable($this->l10n->t('File is not updatable: %1$s', [$e->getMessage()]));
131141
}
132142

133143
// verify path of the target
@@ -161,7 +171,7 @@ public function put($data) {
161171
$partFilePath = $this->path;
162172

163173
if ($view && !$this->emitPreHooks($exists)) {
164-
throw new Exception('Could not write to final file, canceled by hook');
174+
throw new Exception($this->l10n->t('Could not write to final file, canceled by hook'));
165175
}
166176
}
167177

@@ -220,7 +230,7 @@ public function put($data) {
220230
if ($target === false) {
221231
\OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']);
222232
// because we have no clue about the cause we can only throw back a 500/Internal Server Error
223-
throw new Exception('Could not write file contents');
233+
throw new Exception($this->l10n->t('Could not write file contents'));
224234
}
225235
[$count, $result] = \OC_Helper::streamCopy($data, $target);
226236
fclose($target);
@@ -232,7 +242,15 @@ public function put($data) {
232242
$expected = $_SERVER['CONTENT_LENGTH'];
233243
}
234244
if ($expected !== "0") {
235-
throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )');
245+
throw new Exception(
246+
$this->l10n->t(
247+
'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)',
248+
[
249+
$this->l10n->n('%n byte', '%n bytes', $count),
250+
$this->l10n->n('%n byte', '%n bytes', $expected),
251+
],
252+
)
253+
);
236254
}
237255
}
238256

@@ -242,7 +260,15 @@ public function put($data) {
242260
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
243261
$expected = (int)$_SERVER['CONTENT_LENGTH'];
244262
if ($count !== $expected) {
245-
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.');
263+
throw new BadRequest(
264+
$this->l10n->t(
265+
'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.',
266+
[
267+
$this->l10n->n('%n byte', '%n bytes', $expected),
268+
$this->l10n->n('%n byte', '%n bytes', $count),
269+
],
270+
)
271+
);
246272
}
247273
}
248274
} catch (\Exception $e) {
@@ -263,7 +289,7 @@ public function put($data) {
263289
if ($needsPartFile) {
264290
if ($view && !$this->emitPreHooks($exists)) {
265291
$partStorage->unlink($internalPartPath);
266-
throw new Exception('Could not rename part file to final file, canceled by hook');
292+
throw new Exception($this->l10n->t('Could not rename part file to final file, canceled by hook'));
267293
}
268294
try {
269295
$this->changeLock(ILockingProvider::LOCK_EXCLUSIVE);
@@ -292,7 +318,7 @@ public function put($data) {
292318
$fileExists = $storage->file_exists($internalPath);
293319
if ($renameOkay === false || $fileExists === false) {
294320
\OC::$server->getLogger()->error('renaming part file to final file failed $renameOkay: ' . ($renameOkay ? 'true' : 'false') . ', $fileExists: ' . ($fileExists ? 'true' : 'false') . ')', ['app' => 'webdav']);
295-
throw new Exception('Could not rename part file to final file');
321+
throw new Exception($this->l10n->t('Could not rename part file to final file'));
296322
}
297323
} catch (ForbiddenException $ex) {
298324
if (!$ex->getRetry()) {
@@ -350,7 +376,7 @@ public function put($data) {
350376
$this->refreshInfo();
351377
}
352378
} catch (StorageNotAvailableException $e) {
353-
throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage(), 0, $e);
379+
throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e);
354380
}
355381

356382
return '"' . $this->info->getEtag() . '"';
@@ -435,14 +461,14 @@ public function get() {
435461
$this->convertToSabreException($e);
436462
}
437463
if ($res === false) {
438-
throw new ServiceUnavailable("Could not open file");
464+
throw new ServiceUnavailable($this->l10n->t('Could not open file'));
439465
}
440466
return $res;
441467
} catch (GenericEncryptionException $e) {
442468
// returning 503 will allow retry of the operation at a later point in time
443-
throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage());
469+
throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]));
444470
} catch (StorageNotAvailableException $e) {
445-
throw new ServiceUnavailable("Failed to open file: " . $e->getMessage());
471+
throw new ServiceUnavailable($this->l10n->t('Failed to open file: %1$s', [$e->getMessage()]));
446472
} catch (ForbiddenException $ex) {
447473
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
448474
} catch (LockedException $e) {
@@ -467,7 +493,7 @@ public function delete() {
467493
throw new Forbidden();
468494
}
469495
} catch (StorageNotAvailableException $e) {
470-
throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage());
496+
throw new ServiceUnavailable($this->l10n->t('Failed to unlink: %1$s', [$e->getMessage()]));
471497
} catch (ForbiddenException $ex) {
472498
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
473499
} catch (LockedException $e) {
@@ -521,7 +547,7 @@ private function createFileChunked($data) {
521547

522548
$info = \OC_FileChunking::decodeName($name);
523549
if (empty($info)) {
524-
throw new NotImplemented('Invalid chunk name');
550+
throw new NotImplemented($this->l10n->t('Invalid chunk name'));
525551
}
526552

527553
$chunk_handler = new \OC_FileChunking($info);
@@ -533,7 +559,15 @@ private function createFileChunked($data) {
533559
$expected = (int)$_SERVER['CONTENT_LENGTH'];
534560
if ($bytesWritten !== $expected) {
535561
$chunk_handler->remove($info['index']);
536-
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.');
562+
throw new BadRequest(
563+
$this->l10n->t(
564+
'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.',
565+
[
566+
$this->l10n->n('%n byte', '%n bytes', $expected),
567+
$this->l10n->n('%n byte', '%n bytes', $bytesWritten),
568+
],
569+
)
570+
);
537571
}
538572
}
539573
}
@@ -580,7 +614,7 @@ private function createFileChunked($data) {
580614
$targetStorage->unlink($targetInternalPath);
581615
}
582616
$this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED);
583-
throw new Exception('Could not rename part file assembled from chunks');
617+
throw new Exception($this->l10n->t('Could not rename part file assembled from chunks'));
584618
}
585619
} else {
586620
// assemble directly into the final file
@@ -664,13 +698,13 @@ private function convertToSabreException(\Exception $e) {
664698
}
665699
if ($e instanceof GenericEncryptionException) {
666700
// returning 503 will allow retry of the operation at a later point in time
667-
throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e);
701+
throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]), 0, $e);
668702
}
669703
if ($e instanceof StorageNotAvailableException) {
670-
throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e);
704+
throw new ServiceUnavailable($this->l10n->t('Failed to write file contents: %1$s', [$e->getMessage()]), 0, $e);
671705
}
672706
if ($e instanceof NotFoundException) {
673-
throw new NotFound('File not found: ' . $e->getMessage(), 0, $e);
707+
throw new NotFound($this->l10n->t('File not found: %1$s', [$e->getMessage()]), 0, $e);
674708
}
675709

676710
throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e);

0 commit comments

Comments
 (0)