(args: {
workspaceId: string
userId: string
target: WorkspaceFileWriteTarget
buffer: Buffer
inferredMimeType: string
})
| 285 | } |
| 286 | |
| 287 | export async function writeWorkspaceFileByPath(args: { |
| 288 | workspaceId: string |
| 289 | userId: string |
| 290 | target: WorkspaceFileWriteTarget |
| 291 | buffer: Buffer |
| 292 | inferredMimeType: string |
| 293 | }): Promise<WorkspaceFileWriteResult> { |
| 294 | const contentType = args.target.mimeType || args.inferredMimeType |
| 295 | const alias = await resolveWorkflowAliasForWorkspace({ |
| 296 | workspaceId: args.workspaceId, |
| 297 | path: args.target.path, |
| 298 | }) |
| 299 | if (!alias && isPlanAliasPath(args.target.path)) { |
| 300 | throw new Error(`Unsupported plan alias path or missing workflow: ${args.target.path}`) |
| 301 | } |
| 302 | if (alias) { |
| 303 | const resolved = await resolveWorkflowAliasFileTarget({ |
| 304 | workspaceId: args.workspaceId, |
| 305 | userId: args.userId, |
| 306 | alias, |
| 307 | }) |
| 308 | |
| 309 | if (args.target.mode === 'overwrite') { |
| 310 | if (!resolved.existingFile) { |
| 311 | throw new Error(`File not found for overwrite: ${alias.aliasPath}`) |
| 312 | } |
| 313 | const updated = await updateWorkspaceFileContent( |
| 314 | args.workspaceId, |
| 315 | resolved.existingFile.id, |
| 316 | args.userId, |
| 317 | args.buffer, |
| 318 | contentType || resolved.existingFile.type |
| 319 | ) |
| 320 | return { |
| 321 | id: updated.id, |
| 322 | name: updated.name, |
| 323 | size: updated.size, |
| 324 | contentType: updated.type, |
| 325 | downloadUrl: updated.url, |
| 326 | vfsPath: alias.aliasPath, |
| 327 | backingVfsPath: vfsPathForRecord(updated), |
| 328 | mode: 'overwrite', |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if (resolved.existingFile) { |
| 333 | throw new Error( |
| 334 | `File already exists at ${alias.aliasPath}. Use mode "overwrite" to update it.` |
| 335 | ) |
| 336 | } |
| 337 | const uploaded = await uploadWorkspaceFile( |
| 338 | args.workspaceId, |
| 339 | args.userId, |
| 340 | args.buffer, |
| 341 | resolved.fileName, |
| 342 | contentType, |
| 343 | { folderId: resolved.folderId, exactName: true } |
| 344 | ).catch((error: unknown) => { |
no test coverage detected