]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use Guzzle stream to download files from GDrive
authorVincent Petry <pvince81@owncloud.com>
Thu, 14 Jan 2016 16:52:30 +0000 (17:52 +0100)
committerVincent Petry <pvince81@owncloud.com>
Thu, 14 Jan 2016 16:52:30 +0000 (17:52 +0100)
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

index 72ebd4e821dd88a550c3603c9abaa39b7e2abdf1..8a9ffaf7d37d10f8a0d29270dee1c82587cb90c1 100644 (file)
@@ -426,13 +426,23 @@ class Google extends \OC\Files\Storage\Common {
                                        }
                                        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;