Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
key: "google_drive-create-file-from-text",
name: "Create New File From Text",
description: "Create a new file from plain text. [See the documentation](https://developers.google.com/drive/api/v3/reference/files/create) for more information",
version: "0.1.7",
version: "0.2.0",
type: "action",
props: {
googleDrive,
Expand Down Expand Up @@ -44,24 +44,66 @@ export default {
optional: true,
default: "",
},
mimeType: {
type: "string",
label: "Conversion Format",
description:
"The [format](https://developers.google.com/drive/api/v3/ref-export-formats) in which the text is presented",
optional: true,
default: "text/plain",
options: [
{
value: "text/plain",
label: "Plain text",
},
{
value: "text/markdown",
label: "Markdown",
},
{
value: "text/html",
label: "HTML",
},
{
value: "application/rtf",
label: "Rich Text",
},
{
value: "text/csv",
label: "CSV",
},
],
},
},
async run({ $ }) {
const {
parentId,
name,
content,
mimeType,
} = this;
const file = Readable.from([
content,
]);
const drive = this.googleDrive.drive();
const driveId = this.googleDrive.getDriveId(this.drive);
const resp = await this.googleDrive.createFile({
mimeType: "text/plain",
file,
name,
parentId,
driveId,
const parent = parentId ?? driveId;

const { data: resp } = await drive.files.create({
supportsAllDrives: true,
media: {
mimeType,
body: file,
},
requestBody: {
name,
mimeType: "application/vnd.google-apps.document",
parents: [
parent,
],
},
Comment on lines +92 to +104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix conflicting MIME type configuration.

There's a potential issue with specifying two different MIME types:

  1. The user-selected format in media.mimeType
  2. A hardcoded Google Docs format in requestBody.mimeType

This might cause unexpected conversion behavior. Consider one of these approaches:

Option 1 (Recommended) - Keep the original format:

     const { data: resp } = await drive.files.create({
       supportsAllDrives: true,
       media: {
         mimeType,
         body: file,
       },
       requestBody: {
         name,
-        mimeType: "application/vnd.google-apps.document",
+        mimeType, // Use the same format as the media object
         parents: [
-          parent,
+          parentFolderId,
         ],
       },
     });

Option 2 - Add a separate property for conversion:

+    conversionFormat: {
+      type: "boolean",
+      label: "Convert to Google Docs",
+      description: "Whether to convert the file to Google Docs format",
+      optional: true,
+      default: false,
+    },

Then in the code:

     const { data: resp } = await drive.files.create({
       supportsAllDrives: true,
       media: {
         mimeType,
         body: file,
       },
       requestBody: {
         name,
-        mimeType: "application/vnd.google-apps.document",
+        mimeType: this.conversionFormat 
+          ? "application/vnd.google-apps.document" 
+          : mimeType,
         parents: [
-          parent,
+          parentFolderId,
         ],
       },
     });

Committable suggestion skipped: line range outside the PR's diff.

});

$.export("$summary", `Successfully created a new file, "${resp.name}"`);
return resp;
},
Expand Down
Loading