Skip to content

Commit 9c2132a

Browse files
Google drive - adding 'metadata' prop to 'upload file' action (#16949)
* pnpm * Incrementing 'upload file' action with new metadata prop * pnpm * pnpm * Adding JSON parse error handling * Package version bump * Update components/google_drive/actions/upload-file/upload-file.mjs * updates * @pipedream/platform version * pnpm-lock.yaml --------- Co-authored-by: michelle0927 <[email protected]> Co-authored-by: Michelle Bergeron <[email protected]>
1 parent 6a4420e commit 9c2132a

File tree

4 files changed

+65
-45
lines changed

4 files changed

+65
-45
lines changed

components/google_drive/actions/upload-file/upload-file.mjs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import googleDrive from "../../google_drive.app.mjs";
22
import path from "path";
33
import {
4-
getFileStream,
54
omitEmptyStringValues,
5+
parseObjectEntries,
66
} from "../../common/utils.mjs";
77
import { GOOGLE_DRIVE_UPLOAD_TYPE_MULTIPART } from "../../common/constants.mjs";
88
import {
9-
additionalProps, updateType,
10-
} from "../../common/filePathOrUrl.mjs";
9+
getFileStream, ConfigurationError,
10+
} from "@pipedream/platform";
1111

1212
export default {
1313
key: "google_drive-upload-file",
1414
name: "Upload File",
1515
description: "Upload a file to Google Drive. [See the documentation](https://developers.google.com/drive/api/v3/manage-uploads) for more information",
16-
version: "1.0.3",
16+
version: "1.1.0",
1717
type: "action",
18-
additionalProps,
1918
props: {
2019
googleDrive,
21-
updateType,
2220
drive: {
2321
propDefinition: [
2422
googleDrive,
@@ -38,21 +36,10 @@ export default {
3836
"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.",
3937
optional: true,
4038
},
41-
fileUrl: {
42-
propDefinition: [
43-
googleDrive,
44-
"fileUrl",
45-
],
46-
optional: true,
47-
hidden: true,
48-
},
49-
filePath: {
50-
propDefinition: [
51-
googleDrive,
52-
"filePath",
53-
],
54-
optional: true,
55-
hidden: true,
39+
file: {
40+
type: "string",
41+
label: "File",
42+
description: "Provide either a file URL or a path to a file in the /tmp directory (for example, /tmp/myFlie.pdf).",
5643
},
5744
name: {
5845
propDefinition: [
@@ -84,32 +71,38 @@ export default {
8471
"fileId",
8572
],
8673
label: "File to replace",
87-
description: "Id of the file to replace. Leave it empty to upload a new file.",
74+
description: "ID of the file to replace. Leave it empty to upload a new file.",
75+
optional: true,
76+
},
77+
metadata: {
78+
type: "object",
79+
label: "Metadata",
80+
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\" }`",
8881
optional: true,
8982
},
9083
},
9184
async run({ $ }) {
9285
const {
9386
parentId,
94-
fileUrl,
95-
filePath,
9687
name,
9788
mimeType,
9889
} = this;
9990
let { uploadType } = this;
10091
const driveId = this.googleDrive.getDriveId(this.drive);
10192

102-
const filename = name || path.basename(fileUrl || filePath);
93+
const filename = name || path.basename(this.file);
10394

104-
const file = await getFileStream({
105-
$,
106-
fileUrl,
107-
filePath: filePath?.startsWith("/tmp/")
108-
? filePath
109-
: `/tmp/${filePath}`,
110-
});
95+
const file = await getFileStream(this.file);
11196
console.log(`Upload type: ${uploadType}.`);
11297

98+
const metadata = this.metadata
99+
? parseObjectEntries(this.metadata)
100+
: undefined;
101+
102+
if (metadata?.mimeType && !mimeType) {
103+
throw new ConfigurationError(`Please include the file's original MIME type in the \`Mime Type\` prop. File will be converted to \`${metadata.mimeType}\`.`);
104+
}
105+
113106
let result = null;
114107
if (this.fileId) {
115108
await this.googleDrive.updateFileMedia(this.fileId, file, omitEmptyStringValues({
@@ -120,6 +113,7 @@ export default {
120113
name: filename,
121114
mimeType,
122115
uploadType,
116+
requestBody: metadata,
123117
}));
124118
$.export("$summary", `Successfully updated file, "${result.name}"`);
125119
} else {
@@ -130,6 +124,7 @@ export default {
130124
parentId,
131125
driveId,
132126
uploadType,
127+
requestBody: metadata,
133128
}));
134129
$.export("$summary", `Successfully uploaded a new file, "${result.name}"`);
135130
}

components/google_drive/common/utils.mjs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import fs from "fs";
2-
import { axios } from "@pipedream/platform";
2+
import {
3+
axios, ConfigurationError,
4+
} from "@pipedream/platform";
35
import {
46
MY_DRIVE_VALUE,
57
LEGACY_MY_DRIVE_VALUE,
@@ -257,6 +259,36 @@ function toSingleLineString(multiLineString) {
257259
.replace(/\s{2,}/g, " ");
258260
}
259261

262+
function optionalParseAsJSON(value) {
263+
try {
264+
return JSON.parse(value);
265+
} catch (e) {
266+
return value;
267+
}
268+
}
269+
270+
function parseObjectEntries(value = {}) {
271+
let obj;
272+
if (typeof value === "string") {
273+
try {
274+
obj = JSON.parse(value);
275+
} catch (e) {
276+
throw new ConfigurationError(`Invalid JSON string provided: ${e.message}`);
277+
}
278+
} else {
279+
obj = value;
280+
}
281+
return Object.fromEntries(
282+
Object.entries(obj).map(([
283+
key,
284+
value,
285+
]) => [
286+
key,
287+
optionalParseAsJSON(value),
288+
]),
289+
);
290+
}
291+
260292
export {
261293
MY_DRIVE_VALUE,
262294
isMyDrive,
@@ -269,4 +301,5 @@ export {
269301
getFilePaths,
270302
streamToBuffer,
271303
byteToMB,
304+
parseObjectEntries,
272305
};

components/google_drive/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/google_drive",
3-
"version": "0.9.2",
3+
"version": "0.10.0",
44
"description": "Pipedream Google_drive Components",
55
"main": "google_drive.app.mjs",
66
"keywords": [
@@ -11,7 +11,7 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"dependencies": {
1313
"@googleapis/drive": "^2.3.0",
14-
"@pipedream/platform": "^3.0.3",
14+
"@pipedream/platform": "^3.1.0",
1515
"cron-parser": "^4.9.0",
1616
"google-docs-mustaches": "^1.2.2",
1717
"got": "13.0.0",

pnpm-lock.yaml

Lines changed: 2 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)