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)
Murderlon added a commit that referenced this pull request May 28, 2024
* 4.x: (38 commits)
  docs: assume tree-shaking bundler is the most common case (#5160)
  @uppy/core: remove `reason` (#5159)
  Release: uppy@4.0.0-beta.9 (#5194)
  Release: uppy@3.25.5 (#5193)
  @uppy/companion: remove `chalk` from dependencies (#5178)
  @uppy/transloadit: do not cancel assembly when removing all files (#5191)
  @uppy/xhr-upload: fix regression for lowercase HTTP methods (#5179)
  meta: improve changelog generator (#5190)
  Release: uppy@4.0.0-beta.8 (#5189)
  examples: add SvelteKit example (#5181)
  @uppy/companion: fix missing `await`
  Release: uppy@3.25.4 (#5188)
  @uppy/svelte: do not attempt removing plugin before it's created (#5186)
  Update facebook.mdx
  fixup! @uppy/tus: fix no headers passed to companion if argument is a function (#5182)
  @uppy/core: resolve some (breaking) TODOs (#4824)
  @uppy/tus: fix no headers passed to companion if argument is a function (#5182)
  @uppy/companion: fix google drive gsuite export large size (#5144)
  @uppy/companion: encode `uploadId` (#5168)
  @uppy/companion: bump `express-session` (#5177)
  ...
Murderlon added a commit that referenced this pull request Jun 10, 2024
* main: (262 commits)
  Release: uppy@3.26.0 (#5223)
  meta: remove Companion's `prepublishOnly` (#5220)
  docs: document clearUploadedFiles (#5204)
  @uppy/webcam: add missing types for `recordedVideo` (#5208)
  @uppy/core: check capabilities in clearUploadedFiles (#5201)
  PartialTree - change the `maxTotalFileSize` error (#5203)
  @uppy/transloadit: remove `updateNumberOfFilesInAssembly` (#5202)
  @uppy/aws-s3: resolve all headers on response (#5195)
  Improve provider docs: OneDrive (#5196)
  Release: uppy@3.25.5 (#5193)
  @uppy/transloadit: do not cancel assembly when removing all files (#5191)
  @uppy/xhr-upload: fix regression for lowercase HTTP methods (#5179)
  meta: improve changelog generator (#5190)
  Release: uppy@3.25.4 (#5188)
  @uppy/svelte: do not attempt removing plugin before it's created (#5186)
  Update facebook.mdx
  fixup! @uppy/tus: fix no headers passed to companion if argument is a function (#5182)
  @uppy/tus: fix no headers passed to companion if argument is a function (#5182)
  @uppy/companion: fix google drive gsuite export large size (#5144)
  Improve provider docs: Box & Zoom (#5166)
  ...
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.

3 participants