refactor(dav): simplify length header handling

Reduce nesting and drop duplicated sections.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
pull/51491/head
Ferdinand Thiessen 7 months ago
parent 703af4bd44
commit 6141ab1da6
No known key found for this signature in database
GPG Key ID: 45FAE7268762B400
  1. 56
      apps/dav/lib/Connector/Sabre/File.php

@ -232,42 +232,36 @@ class File extends Node implements IFile {
fclose($target);
}
if ($result === false) {
$expected = -1;
$lengthHeader = $this->request->getHeader('content-length');
if ($lengthHeader) {
$expected = (int)$lengthHeader;
}
if ($expected !== 0) {
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),
],
)
);
}
$lengthHeader = $this->request->getHeader('content-length');
$expected = $lengthHeader !== '' ? (int)$lengthHeader : -1;
if ($result === false && $expected >= 0) {
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),
],
)
);
}
// if content length is sent by client:
// double check if the file was fully received
// compare expected and actual size
$lengthHeader = $this->request->getHeader('content-length');
if ($lengthHeader && $this->request->getMethod() === 'PUT') {
$expected = (int)$lengthHeader;
if ($count !== $expected) {
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),
],
)
);
}
if ($expected >= 0
&& $expected !== $count
&& $this->request->getMethod() === 'PUT'
) {
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) {
if ($e instanceof LockedException) {

Loading…
Cancel
Save