diff --git a/components/google_drive/actions/upload-file/upload-file.mjs b/components/google_drive/actions/upload-file/upload-file.mjs index b6f05be40638b..bc09d58a27c6d 100644 --- a/components/google_drive/actions/upload-file/upload-file.mjs +++ b/components/google_drive/actions/upload-file/upload-file.mjs @@ -1,24 +1,22 @@ import googleDrive from "../../google_drive.app.mjs"; import path from "path"; import { - getFileStream, omitEmptyStringValues, + parseObjectEntries, } from "../../common/utils.mjs"; import { GOOGLE_DRIVE_UPLOAD_TYPE_MULTIPART } from "../../common/constants.mjs"; import { - additionalProps, updateType, -} from "../../common/filePathOrUrl.mjs"; + getFileStream, ConfigurationError, +} from "@pipedream/platform"; export default { key: "google_drive-upload-file", name: "Upload File", description: "Upload a file to Google Drive. [See the documentation](https://developers.google.com/drive/api/v3/manage-uploads) for more information", - version: "1.0.3", + version: "1.1.0", type: "action", - additionalProps, props: { googleDrive, - updateType, drive: { propDefinition: [ googleDrive, @@ -38,21 +36,10 @@ export default { "The folder you want to upload the file to. If not specified, the file will be placed directly in the drive's top-level folder.", optional: true, }, - fileUrl: { - propDefinition: [ - googleDrive, - "fileUrl", - ], - optional: true, - hidden: true, - }, - filePath: { - propDefinition: [ - googleDrive, - "filePath", - ], - optional: true, - hidden: true, + file: { + type: "string", + label: "File", + description: "Provide either a file URL or a path to a file in the /tmp directory (for example, /tmp/myFlie.pdf).", }, name: { propDefinition: [ @@ -84,32 +71,38 @@ export default { "fileId", ], label: "File to replace", - description: "Id of the file to replace. Leave it empty to upload a new file.", + description: "ID of the file to replace. Leave it empty to upload a new file.", + optional: true, + }, + metadata: { + type: "object", + label: "Metadata", + description: "Additional metadata to supply in the upload. [See the documentation](https://developers.google.com/workspace/drive/api/reference/rest/v3/files) for information on available fields. Values will be parsed as JSON where applicable. Example: `{ \"description\": \"my file description\" }`", optional: true, }, }, async run({ $ }) { const { parentId, - fileUrl, - filePath, name, mimeType, } = this; let { uploadType } = this; const driveId = this.googleDrive.getDriveId(this.drive); - const filename = name || path.basename(fileUrl || filePath); + const filename = name || path.basename(this.file); - const file = await getFileStream({ - $, - fileUrl, - filePath: filePath?.startsWith("/tmp/") - ? filePath - : `/tmp/${filePath}`, - }); + const file = await getFileStream(this.file); console.log(`Upload type: ${uploadType}.`); + const metadata = this.metadata + ? parseObjectEntries(this.metadata) + : undefined; + + if (metadata?.mimeType && !mimeType) { + throw new ConfigurationError(`Please include the file's original MIME type in the \`Mime Type\` prop. File will be converted to \`${metadata.mimeType}\`.`); + } + let result = null; if (this.fileId) { await this.googleDrive.updateFileMedia(this.fileId, file, omitEmptyStringValues({ @@ -120,6 +113,7 @@ export default { name: filename, mimeType, uploadType, + requestBody: metadata, })); $.export("$summary", `Successfully updated file, "${result.name}"`); } else { @@ -130,6 +124,7 @@ export default { parentId, driveId, uploadType, + requestBody: metadata, })); $.export("$summary", `Successfully uploaded a new file, "${result.name}"`); } diff --git a/components/google_drive/common/utils.mjs b/components/google_drive/common/utils.mjs index 9ff1989b5737f..d3781b034296d 100644 --- a/components/google_drive/common/utils.mjs +++ b/components/google_drive/common/utils.mjs @@ -1,5 +1,7 @@ import fs from "fs"; -import { axios } from "@pipedream/platform"; +import { + axios, ConfigurationError, +} from "@pipedream/platform"; import { MY_DRIVE_VALUE, LEGACY_MY_DRIVE_VALUE, @@ -257,6 +259,36 @@ function toSingleLineString(multiLineString) { .replace(/\s{2,}/g, " "); } +function optionalParseAsJSON(value) { + try { + return JSON.parse(value); + } catch (e) { + return value; + } +} + +function parseObjectEntries(value = {}) { + let obj; + if (typeof value === "string") { + try { + obj = JSON.parse(value); + } catch (e) { + throw new ConfigurationError(`Invalid JSON string provided: ${e.message}`); + } + } else { + obj = value; + } + return Object.fromEntries( + Object.entries(obj).map(([ + key, + value, + ]) => [ + key, + optionalParseAsJSON(value), + ]), + ); +} + export { MY_DRIVE_VALUE, isMyDrive, @@ -269,4 +301,5 @@ export { getFilePaths, streamToBuffer, byteToMB, + parseObjectEntries, }; diff --git a/components/google_drive/package.json b/components/google_drive/package.json index d1374804cdda1..9b571a80a1fe6 100644 --- a/components/google_drive/package.json +++ b/components/google_drive/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/google_drive", - "version": "0.9.2", + "version": "0.10.0", "description": "Pipedream Google_drive Components", "main": "google_drive.app.mjs", "keywords": [ @@ -11,7 +11,7 @@ "author": "Pipedream (https://pipedream.com/)", "dependencies": { "@googleapis/drive": "^2.3.0", - "@pipedream/platform": "^3.0.3", + "@pipedream/platform": "^3.1.0", "cron-parser": "^4.9.0", "google-docs-mustaches": "^1.2.2", "got": "13.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3113eed32cfb1..515cb0d603480 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5548,8 +5548,8 @@ importers: specifier: ^2.3.0 version: 2.4.0 '@pipedream/platform': - specifier: ^3.0.3 - version: 3.0.3 + specifier: ^3.1.0 + version: 3.1.0 cron-parser: specifier: ^4.9.0 version: 4.9.0 @@ -15671,14 +15671,6 @@ importers: specifier: ^6.0.0 version: 6.2.0 - modelcontextprotocol/node_modules2/@modelcontextprotocol/sdk/dist/cjs: {} - - modelcontextprotocol/node_modules2/@modelcontextprotocol/sdk/dist/esm: {} - - modelcontextprotocol/node_modules2/zod-to-json-schema/dist/cjs: {} - - modelcontextprotocol/node_modules2/zod-to-json-schema/dist/esm: {} - packages/ai: dependencies: '@pipedream/sdk':