-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathattachment-processor.ts
More file actions
99 lines (88 loc) · 2.6 KB
/
Copy pathattachment-processor.ts
File metadata and controls
99 lines (88 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { createLogger } from '@sim/logger'
import { uploadFileFromRawData } from '@/lib/uploads/contexts/execution'
import type { UserFile } from '@/executor/types'
const logger = createLogger('WebhookAttachmentProcessor')
export interface WebhookAttachment {
name: string
data: Buffer
contentType?: string
mimeType?: string
size: number
}
/**
* Processes webhook/trigger attachments and converts them to UserFile objects.
* This enables triggers to include file attachments that get automatically stored
* in the execution filesystem and made available as UserFile objects for workflow use.
*/
export class WebhookAttachmentProcessor {
/**
* Process attachments and upload them to execution storage
*/
static async processAttachments(
attachments: WebhookAttachment[],
executionContext: {
workspaceId: string
workflowId: string
executionId: string
requestId: string
userId?: string
}
): Promise<UserFile[]> {
if (!attachments || attachments.length === 0) {
return []
}
logger.info(
`[${executionContext.requestId}] Processing ${attachments.length} attachments for execution ${executionContext.executionId}`
)
const processedFiles: UserFile[] = []
for (const attachment of attachments) {
try {
const userFile = await WebhookAttachmentProcessor.processAttachment(
attachment,
executionContext
)
processedFiles.push(userFile)
} catch (error) {
logger.error(
`[${executionContext.requestId}] Error processing attachment '${attachment.name}':`,
error
)
// Continue with other attachments rather than failing the entire request
}
}
logger.info(
`[${executionContext.requestId}] Successfully processed ${processedFiles.length}/${attachments.length} attachments`
)
return processedFiles
}
/**
* Process a single attachment and upload to execution storage
*/
private static async processAttachment(
attachment: WebhookAttachment,
executionContext: {
workspaceId: string
workflowId: string
executionId: string
requestId: string
userId?: string
}
): Promise<UserFile> {
const userFile = await uploadFileFromRawData(
{
name: attachment.name,
data: attachment.data,
mimeType: attachment.contentType || attachment.mimeType,
},
executionContext,
executionContext.userId
)
if (userFile.base64) {
return userFile
}
return {
...userFile,
base64: attachment.data.toString('base64'),
}
}
}