Skip to content

Commit 4fdabd1

Browse files
authored
Merge pull request #31392 from nextcloud/backport/30536/stable23
[stable23] Make Sabre File exception messages translatable
2 parents f8e3529 + 21f1d43 commit 4fdabd1

File tree

1 file changed

+53
-18
lines changed

1 file changed

+53
-18
lines changed

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

Lines changed: 53 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,9 @@
7578
class File extends Node implements IFile {
7679
protected $request;
7780

81+
/** @var IL10N */
82+
protected $l10n;
83+
7884
/**
7985
* Sets up the node, expects a full path name
8086
*
@@ -86,6 +92,11 @@ class File extends Node implements IFile {
8692
public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) {
8793
parent::__construct($view, $info, $shareManager);
8894

95+
// Querying IL10N directly results in a dependency loop
96+
/** @var IL10NFactory $l10nFactory */
97+
$l10nFactory = \OC::$server->get(IL10NFactory::class);
98+
$this->l10n = $l10nFactory->get(Application::APP_ID);
99+
89100
if (isset($request)) {
90101
$this->request = $request;
91102
} else {
@@ -128,7 +139,7 @@ public function put($data) {
128139
throw new Forbidden();
129140
}
130141
} catch (StorageNotAvailableException $e) {
131-
throw new ServiceUnavailable("File is not updatable: " . $e->getMessage());
142+
throw new ServiceUnavailable($this->l10n->t('File is not updatable: %1$s', [$e->getMessage()]));
132143
}
133144

134145
// verify path of the target
@@ -162,7 +173,7 @@ public function put($data) {
162173
$partFilePath = $this->path;
163174

164175
if ($view && !$this->emitPreHooks($exists)) {
165-
throw new Exception('Could not write to final file, canceled by hook');
176+
throw new Exception($this->l10n->t('Could not write to final file, canceled by hook'));
166177
}
167178
}
168179

@@ -223,7 +234,7 @@ public function put($data) {
223234
if ($target === false) {
224235
\OC::$server->getLogger()->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']);
225236
// 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');
237+
throw new Exception($this->l10n->t('Could not write file contents'));
227238
}
228239
[$count, $result] = \OC_Helper::streamCopy($data, $target);
229240
fclose($target);
@@ -235,7 +246,15 @@ public function put($data) {
235246
$expected = $_SERVER['CONTENT_LENGTH'];
236247
}
237248
if ($expected !== "0") {
238-
throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )');
249+
throw new Exception(
250+
$this->l10n->t(
251+
'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)',
252+
[
253+
$this->l10n->n('%n byte', '%n bytes', $count),
254+
$this->l10n->n('%n byte', '%n bytes', $expected),
255+
],
256+
)
257+
);
239258
}
240259
}
241260

@@ -245,7 +264,15 @@ public function put($data) {
245264
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
246265
$expected = (int)$_SERVER['CONTENT_LENGTH'];
247266
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.');
267+
throw new BadRequest(
268+
$this->l10n->t(
269+
'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.',
270+
[
271+
$this->l10n->n('%n byte', '%n bytes', $expected),
272+
$this->l10n->n('%n byte', '%n bytes', $count),
273+
],
274+
)
275+
);
249276
}
250277
}
251278
} catch (\Exception $e) {
@@ -266,7 +293,7 @@ public function put($data) {
266293
if ($needsPartFile) {
267294
if ($view && !$this->emitPreHooks($exists)) {
268295
$partStorage->unlink($internalPartPath);
269-
throw new Exception('Could not rename part file to final file, canceled by hook');
296+
throw new Exception($this->l10n->t('Could not rename part file to final file, canceled by hook'));
270297
}
271298
try {
272299
$this->changeLock(ILockingProvider::LOCK_EXCLUSIVE);
@@ -295,7 +322,7 @@ public function put($data) {
295322
$fileExists = $storage->file_exists($internalPath);
296323
if ($renameOkay === false || $fileExists === false) {
297324
\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');
325+
throw new Exception($this->l10n->t('Could not rename part file to final file'));
299326
}
300327
} catch (ForbiddenException $ex) {
301328
if (!$ex->getRetry()) {
@@ -353,7 +380,7 @@ public function put($data) {
353380
$this->refreshInfo();
354381
}
355382
} catch (StorageNotAvailableException $e) {
356-
throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage(), 0, $e);
383+
throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e);
357384
}
358385

359386
return '"' . $this->info->getEtag() . '"';
@@ -438,14 +465,14 @@ public function get() {
438465
$this->convertToSabreException($e);
439466
}
440467
if ($res === false) {
441-
throw new ServiceUnavailable("Could not open file");
468+
throw new ServiceUnavailable($this->l10n->t('Could not open file'));
442469
}
443470
return $res;
444471
} catch (GenericEncryptionException $e) {
445472
// returning 503 will allow retry of the operation at a later point in time
446-
throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage());
473+
throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]));
447474
} catch (StorageNotAvailableException $e) {
448-
throw new ServiceUnavailable("Failed to open file: " . $e->getMessage());
475+
throw new ServiceUnavailable($this->l10n->t('Failed to open file: %1$s', [$e->getMessage()]));
449476
} catch (ForbiddenException $ex) {
450477
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
451478
} catch (LockedException $e) {
@@ -470,7 +497,7 @@ public function delete() {
470497
throw new Forbidden();
471498
}
472499
} catch (StorageNotAvailableException $e) {
473-
throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage());
500+
throw new ServiceUnavailable($this->l10n->t('Failed to unlink: %1$s', [$e->getMessage()]));
474501
} catch (ForbiddenException $ex) {
475502
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
476503
} catch (LockedException $e) {
@@ -524,7 +551,7 @@ private function createFileChunked($data) {
524551

525552
$info = \OC_FileChunking::decodeName($name);
526553
if (empty($info)) {
527-
throw new NotImplemented('Invalid chunk name');
554+
throw new NotImplemented($this->l10n->t('Invalid chunk name'));
528555
}
529556

530557
$chunk_handler = new \OC_FileChunking($info);
@@ -536,7 +563,15 @@ private function createFileChunked($data) {
536563
$expected = (int)$_SERVER['CONTENT_LENGTH'];
537564
if ($bytesWritten !== $expected) {
538565
$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.');
566+
throw new BadRequest(
567+
$this->l10n->t(
568+
'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.',
569+
[
570+
$this->l10n->n('%n byte', '%n bytes', $expected),
571+
$this->l10n->n('%n byte', '%n bytes', $bytesWritten),
572+
],
573+
)
574+
);
540575
}
541576
}
542577
}
@@ -583,7 +618,7 @@ private function createFileChunked($data) {
583618
$targetStorage->unlink($targetInternalPath);
584619
}
585620
$this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED);
586-
throw new Exception('Could not rename part file assembled from chunks');
621+
throw new Exception($this->l10n->t('Could not rename part file assembled from chunks'));
587622
}
588623
} else {
589624
// assemble directly into the final file
@@ -667,13 +702,13 @@ private function convertToSabreException(\Exception $e) {
667702
}
668703
if ($e instanceof GenericEncryptionException) {
669704
// 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);
705+
throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]), 0, $e);
671706
}
672707
if ($e instanceof StorageNotAvailableException) {
673-
throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e);
708+
throw new ServiceUnavailable($this->l10n->t('Failed to write file contents: %1$s', [$e->getMessage()]), 0, $e);
674709
}
675710
if ($e instanceof NotFoundException) {
676-
throw new NotFound('File not found: ' . $e->getMessage(), 0, $e);
711+
throw new NotFound($this->l10n->t('File not found: %1$s', [$e->getMessage()]), 0, $e);
677712
}
678713

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

0 commit comments

Comments
 (0)