Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/companion: fix google drive gsuite export large size #5144

Merged

Conversation

milannakum
Copy link
Contributor

Fix for GSuite exporting large file based on the StackOverflow answer is working, just have to use the specific export link provided by API based on the mimeType.

Copy link
Contributor

github-actions bot commented May 3, 2024

Diff output files
diff --git a/packages/@uppy/companion/lib/server/provider/drive/index.js b/packages/@uppy/companion/lib/server/provider/drive/index.js
index 872cf5c..02359f6 100644
--- a/packages/@uppy/companion/lib/server/provider/drive/index.js
+++ b/packages/@uppy/companion/lib/server/provider/drive/index.js
@@ -24,7 +24,7 @@ const mockAccessTokenExpiredError = undefined;
 // const mockAccessTokenExpiredError = true
 // const mockAccessTokenExpiredError = ''
 const DRIVE_FILE_FIELDS =
-  "kind,id,imageMediaMetadata,name,mimeType,ownedByMe,size,modifiedTime,iconLink,thumbnailLink,teamDriveId,videoMediaMetadata,shortcutDetails(targetId,targetMimeType)";
+  "kind,id,imageMediaMetadata,name,mimeType,ownedByMe,size,modifiedTime,iconLink,thumbnailLink,teamDriveId,videoMediaMetadata,exportLinks,shortcutDetails(targetId,targetMimeType)";
 const DRIVE_FILES_FIELDS = `kind,nextPageToken,incompleteSearch,files(${DRIVE_FILE_FIELDS})`;
 // using wildcard to get all 'drive' fields because specifying fields seems no to work for the /drives endpoint
 const SHARED_DRIVE_FIELDS = "*";
@@ -149,15 +149,29 @@ class Drive extends Provider {
       "provider.drive.download.error",
       async () => {
         const client = getClient({ token });
-        const { mimeType, id } = await getStats({ id: idIn, token });
+        const { mimeType, id, exportLinks } = await getStats({ id: idIn, token });
         let stream;
         if (isGsuiteFile(mimeType)) {
           const mimeType2 = getGsuiteExportType(mimeType);
           logger.info(`calling google file export for ${id} to ${mimeType2}`, "provider.drive.export");
-          stream = client.stream.get(`files/${encodeURIComponent(id)}/export`, {
-            searchParams: { supportsAllDrives: true, mimeType: mimeType2 },
-            responseType: "json",
-          });
+          // GSuite files exported with large converted size results in error using standard export method.
+          // Error message: "This file is too large to be exported.".
+          // Issue logged in Google APIs: https://github.com/googleapis/google-api-nodejs-client/issues/3446
+          // Implemented based on the answer from StackOverflow: https://stackoverflow.com/a/59168288
+          const mimeTypeExportLink = exportLinks === null || exportLinks === void 0 ? void 0 : exportLinks[mimeType2];
+          if (mimeTypeExportLink) {
+            const gSuiteFilesClient = got.extend({
+              headers: {
+                authorization: `Bearer ${token}`,
+              },
+            });
+            stream = gSuiteFilesClient.stream.get(mimeTypeExportLink, { responseType: "json" });
+          } else {
+            stream = client.stream.get(`files/${encodeURIComponent(id)}/export`, {
+              searchParams: { supportsAllDrives: true, mimeType: mimeType2 },
+              responseType: "json",
+            });
+          }
         } else {
           stream = client.stream.get(`files/${encodeURIComponent(id)}`, {
             searchParams: { alt: "media", supportsAllDrives: true },

@mifi
Copy link
Contributor

mifi commented May 8, 2024

thanks for your pr. Tests and linters are failing

@milannakum milannakum force-pushed the gdrive-suite-export-too-large-size-fix branch from 9086520 to 03ddbbe Compare May 17, 2024 12:28
@milannakum
Copy link
Contributor Author

Done, all checks should be passing now.

@Murderlon Murderlon merged commit b5539de into transloadit:main May 21, 2024
20 checks passed
@Murderlon
Copy link
Member

Thanks!

@github-actions github-actions bot mentioned this pull request May 22, 2024
github-actions bot added a commit that referenced this pull request May 22, 2024
| Package         | Version | Package         | Version |
| --------------- | ------- | --------------- | ------- |
| @uppy/companion |  4.13.3 | @uppy/tus       |   3.5.5 |
| @uppy/svelte    |   3.1.5 | uppy            |  3.25.4 |

- @uppy/svelte: do not attempt removing plugin before it's created (Antoine du Hamel / #5186)
- docs: Update `facebook.mdx` (Evgenia Karunus)
- @uppy/tus: fix no headers passed to companion if argument is a function (netdown / #5182)
- @uppy/companion: fix google drive gsuite export large size (Milan Nakum / #5144)
- meta: Improve provider docs: Box & Zoom (Evgenia Karunus / #5166)
- meta: add MDX file to `lint-staged` list (Antoine du Hamel / #5174)
- @uppy/companion: handle ws `'error'` event (Mikael Finstad / #5167)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants