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.
This commit is contained in:
parent
3f64d37f2a
commit
d608c37c90
|
@ -426,14 +426,24 @@ class Google extends \OC\Files\Storage\Common {
|
||||||
}
|
}
|
||||||
if (isset($downloadUrl)) {
|
if (isset($downloadUrl)) {
|
||||||
$request = new \Google_Http_Request($downloadUrl, 'GET', null, null);
|
$request = new \Google_Http_Request($downloadUrl, 'GET', null, null);
|
||||||
$httpRequest = $this->client->getAuth()->authenticatedRequest($request);
|
$httpRequest = $this->client->getAuth()->sign($request);
|
||||||
if ($httpRequest->getResponseHttpCode() == 200) {
|
// the library's service doesn't support streaming, so we use Guzzle instead
|
||||||
$tmpFile = \OCP\Files::tmpFile($ext);
|
$client = \OC::$server->getHTTPClientService()->newClient();
|
||||||
$data = $httpRequest->getResponseBody();
|
try {
|
||||||
file_put_contents($tmpFile, $data);
|
$response = $client->get($downloadUrl, [
|
||||||
return fopen($tmpFile, $mode);
|
'headers' => $httpRequest->getRequestHeaders(),
|
||||||
|
'stream' => true
|
||||||
|
]);
|
||||||
|
} catch (RequestException $e) {
|
||||||
|
if ($e->getResponse()->getStatusCode() === 404) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $response->getBody();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
case 'w':
|
case 'w':
|
||||||
|
|
Loading…
Reference in New Issue