Skip to content

Commit cb9a4d4

Browse files
tjdettDanielTosello
authored andcommitted
Streaming download from Swift external storage
Speeds up downloads as they no longer need to buffer completely on the ownCloud server before being sent to the client.
1 parent d4a4270 commit cb9a4d4

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

apps/files_external/lib/swift.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -314,27 +314,20 @@ public function fopen($path, $mode) {
314314
switch ($mode) {
315315
case 'r':
316316
case 'rb':
317-
$tmpFile = \OCP\Files::tmpFile();
318-
self::$tmpFiles[$tmpFile] = $path;
319-
try {
320-
$object = $this->getContainer()->getObject($path);
321-
} catch (ClientErrorResponseException $e) {
322-
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
323-
return false;
324-
} catch (Exception\ObjectNotFoundException $e) {
325-
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
326-
return false;
327-
}
328317
try {
329-
$objectContent = $object->getContent();
330-
$objectContent->rewind();
331-
$stream = $objectContent->getStream();
332-
file_put_contents($tmpFile, $stream);
333-
} catch (Exceptions\IOError $e) {
318+
$c = $this->getContainer();
319+
$streamFactory = new \Guzzle\Stream\PhpStreamRequestFactory();
320+
$streamInterface = $streamFactory->fromRequest(
321+
$c->getClient()
322+
->get($c->getUrl($path)));
323+
$streamInterface->rewind();
324+
$stream = $streamInterface->getStream();
325+
stream_context_set_option($stream, 'swift','content', $streamInterface);
326+
return $stream;
327+
} catch (\Guzzle\Http\Exception\BadResponseException $e) {
334328
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
335329
return false;
336330
}
337-
return fopen($tmpFile, 'r');
338331
case 'w':
339332
case 'wb':
340333
case 'a':

0 commit comments

Comments
 (0)