From cb9a4d4cdcb0bf53121c39d2c918c16a027f69bd Mon Sep 17 00:00:00 2001 From: Tim Dettrick Date: Thu, 28 May 2015 18:18:06 +1000 Subject: [PATCH 1/3] 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. --- apps/files_external/lib/swift.php | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/apps/files_external/lib/swift.php b/apps/files_external/lib/swift.php index a64a02a4ed..8311ad202a 100644 --- a/apps/files_external/lib/swift.php +++ b/apps/files_external/lib/swift.php @@ -314,27 +314,20 @@ class Swift extends \OC\Files\Storage\Common { switch ($mode) { case 'r': case 'rb': - $tmpFile = \OCP\Files::tmpFile(); - self::$tmpFiles[$tmpFile] = $path; try { - $object = $this->getContainer()->getObject($path); - } catch (ClientErrorResponseException $e) { - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); - return false; - } catch (Exception\ObjectNotFoundException $e) { + $c = $this->getContainer(); + $streamFactory = new \Guzzle\Stream\PhpStreamRequestFactory(); + $streamInterface = $streamFactory->fromRequest( + $c->getClient() + ->get($c->getUrl($path))); + $streamInterface->rewind(); + $stream = $streamInterface->getStream(); + stream_context_set_option($stream, 'swift','content', $streamInterface); + return $stream; + } catch (\Guzzle\Http\Exception\BadResponseException $e) { \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); return false; } - try { - $objectContent = $object->getContent(); - $objectContent->rewind(); - $stream = $objectContent->getStream(); - file_put_contents($tmpFile, $stream); - } catch (Exceptions\IOError $e) { - \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); - return false; - } - return fopen($tmpFile, 'r'); case 'w': case 'wb': case 'a': From 41f1feaf2304c8c2367168a7f3ac2793f0fb7eb2 Mon Sep 17 00:00:00 2001 From: Daniel Tosello Date: Thu, 10 Dec 2015 16:36:28 +1100 Subject: [PATCH 2/3] Fixing swift fopen by ensuring stream is a valid resource --- apps/files_external/lib/swift.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/files_external/lib/swift.php b/apps/files_external/lib/swift.php index 8311ad202a..02bfc44c36 100644 --- a/apps/files_external/lib/swift.php +++ b/apps/files_external/lib/swift.php @@ -323,7 +323,10 @@ class Swift extends \OC\Files\Storage\Common { $streamInterface->rewind(); $stream = $streamInterface->getStream(); stream_context_set_option($stream, 'swift','content', $streamInterface); - return $stream; + if(is_resource($stream)) { + return $stream; + } + return false; } catch (\Guzzle\Http\Exception\BadResponseException $e) { \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); return false; From 8783eb99f7db7c8d08f2b33223a9a547438c021a Mon Sep 17 00:00:00 2001 From: Daniel Tosello Date: Tue, 12 Jan 2016 16:47:52 +1100 Subject: [PATCH 3/3] Modified swift handling to explicitly set 404 responses to false --- apps/files_external/lib/swift.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/files_external/lib/swift.php b/apps/files_external/lib/swift.php index 02bfc44c36..62c28f19b4 100644 --- a/apps/files_external/lib/swift.php +++ b/apps/files_external/lib/swift.php @@ -323,7 +323,8 @@ class Swift extends \OC\Files\Storage\Common { $streamInterface->rewind(); $stream = $streamInterface->getStream(); stream_context_set_option($stream, 'swift','content', $streamInterface); - if(is_resource($stream)) { + if(!strrpos($streamInterface + ->getMetaData('wrapper_data')[0], '404 Not Found')) { return $stream; } return false;