Skip to content

Commit 9cbe2f3

Browse files
committed
feat: add isAllowedToSave option and improve error handling for image generation and saving
1 parent f75c154 commit 9cbe2f3

File tree

4 files changed

+54
-37
lines changed

4 files changed

+54
-37
lines changed

custom/imageGenerationCarousel.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ async function getGenerationPrompt() {
339339
if(!resp) {
340340
emit('error', {
341341
isError: true,
342-
errorMessage: "Something went wrong. Check your internet connection and try again."
342+
errorMessage: "Error getting generation prompts."
343343
});
344344
}
345345
return resp?.generationOptions || null;
@@ -400,7 +400,7 @@ async function generateImages() {
400400
});
401401
emit('error', {
402402
isError: true,
403-
errorMessage: error
403+
errorMessage: "Error re-generating images"
404404
});
405405
}
406406
return;

custom/visionAction.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,18 @@ async function saveData() {
429429
},
430430
});
431431
432-
if(res.ok) {
432+
if(res.ok === true) {
433433
confirmDialog.value.close();
434434
props.updateList();
435435
props.clearCheckboxes();
436+
} else if (res.ok === false) {
437+
adminforth.alert({
438+
message: 'You are not allowed to save.',
439+
variant: 'danger',
440+
timeout: 'unlimited',
441+
});
442+
isError.value = true;
443+
errorMessage.value = `Failed to save data. You are not allowed to save.`;
436444
} else {
437445
console.error('Error saving data:', res);
438446
isError.value = true;

index.ts

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -339,45 +339,49 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
339339
method: 'POST',
340340
path: `/plugin/${this.pluginInstanceId}/update_fields`,
341341
handler: async ( body ) => {
342-
const selectedIds = body.body.selectedIds || [];
343-
const fieldsToUpdate = body.body.fields || {};
344-
const outputImageFields = [];
345-
if (this.options.generateImages) {
346-
for (const [key, value] of Object.entries(this.options.generateImages)) {
347-
outputImageFields.push(key);
342+
if(this.options.isAllowedToSave !== false) {
343+
const selectedIds = body.body.selectedIds || [];
344+
const fieldsToUpdate = body.body.fields || {};
345+
const outputImageFields = [];
346+
if (this.options.generateImages) {
347+
for (const [key, value] of Object.entries(this.options.generateImages)) {
348+
outputImageFields.push(key);
349+
}
348350
}
349-
}
350-
const primaryKeyColumn = this.resourceConfig.columns.find((col) => col.primaryKey);
351-
const updates = selectedIds.map(async (ID, idx) => {
352-
const oldRecord = await this.adminforth.resource(this.resourceConfig.resourceId).get( [Filters.EQ(primaryKeyColumn.name, ID)] );
353-
for (const [key, value] of Object.entries(outputImageFields)) {
354-
const columnPlugin = this.adminforth.activatedPlugins.find(p =>
355-
p.resourceConfig!.resourceId === this.resourceConfig.resourceId &&
356-
p.pluginOptions.pathColumnName === value
357-
);
358-
if (columnPlugin) {
359-
if(columnPlugin.pluginOptions.storageAdapter.objectCanBeAccesedPublicly()) {
360-
if (oldRecord[value]) {
361-
// put tag to delete old file
362-
try {
363-
await columnPlugin.pluginOptions.storageAdapter.markKeyForDeletation(oldRecord[value]);
364-
} catch (e) {
365-
// file might be e.g. already deleted, so we catch error
366-
console.error(`Error setting tag to true for object ${oldRecord[value]}. File will not be auto-cleaned up`, e);
351+
const primaryKeyColumn = this.resourceConfig.columns.find((col) => col.primaryKey);
352+
const updates = selectedIds.map(async (ID, idx) => {
353+
const oldRecord = await this.adminforth.resource(this.resourceConfig.resourceId).get( [Filters.EQ(primaryKeyColumn.name, ID)] );
354+
for (const [key, value] of Object.entries(outputImageFields)) {
355+
const columnPlugin = this.adminforth.activatedPlugins.find(p =>
356+
p.resourceConfig!.resourceId === this.resourceConfig.resourceId &&
357+
p.pluginOptions.pathColumnName === value
358+
);
359+
if (columnPlugin) {
360+
if(columnPlugin.pluginOptions.storageAdapter.objectCanBeAccesedPublicly()) {
361+
if (oldRecord[value]) {
362+
// put tag to delete old file
363+
try {
364+
await columnPlugin.pluginOptions.storageAdapter.markKeyForDeletation(oldRecord[value]);
365+
} catch (e) {
366+
// file might be e.g. already deleted, so we catch error
367+
console.error(`Error setting tag to true for object ${oldRecord[value]}. File will not be auto-cleaned up`, e);
368+
}
369+
}
370+
if (fieldsToUpdate[idx][key] !== null) {
371+
// remove tag from new file
372+
// in this case we let it crash if it fails: this is a new file which just was uploaded.
373+
await columnPlugin.pluginOptions.storageAdapter.markKeyForNotDeletation(fieldsToUpdate[idx][value]);
367374
}
368-
}
369-
if (fieldsToUpdate[idx][key] !== null) {
370-
// remove tag from new file
371-
// in this case we let it crash if it fails: this is a new file which just was uploaded.
372-
await columnPlugin.pluginOptions.storageAdapter.markKeyForNotDeletation(fieldsToUpdate[idx][value]);
373375
}
374376
}
375377
}
376-
}
377-
return this.adminforth.resource(this.resourceConfig.resourceId).update(ID, fieldsToUpdate[idx])
378-
});
379-
await Promise.all(updates);
380-
return { ok: true };
378+
return this.adminforth.resource(this.resourceConfig.resourceId).update(ID, fieldsToUpdate[idx])
379+
});
380+
await Promise.all(updates);
381+
return { ok: true };
382+
} else {
383+
return { ok: false }
384+
}
381385
}
382386
});
383387

types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ export interface PluginOptions {
4747
* As rateLimit on generateImages, but applied to bulk generations
4848
**/
4949
bulkGenerationRateLimit?: string,
50+
51+
/**
52+
* Whether the user is allowed to save the generated images
53+
*/
54+
isAllowedToSave?: boolean
5055
}

0 commit comments

Comments
 (0)