Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions lib/Service/GoogleDriveAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public function importFiles(
$filePathInDrive = $dirId === 'root' ? '/' . $fileItem['name'] : $directoriesById[$dirId]['name'] . '/' . $fileItem['name'];
$this->logFailedDownloadsForUser($rootImportFolder, $filePathInDrive);
}
} catch (\Throwable $e) {
} catch (Throwable $e) {
$this->logger->warning('Error while importing file', ['exception' => $e]);
$this->logger->debug('Skipping file ' . strval($fileItem['id']));
continue;
Expand Down Expand Up @@ -505,7 +505,7 @@ private function getFilesWithNameConflict(string $userId, string $dirId, bool $c
* @param Folder $folder
* @param string $fileName
* @throws LockedException
* @throws \OCP\Files\NotPermittedException
* @throws NotPermittedException
*/
private function logFailedDownloadsForUser(Folder $folder, string $fileName): void {
try {
Expand Down Expand Up @@ -703,7 +703,37 @@ private function getFile(string $userId, array $fileItem, Folder $saveFolder, st
// potentially a doc
$params = $this->getDocumentRequestParams($fileItem['mimeType'], $documentFormat);
$fileUrl = 'https://www.googleapis.com/drive/v3/files/' . urlencode((string)$fileItem['id']) . '/export';
return $this->downloadAndSaveFile($saveFolder, $fileName, $userId, $fileUrl, $fileItem, $params);
$result = $this->downloadAndSaveFile($saveFolder, $fileName, $userId, $fileUrl, $fileItem, $params);
if ($result !== null) {
return $result;
}
$this->logger->debug('Document export failed, trying through exportLinks', ['fileItem' => $fileItem]);
// Try a different method to export if that fails due to the file being too large
$exportUrl = '/drive/v3/files/' . urlencode((string)$fileItem['id']);
$result = $this->googleApiService->request($userId, $exportUrl, ['fields' => 'exportLinks']);
if (!isset($result['exportLinks'])) {
return null;
}
$fileUrl = null;
$formatStart = $documentFormat === 'openxml' ? 'application/vnd.openxmlformats-officedocument.' : 'application/vnd.oasis.opendocument.';
foreach ($result['exportLinks'] as $exportType => $potentialUrl) {
if (str_starts_with($exportType, $formatStart)) {
$fileUrl = $potentialUrl;
break;
}
}
// Fallback to PDF if needed
if ($fileUrl === null) {
if (isset($result['exportLinks']['application/pdf'])) {
$this->logger->debug('Falling back to pdf', ['fileItem' => $fileItem]);
$fileUrl = $result['exportLinks']['application/pdf'];
} else {
$this->logger->error('Could not export document', ['fileItem' => $fileItem]);
return null;
}
}
$this->logger->debug('Document export succeeded', ['fileItem' => $fileItem, 'fileUrl' => $fileUrl]);
return $this->downloadAndSaveFile($saveFolder, $fileName, $userId, $fileUrl, $fileItem);
} elseif (isset($fileItem['webContentLink'])) {
// classic file
$fileUrl = 'https://www.googleapis.com/drive/v3/files/' . urlencode((string)$fileItem['id']) . '?alt=media';
Expand Down