From 3a0a33b4c2399415c3def375ab7ba0c737d16620 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 14 Jan 2016 17:52:30 +0100 Subject: [PATCH] Use Guzzle stream to download files from GDrive The API library does not support streaming and always reads the full file into memory. This workaround copies the signed headers to a Guzzle request and returns the response as stream. --- apps/files_external/lib/google.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/apps/files_external/lib/google.php b/apps/files_external/lib/google.php index eba2ee7f9b58..d0734aa98e97 100644 --- a/apps/files_external/lib/google.php +++ b/apps/files_external/lib/google.php @@ -426,13 +426,23 @@ public function fopen($path, $mode) { } if (isset($downloadUrl)) { $request = new \Google_Http_Request($downloadUrl, 'GET', null, null); - $httpRequest = $this->client->getAuth()->authenticatedRequest($request); - if ($httpRequest->getResponseHttpCode() == 200) { - $tmpFile = \OCP\Files::tmpFile($ext); - $data = $httpRequest->getResponseBody(); - file_put_contents($tmpFile, $data); - return fopen($tmpFile, $mode); + $httpRequest = $this->client->getAuth()->sign($request); + // the library's service doesn't support streaming, so we use Guzzle instead + $client = \OC::$server->getHTTPClientService()->newClient(); + try { + $response = $client->get($downloadUrl, [ + 'headers' => $httpRequest->getRequestHeaders(), + 'stream' => true + ]); + } catch (RequestException $e) { + if ($e->getResponse()->getStatusCode() === 404) { + return false; + } else { + throw $e; + } } + + return $response->getBody(); } } return false;