Skip to content

Commit 67ec981

Browse files
committed
Make Sabre File exception messages translatable
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 3026d57 commit 67ec981

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;
@@ -60,7 +61,9 @@
6061
use OCP\Files\NotPermittedException;
6162
use OCP\Files\Storage;
6263
use OCP\Files\StorageNotAvailableException;
64+
use OCP\IL10N;
6365
use OCP\ILogger;
66+
use OCP\L10N\IFactory as IL10NFactory;
6467
use OCP\Lock\ILockingProvider;
6568
use OCP\Lock\LockedException;
6669
use OCP\Share\IManager;
@@ -75,6 +78,8 @@
7578
class File extends Node implements IFile {
7679
protected $request;
7780

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

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

134144
// verify path of the target
@@ -162,7 +172,7 @@ public function put($data) {
162172
$partFilePath = $this->path;
163173

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

@@ -223,7 +233,7 @@ public function put($data) {
223233
if ($target === false) {
224234
\OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']);
225235
// because we have no clue about the cause we can only throw back a 500/Internal Server Error
226-
throw new Exception('Could not write file contents');
236+
throw new Exception($this->l10n->t('Could not write file contents'));
227237
}
228238
[$count, $result] = \OC_Helper::streamCopy($data, $target);
229239
fclose($target);
@@ -235,7 +245,15 @@ public function put($data) {
235245
$expected = $_SERVER['CONTENT_LENGTH'];
236246
}
237247
if ($expected !== "0") {
238-
throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )');
248+
throw new Exception(
249+
$this->l10n->t(
250+
'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)',
251+
[
252+
$this->l10n->n('%n byte', '%n bytes', $count),
253+
$this->l10n->n('%n byte', '%n bytes', $expected),
254+
],
255+
)
256+
);
239257
}
240258
}
241259

@@ -245,7 +263,15 @@ public function put($data) {
245263
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
246264
$expected = (int)$_SERVER['CONTENT_LENGTH'];
247265
if ($count !== $expected) {
248-
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.');
266+
throw new BadRequest(
267+
$this->l10n->t(
268+
'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.',
269+
[
270+
$this->l10n->n('%n byte', '%n bytes', $expected),
271+
$this->l10n->n('%n byte', '%n bytes', $count),
272+
],
273+
)
274+
);
249275
}
250276
}
251277
} catch (\Exception $e) {
@@ -266,7 +292,7 @@ public function put($data) {
266292
if ($needsPartFile) {
267293
if ($view && !$this->emitPreHooks($exists)) {
268294
$partStorage->unlink($internalPartPath);
269-
throw new Exception('Could not rename part file to final file, canceled by hook');
295+
throw new Exception($this->l10n->t('Could not rename part file to final file, canceled by hook'));
270296
}
271297
try {
272298
$this->changeLock(ILockingProvider::LOCK_EXCLUSIVE);
@@ -295,7 +321,7 @@ public function put($data) {
295321
$fileExists = $storage->file_exists($internalPath);
296322
if ($renameOkay === false || $fileExists === false) {
297323
\OC::$server->getLogger()->error('renaming part file to final file failed $renameOkay: ' . ($renameOkay ? 'true' : 'false') . ', $fileExists: ' . ($fileExists ? 'true' : 'false') . ')', ['app' => 'webdav']);
298-
throw new Exception('Could not rename part file to final file');
324+
throw new Exception($this->l10n->t('Could not rename part file to final file'));
299325
}
300326
} catch (ForbiddenException $ex) {
301327
if (!$ex->getRetry()) {
@@ -353,7 +379,7 @@ public function put($data) {
353379
$this->refreshInfo();
354380
}
355381
} catch (StorageNotAvailableException $e) {
356-
throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage(), 0, $e);
382+
throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e);
357383
}
358384

359385
return '"' . $this->info->getEtag() . '"';
@@ -438,14 +464,14 @@ public function get() {
438464
$this->convertToSabreException($e);
439465
}
440466
if ($res === false) {
441-
throw new ServiceUnavailable("Could not open file");
467+
throw new ServiceUnavailable($this->l10n->t('Could not open file'));
442468
}
443469
return $res;
444470
} catch (GenericEncryptionException $e) {
445471
// returning 503 will allow retry of the operation at a later point in time
446-
throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage());
472+
throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]));
447473
} catch (StorageNotAvailableException $e) {
448-
throw new ServiceUnavailable("Failed to open file: " . $e->getMessage());
474+
throw new ServiceUnavailable($this->l10n->t('Failed to open file: %1$s', [$e->getMessage()]));
449475
} catch (ForbiddenException $ex) {
450476
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
451477
} catch (LockedException $e) {
@@ -470,7 +496,7 @@ public function delete() {
470496
throw new Forbidden();
471497
}
472498
} catch (StorageNotAvailableException $e) {
473-
throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage());
499+
throw new ServiceUnavailable($this->l10n->t('Failed to unlink: %1$s', [$e->getMessage()]));
474500
} catch (ForbiddenException $ex) {
475501
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
476502
} catch (LockedException $e) {
@@ -524,7 +550,7 @@ private function createFileChunked($data) {
524550

525551
$info = \OC_FileChunking::decodeName($name);
526552
if (empty($info)) {
527-
throw new NotImplemented('Invalid chunk name');
553+
throw new NotImplemented($this->l10n->t('Invalid chunk name'));
528554
}
529555

530556
$chunk_handler = new \OC_FileChunking($info);
@@ -536,7 +562,15 @@ private function createFileChunked($data) {
536562
$expected = (int)$_SERVER['CONTENT_LENGTH'];
537563
if ($bytesWritten !== $expected) {
538564
$chunk_handler->remove($info['index']);
539-
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.');
565+
throw new BadRequest(
566+
$this->l10n->t(
567+
'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.',
568+
[
569+
$this->l10n->n('%n byte', '%n bytes', $expected),
570+
$this->l10n->n('%n byte', '%n bytes', $bytesWritten),
571+
],
572+
)
573+
);
540574
}
541575
}
542576
}
@@ -583,7 +617,7 @@ private function createFileChunked($data) {
583617
$targetStorage->unlink($targetInternalPath);
584618
}
585619
$this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED);
586-
throw new Exception('Could not rename part file assembled from chunks');
620+
throw new Exception($this->l10n->t('Could not rename part file assembled from chunks'));
587621
}
588622
} else {
589623
// assemble directly into the final file
@@ -667,13 +701,13 @@ private function convertToSabreException(\Exception $e) {
667701
}
668702
if ($e instanceof GenericEncryptionException) {
669703
// returning 503 will allow retry of the operation at a later point in time
670-
throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e);
704+
throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]), 0, $e);
671705
}
672706
if ($e instanceof StorageNotAvailableException) {
673-
throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e);
707+
throw new ServiceUnavailable($this->l10n->t('Failed to write file contents: %1$s', [$e->getMessage()]), 0, $e);
674708
}
675709
if ($e instanceof NotFoundException) {
676-
throw new NotFound('File not found: ' . $e->getMessage(), 0, $e);
710+
throw new NotFound($this->l10n->t('File not found: %1$s', [$e->getMessage()]), 0, $e);
677711
}
678712

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

0 commit comments

Comments
 (0)