Skip to content

Commit bb06b6c

Browse files
committed
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 <roeland@famdouma.nl>
1 parent cd56302 commit bb06b6c

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

apps/files_external/lib/Lib/Storage/AmazonS3.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,12 @@ public function stat($path) {
405405
*/
406406
private function getContentLength($path) {
407407
if (isset($this->filesCache[$path])) {
408-
return $this->filesCache[$path]['ContentLength'];
408+
return (int)$this->filesCache[$path]['ContentLength'];
409409
}
410410

411411
$result = $this->headObject($path);
412412
if (isset($result['ContentLength'])) {
413-
return $result['ContentLength'];
413+
return (int)$result['ContentLength'];
414414
}
415415

416416
return 0;
@@ -507,6 +507,12 @@ public function fopen($path, $mode) {
507507
switch ($mode) {
508508
case 'r':
509509
case 'rb':
510+
// Don't try to fetch empty files
511+
$stat = $this->stat($path);
512+
if (is_array($stat) && isset($stat['size']) && $stat['size'] === 0) {
513+
return fopen('php://memory', $mode);
514+
}
515+
510516
try {
511517
return $this->readObject($path);
512518
} catch (S3Exception $e) {

lib/private/Files/ObjectStore/ObjectStoreStorage.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ public function fopen($path, $mode) {
296296
case 'rb':
297297
$stat = $this->stat($path);
298298
if (is_array($stat)) {
299+
// Reading 0 sized files is a waste of time
300+
if (isset($stat['size']) && $stat['size'] === 0) {
301+
return fopen('php://memory', $mode);
302+
}
303+
299304
try {
300305
return $this->objectStore->readObject($this->getURN($stat['fileid']));
301306
} catch (NotFoundException $e) {

0 commit comments

Comments
 (0)