import path from "path";
import googleDrive from "../../google_drive.app.mjs";
import {
omitEmptyStringValues,
getFileStream,
streamToBuffer,
byteToMB,
} from "../../common/utils.mjs";
import {
GOOGLE_DRIVE_UPLOAD_TYPE_MEDIA,
GOOGLE_DRIVE_UPLOAD_TYPE_RESUMABLE,
} from "../../common/constants.mjs";
export default {
key: "google_drive-replace-file",
name: "Replace File",
description: "Upload a file that replaces an existing file. [See the docs](https://developers.google.com/drive/api/v3/reference/files/update) for more information",
version: "0.1.4",
type: "action",
props: {
googleDrive,
drive: {
propDefinition: [
googleDrive,
"watchedDrive",
],
optional: true,
},
fileId: {
propDefinition: [
googleDrive,
"fileId",
(c) => ({
drive: c.drive,
}),
],
optional: false,
description: "The file to update",
},
fileUrl: {
propDefinition: [
googleDrive,
"fileUrl",
],
},
filePath: {
propDefinition: [
googleDrive,
"filePath",
],
},
name: {
propDefinition: [
googleDrive,
"fileName",
],
label: "Name",
description: "The name of the new file (e.g., `myFile.csv`)",
},
mimeType: {
propDefinition: [
googleDrive,
"mimeType",
],
description: "The MIME type of the new file (e.g., `image/jpeg`)",
},
uploadType: {
propDefinition: [
googleDrive,
"uploadType",
],
optional: true,
},
},
async run({ $ }) {
const {
fileId,
fileUrl,
filePath,
name,
mimeType,
} = this;
let { uploadType } = this;
if (!fileUrl && !filePath) {
throw new Error("One of File URL and File Path is required.");
}
const fileStream = await getFileStream({
$,
fileUrl,
filePath,
});
if (!uploadType || uploadType === "") {
try {
const fileBuffer = await streamToBuffer(await getFileStream({
$,
fileUrl,
filePath,
}));
const bufferSize = byteToMB(Buffer.byteLength(fileBuffer));
uploadType = bufferSize > 5
? GOOGLE_DRIVE_UPLOAD_TYPE_RESUMABLE
: undefined;
} catch (err) {
console.log(err);
uploadType = undefined;
}
}
if (uploadType === GOOGLE_DRIVE_UPLOAD_TYPE_MEDIA) {
uploadType = undefined;
}
console.log(`Upload type: ${uploadType}`);
await this.googleDrive.updateFileMedia(fileId, fileStream, omitEmptyStringValues({
mimeType,
uploadType,
}));
const resp = await this.googleDrive.updateFile(fileId, omitEmptyStringValues({
name: name || path.basename(fileUrl || filePath),
mimeType,
uploadType,
}));
$.export("$summary", "Successfully replaced the file");
return resp;
},
};