|
| 1 | +import autodesk from "../../autodesk.app.mjs"; |
| 2 | +import { axios } from "@pipedream/platform"; |
| 3 | +import fs from "fs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + key: "autodesk-upload-file", |
| 7 | + name: "Upload File", |
| 8 | + description: "Uploads a new file to a specified folder in Autodesk. [See the documentation](https://aps.autodesk.com/en/docs/data/v2/tutorials/upload-file/).", |
| 9 | + version: "0.0.1", |
| 10 | + type: "action", |
| 11 | + props: { |
| 12 | + autodesk, |
| 13 | + hubId: { |
| 14 | + propDefinition: [ |
| 15 | + autodesk, |
| 16 | + "hubId", |
| 17 | + ], |
| 18 | + }, |
| 19 | + projectId: { |
| 20 | + propDefinition: [ |
| 21 | + autodesk, |
| 22 | + "projectId", |
| 23 | + (c) => ({ |
| 24 | + hubId: c.hubId, |
| 25 | + }), |
| 26 | + ], |
| 27 | + }, |
| 28 | + folderId: { |
| 29 | + propDefinition: [ |
| 30 | + autodesk, |
| 31 | + "folderId", |
| 32 | + (c) => ({ |
| 33 | + hubId: c.hubId, |
| 34 | + projectId: c.projectId, |
| 35 | + }), |
| 36 | + ], |
| 37 | + }, |
| 38 | + fileName: { |
| 39 | + type: "string", |
| 40 | + label: "File Name", |
| 41 | + description: "The name of the file to upload", |
| 42 | + }, |
| 43 | + filePath: { |
| 44 | + type: "string", |
| 45 | + label: "File Path", |
| 46 | + description: "The path to a file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)", |
| 47 | + }, |
| 48 | + type: { |
| 49 | + type: "string", |
| 50 | + label: "Extension Type", |
| 51 | + description: "The type of file extension. For BIM 360 Docs files, use `items:autodesk.bim360:File`. For all other services, use `items:autodesk.core:File`.", |
| 52 | + options: [ |
| 53 | + { |
| 54 | + label: "BIM 360 Docs files", |
| 55 | + value: "items:autodesk.core:File", |
| 56 | + }, |
| 57 | + { |
| 58 | + label: "Other files", |
| 59 | + value: "items:autodesk.bim360:File", |
| 60 | + }, |
| 61 | + ], |
| 62 | + default: "items:autodesk.core:File", |
| 63 | + optional: true, |
| 64 | + }, |
| 65 | + }, |
| 66 | + async run({ $ }) { |
| 67 | + // Create storage location |
| 68 | + const { data } = await this.autodesk.createStorageLocation({ |
| 69 | + $, |
| 70 | + projectId: this.projectId, |
| 71 | + data: { |
| 72 | + jsonapi: { |
| 73 | + version: "1.0", |
| 74 | + }, |
| 75 | + data: { |
| 76 | + type: "objects", |
| 77 | + attributes: { |
| 78 | + name: this.fileName, |
| 79 | + }, |
| 80 | + relationships: { |
| 81 | + target: { |
| 82 | + data: { |
| 83 | + type: "folders", |
| 84 | + id: this.folderId, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }); |
| 91 | + |
| 92 | + const objectId = data.id; |
| 93 | + const [ |
| 94 | + bucketKey, |
| 95 | + objectKey, |
| 96 | + ] = objectId.split("os.object:")[1]?.split("/") || []; |
| 97 | + |
| 98 | + // Generate signed URL |
| 99 | + const { |
| 100 | + urls, uploadKey, |
| 101 | + } = await this.autodesk.generateSignedUrl({ |
| 102 | + $, |
| 103 | + bucketKey, |
| 104 | + objectKey, |
| 105 | + }); |
| 106 | + |
| 107 | + const signedUrl = urls[0]; |
| 108 | + |
| 109 | + // Upload to signed URL |
| 110 | + const filePath = this.filePath.includes("tmp/") |
| 111 | + ? this.filePath |
| 112 | + : `/tmp/${this.filePath}`; |
| 113 | + const fileStream = fs.createReadStream(filePath); |
| 114 | + const fileSize = fs.statSync(filePath).size; |
| 115 | + |
| 116 | + await axios($, { |
| 117 | + url: signedUrl, |
| 118 | + data: fileStream, |
| 119 | + method: "PUT", |
| 120 | + headers: { |
| 121 | + "Content-Type": "application/octet-stream", |
| 122 | + "Content-Length": fileSize, |
| 123 | + }, |
| 124 | + maxContentLength: Infinity, |
| 125 | + maxBodyLength: Infinity, |
| 126 | + }); |
| 127 | + |
| 128 | + // Complete the upload |
| 129 | + await this.autodesk.completeUpload({ |
| 130 | + $, |
| 131 | + bucketKey, |
| 132 | + objectKey, |
| 133 | + data: { |
| 134 | + uploadKey, |
| 135 | + }, |
| 136 | + }); |
| 137 | + |
| 138 | + // Create version 1.0 of uploaded file |
| 139 | + const response = await this.autodesk.createFirstVersionOfFile({ |
| 140 | + $, |
| 141 | + projectId: this.projectId, |
| 142 | + data: { |
| 143 | + jsonapi: { |
| 144 | + version: "1.0", |
| 145 | + }, |
| 146 | + data: { |
| 147 | + type: "items", |
| 148 | + attributes: { |
| 149 | + displayName: this.fileName, |
| 150 | + extension: { |
| 151 | + type: this.type, |
| 152 | + version: "1.0", |
| 153 | + }, |
| 154 | + }, |
| 155 | + relationships: { |
| 156 | + tip: { |
| 157 | + data: { |
| 158 | + type: "versions", |
| 159 | + id: "1", |
| 160 | + }, |
| 161 | + }, |
| 162 | + parent: { |
| 163 | + data: { |
| 164 | + type: "folders", |
| 165 | + id: this.folderId, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + included: [ |
| 171 | + { |
| 172 | + type: "versions", |
| 173 | + id: "1", |
| 174 | + attributes: { |
| 175 | + name: this.fileName, |
| 176 | + extension: { |
| 177 | + type: this.type.replace("items", "versions"), |
| 178 | + version: "1.0", |
| 179 | + }, |
| 180 | + }, |
| 181 | + relationships: { |
| 182 | + storage: { |
| 183 | + data: { |
| 184 | + type: "objects", |
| 185 | + id: objectId, |
| 186 | + }, |
| 187 | + }, |
| 188 | + }, |
| 189 | + }, |
| 190 | + ], |
| 191 | + }, |
| 192 | + }); |
| 193 | + |
| 194 | + $.export("$summary", `Successfully uploaded file ${this.fileName}`); |
| 195 | + return response; |
| 196 | + }, |
| 197 | +}; |
0 commit comments