@@ -89,6 +89,14 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
89
89
}
90
90
return { } ;
91
91
} else if ( attachmentFiles . length !== 0 ) {
92
+ try {
93
+ for ( const fileUrl of attachmentFiles ) {
94
+ new URL ( fileUrl ) ;
95
+ }
96
+ } catch ( e ) {
97
+ jobs . set ( jobId , { status : 'failed' , error : 'One of the image URLs is not valid' } ) ;
98
+ return { ok : false , error : 'One of the image URLs is not valid' } ;
99
+ }
92
100
//create prompt for OpenAI
93
101
const compiledOutputFields = this . compileOutputFieldsTemplates ( record ) ;
94
102
const prompt = `Analyze the following image(s) and return a single JSON in format like: {'param1': 'value1', 'param2': 'value2'}.
@@ -118,7 +126,13 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
118
126
}
119
127
120
128
//parse response and update record
121
- const resData = JSON . parse ( textOutput ) ;
129
+ let resData ;
130
+ try {
131
+ resData = JSON . parse ( textOutput ) ;
132
+ } catch ( e ) {
133
+ jobs . set ( jobId , { status : 'failed' , error : 'AI response is not valid JSON. Probably attached invalid image URL' } ) ;
134
+ return { ok : false , error : 'AI response is not valid JSON. Probably attached invalid image URL' } ;
135
+ }
122
136
const result = resData ;
123
137
jobs . set ( jobId , { status : 'completed' , result } ) ;
124
138
return { ok : true } ;
@@ -180,6 +194,14 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
180
194
attachmentFiles = [ ] ;
181
195
} else {
182
196
attachmentFiles = await this . options . attachFiles ( { record } ) ;
197
+ try {
198
+ for ( const fileUrl of attachmentFiles ) {
199
+ new URL ( fileUrl ) ;
200
+ }
201
+ } catch ( e ) {
202
+ jobs . set ( jobId , { status : 'failed' , error : 'One of the image URLs is not valid' } ) ;
203
+ return { ok : false , error : 'One of the image URLs is not valid' } ;
204
+ }
183
205
}
184
206
const fieldTasks = Object . keys ( this . options ?. generateImages || { } ) . map ( async ( key ) => {
185
207
const prompt = this . compileGenerationFieldTemplates ( record ) [ key ] ;
0 commit comments