Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compare cached filesize on download #34232

Merged
merged 1 commit into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion apps/dav/lib/Connector/Sabre/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
use OC\Files\View;
use OC\Metadata\FileMetadata;
use OCA\DAV\AppInfo\Application;
use OCA\DAV\Connector\Sabre\Exception\BadGateway;
use OCA\DAV\Connector\Sabre\Exception\EntityTooLarge;
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
use OCA\DAV\Connector\Sabre\Exception\Forbidden as DAVForbiddenException;
use OCA\DAV\Connector\Sabre\Exception\UnsupportedMediaType;
use OCA\DAV\Connector\Sabre\Exception\BadGateway;
use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\Files\EntityTooLargeException;
use OCP\Files\FileInfo;
Expand Down Expand Up @@ -491,9 +491,21 @@ public function get() {
} catch (\Exception $e) {
$this->convertToSabreException($e);
}

if ($res === false) {
throw new ServiceUnavailable($this->l10n->t('Could not open file'));
}

// comparing current file size with the one in DB
// if different, fix DB and refresh cache.
if ($this->getSize() !== $this->fileView->filesize($this->getPath())) {
$logger = \OC::$server->get(LoggerInterface::class);
$logger->warning('fixing cached size of file id=' . $this->getId());

$this->getFileInfo()->getStorage()->getUpdater()->update($this->getFileInfo()->getInternalPath());
$this->refreshInfo();
}

return $res;
} catch (GenericEncryptionException $e) {
// returning 503 will allow retry of the operation at a later point in time
Expand Down
14 changes: 12 additions & 2 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,23 @@ public function fopen($path, $mode) {
case 'rb':
$stat = $this->stat($path);
if (is_array($stat)) {
$filesize = $stat['size'] ?? 0;
// Reading 0 sized files is a waste of time
if (isset($stat['size']) && $stat['size'] === 0) {
if ($filesize === 0) {
ArtificialOwl marked this conversation as resolved.
Show resolved Hide resolved
return fopen('php://memory', $mode);
}

try {
return $this->objectStore->readObject($this->getURN($stat['fileid']));
$handle = $this->objectStore->readObject($this->getURN($stat['fileid']));
if ($handle === false) {
return false; // keep backward compatibility
}
$streamStat = fstat($handle);
$actualSize = $streamStat['size'] ?? -1;
if ($actualSize > -1 && $actualSize !== $filesize) {
$this->getCache()->update((int)$stat['fileid'], ['size' => $actualSize]);
}
return $handle;
} catch (NotFoundException $e) {
$this->logger->logException($e, [
'app' => 'objectstore',
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Files/ObjectStore/S3ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
use Aws\S3\Exception\S3MultipartUploadException;
use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Utils;
use OC\Files\Stream\SeekableHttpStream;
use GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;

trait S3ObjectTrait {
Expand All @@ -47,6 +47,7 @@ abstract protected function getCertificateBundlePath(): ?string;

/**
* @param string $urn the unified resource name used to identify the object
*
* @return resource stream with the read data
* @throws \Exception when something goes wrong, message will be logged
* @since 7.0.0
Expand Down Expand Up @@ -88,6 +89,7 @@ public function readObject($urn) {
});
}


/**
* Single object put helper
*
Expand Down
11 changes: 10 additions & 1 deletion lib/private/Files/Stream/SeekableHttpStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ public static function open(callable $callback) {

/** @var ?resource|closed-resource */
private $current;
/** @var int $offset offset of the current chunk */
private int $offset = 0;
/** @var int $length length of the current chunk */
private int $length = 0;
/** @var int $totalSize size of the full stream */
private int $totalSize = 0;
private bool $needReconnect = false;

private function reconnect(int $start): bool {
Expand Down Expand Up @@ -128,6 +132,9 @@ private function reconnect(int $start): bool {

$this->offset = $begin;
$this->length = $length;
if ($start === 0) {
$this->totalSize = $length;
}

return true;
}
Expand Down Expand Up @@ -211,7 +218,9 @@ public function stream_tell() {

public function stream_stat() {
if ($this->getCurrent()) {
return fstat($this->getCurrent());
$stat = fstat($this->getCurrent());
$stat['size'] = $this->totalSize;
return $stat;
} else {
return false;
}
Expand Down