From af6b57cd0df02db80eceeea39b0df5e0e8727e10 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 7 Sep 2020 20:53:56 +0200 Subject: [PATCH] Fix reading empty files from objectstorage Since we try to do range requests this will fail hard. However since empty files are not that interesting to read anyways we just read from an emptry memory stream. Signed-off-by: Roeland Jago Douma --- apps/files_external/lib/Lib/Storage/AmazonS3.php | 10 ++++++++-- lib/private/Files/ObjectStore/ObjectStoreStorage.php | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/apps/files_external/lib/Lib/Storage/AmazonS3.php b/apps/files_external/lib/Lib/Storage/AmazonS3.php index c9862d7fb34a9..6838784204f73 100644 --- a/apps/files_external/lib/Lib/Storage/AmazonS3.php +++ b/apps/files_external/lib/Lib/Storage/AmazonS3.php @@ -403,12 +403,12 @@ public function stat($path) { */ private function getContentLength($path) { if (isset($this->filesCache[$path])) { - return $this->filesCache[$path]['ContentLength']; + return (int)$this->filesCache[$path]['ContentLength']; } $result = $this->headObject($path); if (isset($result['ContentLength'])) { - return $result['ContentLength']; + return (int)$result['ContentLength']; } return 0; @@ -505,6 +505,12 @@ public function fopen($path, $mode) { switch ($mode) { case 'r': case 'rb': + // Don't try to fetch empty files + $stat = $this->stat($path); + if (is_array($stat) && isset($stat['size']) && $stat['size'] === 0) { + return fopen('php://memory', $mode); + } + try { return $this->readObject($path); } catch (S3Exception $e) { diff --git a/lib/private/Files/ObjectStore/ObjectStoreStorage.php b/lib/private/Files/ObjectStore/ObjectStoreStorage.php index bd5b2d4777085..a752b27ca26e8 100644 --- a/lib/private/Files/ObjectStore/ObjectStoreStorage.php +++ b/lib/private/Files/ObjectStore/ObjectStoreStorage.php @@ -285,6 +285,11 @@ public function fopen($path, $mode) { case 'rb': $stat = $this->stat($path); if (is_array($stat)) { + // Reading 0 sized files is a waste of time + if (isset($stat['size']) && $stat['size'] === 0) { + return fopen('php://memory', $mode); + } + try { return $this->objectStore->readObject($this->getURN($stat['fileid'])); } catch (NotFoundException $e) {