Skip to content

Commit d301e69

Browse files
committed
fix(downloadFile): write directly to file using Guzzle's "sink" option
Signed-off-by: Oleksander Piskun <oleksandr2088@icloud.com>
1 parent fd54d2e commit d301e69

File tree

2 files changed

+5
-15
lines changed

2 files changed

+5
-15
lines changed

lib/Service/DropboxAPIService.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function downloadFile(string $accessToken, string $refreshToken, string $
159159
'User-Agent' => 'Nextcloud Dropbox integration',
160160
'Dropbox-API-Arg' => json_encode(['path' => $fileId]),
161161
],
162-
'stream' => true,
162+
'sink' => $resource,
163163
];
164164

165165
$response = $this->client->post($url, $options);
@@ -168,25 +168,13 @@ public function downloadFile(string $accessToken, string $refreshToken, string $
168168
if ($respCode >= 400) {
169169
return ['error' => $this->l10n->t('Bad credentials')];
170170
}
171-
172-
$body = $response->getBody();
173-
if (is_resource($body)) {
174-
while (!feof($body)) {
175-
// write ~5 MB chunks
176-
$chunk = fread($body, 5000000);
177-
fwrite($resource, $chunk);
178-
}
179-
} else {
180-
fwrite($resource, $body);
181-
}
182-
183171
return ['success' => true];
184172
} catch (ServerException|ClientException $e) {
185173
$response = $e->getResponse();
186174
if ($response->getStatusCode() === 401) {
187175
if ($try > 3) {
188176
// impossible to refresh the token
189-
$this->logger->info('Received the following response upon trying to download a file: ' . $response->getBody()->getContents());
177+
$this->logger->info('Could not access file due to failed authentication.');
190178
return ['error' => $this->l10n->t('Could not access file due to failed authentication.')];
191179
}
192180
$this->logger->info('Trying to REFRESH the access token', ['app' => $this->appName]);

lib/Service/DropboxStorageAPIService.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ private function getFile(string $accessToken, string $refreshToken, string $clie
312312
}
313313
return null;
314314
}
315-
fclose($resource);
315+
if (is_resource($resource)) {
316+
fclose($resource);
317+
}
316318
if (isset($fileItem['server_modified'])) {
317319
$d = new DateTime($fileItem['server_modified']);
318320
$ts = $d->getTimestamp();

0 commit comments

Comments
 (0)