Skip to content
59 changes: 27 additions & 32 deletions components/google_drive/actions/upload-file/upload-file.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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: [
Expand Down Expand Up @@ -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({
Expand All @@ -120,6 +113,7 @@ export default {
name: filename,
mimeType,
uploadType,
requestBody: metadata,
}));
$.export("$summary", `Successfully updated file, "${result.name}"`);
} else {
Expand All @@ -130,6 +124,7 @@ export default {
parentId,
driveId,
uploadType,
requestBody: metadata,
}));
$.export("$summary", `Successfully uploaded a new file, "${result.name}"`);
}
Expand Down
35 changes: 34 additions & 1 deletion components/google_drive/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -269,4 +301,5 @@ export {
getFilePaths,
streamToBuffer,
byteToMB,
parseObjectEntries,
};
4 changes: 2 additions & 2 deletions components/google_drive/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -11,7 +11,7 @@
"author": "Pipedream <[email protected]> (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",
Expand Down
12 changes: 2 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading