|
| 1 | +const googleDrive = require("../../google_drive.app"); |
| 2 | +const fs = require("fs"); |
| 3 | +const got = require("got"); |
| 4 | +const isoLanguages = require("../language-codes.js"); |
| 5 | +const googleMimeTypes = require("../google-mime-types.js"); |
| 6 | +const mimeDb = require("mime-db"); |
| 7 | +const mimeTypes = Object.keys(mimeDb); |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + key: "google_drive-create-file", |
| 11 | + name: "Create a New File", |
| 12 | + description: "Create a new file from a URL or /tmp/filepath.", |
| 13 | + version: "0.0.1", |
| 14 | + type: "action", |
| 15 | + props: { |
| 16 | + googleDrive, |
| 17 | + drive: { |
| 18 | + propDefinition: [ |
| 19 | + googleDrive, |
| 20 | + "watchedDrive", |
| 21 | + ], |
| 22 | + }, |
| 23 | + parent: { |
| 24 | + type: "string", |
| 25 | + label: "Parent Folder", |
| 26 | + description: |
| 27 | + `The ID of the parent folder which contains the file. If not specified as part of a |
| 28 | + create request, the file will be placed directly in the user's My Drive folder.`, |
| 29 | + optional: true, |
| 30 | + async options({ prevContext }) { |
| 31 | + const { nextPageToken } = prevContext; |
| 32 | + let results; |
| 33 | + if (this.drive === "myDrive") { |
| 34 | + results = await this.googleDrive.listFolderOptions(nextPageToken); |
| 35 | + } else { |
| 36 | + results = await this.googleDrive.listFolderOptions(nextPageToken, { |
| 37 | + corpora: "drive", |
| 38 | + driveId: this.drive, |
| 39 | + includeItemsFromAllDrives: true, |
| 40 | + supportsAllDrives: true, |
| 41 | + }); |
| 42 | + } |
| 43 | + return results; |
| 44 | + }, |
| 45 | + }, |
| 46 | + uploadType: { |
| 47 | + type: "string", |
| 48 | + label: "Upload Type", |
| 49 | + description: `The type of upload request to the /upload URI. If you are uploading data |
| 50 | + (using an /upload URI), this field is required. If you are creating a metadata-only file, |
| 51 | + this field is not required. |
| 52 | + media - Simple upload. Upload the media only, without any metadata. |
| 53 | + multipart - Multipart upload. Upload both the media and its metadata, in a single request. |
| 54 | + resumable - Resumable upload. Upload the file in a resumable fashion, using a series of |
| 55 | + at least two requests where the first request includes the metadata.`, |
| 56 | + options: [ |
| 57 | + "media", |
| 58 | + "multipart", |
| 59 | + "resumable", |
| 60 | + ], |
| 61 | + }, |
| 62 | + fileUrl: { |
| 63 | + type: "string", |
| 64 | + label: "File URL", |
| 65 | + description: |
| 66 | + `The URL of the file you want to upload to Google Drive. Must specify either File URL |
| 67 | + or File Path.`, |
| 68 | + optional: true, |
| 69 | + }, |
| 70 | + filePath: { |
| 71 | + type: "string", |
| 72 | + label: "File Path", |
| 73 | + description: |
| 74 | + "The path to the file, e.g. /tmp/myFile.csv . Must specify either File URL or File Path.", |
| 75 | + optional: true, |
| 76 | + }, |
| 77 | + ignoreDefaultVisibility: { |
| 78 | + type: "boolean", |
| 79 | + label: "Ignore Default Visibility", |
| 80 | + description: `Whether to ignore the domain's default visibility settings for the created |
| 81 | + file. Domain administrators can choose to make all uploaded files visible to the domain |
| 82 | + by default; this parameter bypasses that behavior for the request. Permissions are still |
| 83 | + inherited from parent folders.`, |
| 84 | + default: false, |
| 85 | + }, |
| 86 | + includePermissionsForView: { |
| 87 | + type: "string", |
| 88 | + label: "Include Permissions For View", |
| 89 | + description: |
| 90 | + `Specifies which additional view's permissions to include in the response. Only |
| 91 | + 'published' is supported.`, |
| 92 | + optional: true, |
| 93 | + options: [ |
| 94 | + "published", |
| 95 | + ], |
| 96 | + }, |
| 97 | + keepRevisionForever: { |
| 98 | + type: "boolean", |
| 99 | + label: "Keep Revision Forever", |
| 100 | + description: |
| 101 | + `Whether to set the 'keepForever' field in the new head revision. This is only applicable |
| 102 | + to files with binary content in Google Drive. Only 200 revisions for the file can be kept |
| 103 | + forever. If the limit is reached, try deleting pinned revisions.`, |
| 104 | + default: false, |
| 105 | + }, |
| 106 | + ocrLanguage: { |
| 107 | + type: "string", |
| 108 | + label: "OCR Language", |
| 109 | + description: |
| 110 | + "A language hint for OCR processing during image import (ISO 639-1 code).", |
| 111 | + optional: true, |
| 112 | + options: isoLanguages, |
| 113 | + }, |
| 114 | + useContentAsIndexableText: { |
| 115 | + type: "boolean", |
| 116 | + label: "Use Content As Indexable Text", |
| 117 | + description: |
| 118 | + "Whether to use the uploaded content as indexable text.", |
| 119 | + default: false, |
| 120 | + }, |
| 121 | + supportsAllDrives: { |
| 122 | + type: "boolean", |
| 123 | + label: "Supports All Drives", |
| 124 | + description: |
| 125 | + `Whether to include shared drives. Set to 'true' if saving to a shared drive. |
| 126 | + Defaults to 'false' if left blank.`, |
| 127 | + optional: true, |
| 128 | + }, |
| 129 | + contentHintsIndexableText: { |
| 130 | + type: "string", |
| 131 | + label: "Content Hints Indexable Text", |
| 132 | + description: |
| 133 | + `Text to be indexed for the file to improve fullText queries. This is limited to 128KB in |
| 134 | + length and may contain HTML elements.`, |
| 135 | + optional: true, |
| 136 | + }, |
| 137 | + contentRestrictionsReadOnly: { |
| 138 | + type: "boolean", |
| 139 | + label: "Content Restrictions Read Only", |
| 140 | + description: |
| 141 | + `Whether the content of the file is read-only. If a file is read-only, a new revision of |
| 142 | + the file may not be added, comments may not be added or modified, and the title of the file |
| 143 | + may not be modified.`, |
| 144 | + optional: true, |
| 145 | + }, |
| 146 | + contentRestrictionsReason: { |
| 147 | + type: "string", |
| 148 | + label: "Content Restrictions Reason", |
| 149 | + description: |
| 150 | + `Reason for why the content of the file is restricted. This is only mutable on requests |
| 151 | + that also set readOnly=true.`, |
| 152 | + optional: true, |
| 153 | + }, |
| 154 | + copyRequiresWriterPermission: { |
| 155 | + type: "boolean", |
| 156 | + label: "Copy Requires Writer Permission", |
| 157 | + description: |
| 158 | + `Whether the options to copy, print, or download this file, should be disabled for |
| 159 | + readers and commenters.`, |
| 160 | + optional: true, |
| 161 | + }, |
| 162 | + description: { |
| 163 | + type: "string", |
| 164 | + label: "Description", |
| 165 | + description: "A short description of the file.", |
| 166 | + optional: true, |
| 167 | + }, |
| 168 | + folderColorRgb: { |
| 169 | + type: "string", |
| 170 | + label: "Folder Color RGB", |
| 171 | + description: |
| 172 | + `The color for a folder as an RGB hex string. If an unsupported color is specified, |
| 173 | + the closest color in the palette will be used instead.`, |
| 174 | + optional: true, |
| 175 | + }, |
| 176 | + mimeType: { |
| 177 | + type: "string", |
| 178 | + label: "Mime Type", |
| 179 | + description: `The MIME type of the file. Google Drive will attempt to automatically detect |
| 180 | + an appropriate value from uploaded content if no value is provided. The value cannot be |
| 181 | + changed unless a new revision is uploaded. If a file is created with a Google Doc MIME |
| 182 | + type, the uploaded content will be imported if possible. Google Workspace and Drive |
| 183 | + MIME Types: https://developers.google.com/drive/api/v3/mime-types`, |
| 184 | + optional: true, |
| 185 | + async options({ page = 0 }) { |
| 186 | + const allTypes = googleMimeTypes.concat(mimeTypes); |
| 187 | + const start = (page - 1) * 10; |
| 188 | + const end = start + 10; |
| 189 | + return allTypes.slice(start, end); |
| 190 | + }, |
| 191 | + }, |
| 192 | + name: { |
| 193 | + type: "string", |
| 194 | + label: "Name", |
| 195 | + description: "Name of the file", |
| 196 | + optional: true, |
| 197 | + }, |
| 198 | + originalFilename: { |
| 199 | + type: "string", |
| 200 | + label: "Original Filename", |
| 201 | + description: |
| 202 | + "The original filename of the uploaded content if available, or else the original value of the name field. This is only available for files with binary content in Google Drive.", |
| 203 | + optional: true, |
| 204 | + }, |
| 205 | + shortcutDetailsTargetId: { |
| 206 | + type: "string", |
| 207 | + label: "Shortcut Details Target ID", |
| 208 | + description: "The ID of the file that this shortcut points to.", |
| 209 | + optional: true, |
| 210 | + }, |
| 211 | + starred: { |
| 212 | + type: "boolean", |
| 213 | + label: "Starred", |
| 214 | + description: "Whether the user has starred the file.", |
| 215 | + optional: true, |
| 216 | + }, |
| 217 | + writersCanShare: { |
| 218 | + type: "boolean", |
| 219 | + label: "Writers Can Share", |
| 220 | + description: |
| 221 | + "Whether users with only writer permission can modify the file's permissions. Not populated for items in shared drives.", |
| 222 | + optional: true, |
| 223 | + }, |
| 224 | + }, |
| 225 | + async run() { |
| 226 | + const body = this.fileUrl |
| 227 | + ? await got.stream(this.fileUrl) |
| 228 | + : fs.createReadStream(this.filePath); |
| 229 | + return ( |
| 230 | + await this.googleDrive.createFile({ |
| 231 | + ignoreDefaultVisibility: this.ignoreDefaultVisibility, |
| 232 | + includePermissionsForView: this.includePermissionsForView, |
| 233 | + keepRevisionForever: this.keeprevisionForever, |
| 234 | + ocrLanguage: this.ocrLanguage, |
| 235 | + useContentAsIndexableText: this.useContentAsIndexableText, |
| 236 | + supportsAllDrives: this.supportsAllDrives, |
| 237 | + resource: { |
| 238 | + name: this.name, |
| 239 | + originalFilename: this.originalFilename, |
| 240 | + parents: [ |
| 241 | + this.parent, |
| 242 | + ], |
| 243 | + mimeType: this.mimeType, |
| 244 | + description: this.description, |
| 245 | + folderColorRgb: this.folderColorRgb, |
| 246 | + shortcutDetails: { |
| 247 | + targetId: this.shortcutDetailsTargetId, |
| 248 | + }, |
| 249 | + starred: this.starred, |
| 250 | + writersCanShare: this.writersCanShare, |
| 251 | + contentHints: { |
| 252 | + indexableText: this.contentHintsIndexableText, |
| 253 | + }, |
| 254 | + contentRestrictions: { |
| 255 | + readOnly: this.contentRestrictionsReadOnly, |
| 256 | + reason: this.contentRestrictionsReason, |
| 257 | + }, |
| 258 | + copyRequiresWriterPermission: this.copyRequiresWriterPermission, |
| 259 | + }, |
| 260 | + media: { |
| 261 | + mimeType: this.mimeType, |
| 262 | + uploadType: this.uploadType, |
| 263 | + body, |
| 264 | + }, |
| 265 | + fields: "*", |
| 266 | + }) |
| 267 | + ); |
| 268 | + }, |
| 269 | +}; |
0 commit comments