Skip to content
15 changes: 13 additions & 2 deletions components/google_drive/actions/upload-file/upload-file.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "path";
import {
getFileStream,
omitEmptyStringValues,
parseObjectEntries,
} from "../../common/utils.mjs";
import { GOOGLE_DRIVE_UPLOAD_TYPE_MULTIPART } from "../../common/constants.mjs";
import {
Expand All @@ -13,7 +14,7 @@ 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.2",
version: "1.1.0",
type: "action",
additionalProps,
props: {
Expand Down Expand Up @@ -84,7 +85,13 @@ 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 availabl efields. Values will be parsed as JSON where applicable. Example: `{ \"description\": \"my file description\" }`",
optional: true,
},
},
Expand All @@ -110,6 +117,8 @@ export default {
});
console.log(`Upload type: ${uploadType}.`);

const metadata = parseObjectEntries(this.metadata);

let result = null;
if (this.fileId) {
await this.googleDrive.updateFileMedia(this.fileId, file, omitEmptyStringValues({
Expand All @@ -120,6 +129,7 @@ export default {
name: filename,
mimeType,
uploadType,
requestBody: metadata,
}));
$.export("$summary", `Successfully updated file, "${result.name}"`);
} else {
Expand All @@ -130,6 +140,7 @@ export default {
parentId,
driveId,
uploadType,
requestBody: metadata,
}));
$.export("$summary", `Successfully uploaded a new file, "${result.name}"`);
}
Expand Down
24 changes: 24 additions & 0 deletions components/google_drive/common/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,29 @@ function toSingleLineString(multiLineString) {
.replace(/\s{2,}/g, " ");
}

function optionalParseAsJSON(value) {
try {
return JSON.parse(value);
} catch (e) {
return value;
}
}

function parseObjectEntries(value = {}) {
const obj = typeof value === "string"
? JSON.parse(value)
: value;
return Object.fromEntries(
Object.entries(obj).map(([
key,
value,
]) => [
key,
optionalParseAsJSON(value),
]),
);
}

export {
MY_DRIVE_VALUE,
isMyDrive,
Expand All @@ -269,4 +292,5 @@ export {
getFilePaths,
streamToBuffer,
byteToMB,
parseObjectEntries,
};
2 changes: 1 addition & 1 deletion 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.1",
"version": "0.9.2",
"description": "Pipedream Google_drive Components",
"main": "google_drive.app.mjs",
"keywords": [
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

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