Skip to content

Commit 259fe7a

Browse files
committed
fix: fix S3 deletation tags
1 parent 53c42b2 commit 259fe7a

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

custom/imageGenerationCarousel.vue

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,9 @@ onMounted(async () => {
212212
images.value.push((props.images || []));
213213
const temp = await getGenerationPrompt() || '';
214214
prompt.value = temp[props.fieldName];
215-
console.log('Initial images:', images.value, "with id:", props.recordId, "and prompt:", prompt.value);
216215
await nextTick();
217216
218217
const currentIndex = caurosel.value?.getActiveItem()?.position || 0;
219-
console.log("Setting up carousel with currentIndex:", currentIndex);
220218
caurosel.value = new Carousel(
221219
document.getElementById('gallery'),
222220
images.value.map((img, index) => {
@@ -273,20 +271,6 @@ onMounted(async () => {
273271
const recordId = props.record[props.meta.recorPkFieldName];
274272
if (!recordId) return;
275273
276-
// try {
277-
// const resp = await callAdminForthApi({
278-
// path: `/plugin/${props.meta.pluginInstanceId}/get_attachment_files`,
279-
// method: 'POST',
280-
// body: { recordId },
281-
// });
282-
283-
// if (resp?.files?.length) {
284-
// attachmentFiles.value = resp.files;
285-
// console.log('attachmentFiles', attachmentFiles.value);
286-
// }
287-
// } catch (err) {
288-
// console.error('Failed to fetch attachment files', err);
289-
// }
290274
});
291275
292276
async function slide(direction: number) {
@@ -345,7 +329,6 @@ async function getGenerationPrompt() {
345329
recordId: props.recordId,
346330
},
347331
});
348-
console.log('Received generation prompts from server:', resp);
349332
return resp?.generationOptions || null;
350333
}
351334

custom/visionAction.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const openDialog = async () => {
105105
}
106106
isLoading.value = true;
107107
await Promise.all([
108-
//analyzeFields(),
108+
analyzeFields(),
109109
generateImages()
110110
]);
111111
isLoading.value = false;
@@ -406,7 +406,7 @@ async function generateImages() {
406406
407407
408408
async function uploadImage(imgBlob, id, fieldName) {
409-
const file = new File([imgBlob], `generated_${fieldName}_${id}.${imgBlob.type}`, { type: imgBlob.type });
409+
const file = new File([imgBlob], `generated_${fieldName}_${id}.${imgBlob.type.split('/').pop()}`, { type: imgBlob.type });
410410
const { name, size, type } = file;
411411
412412
const extension = name.split('.').pop();

index.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { json } from "stream/consumers";
55
import Handlebars, { compile } from 'handlebars';
66
import { RateLimiter } from "adminforth";
77

8-
const ADMINFORTH_NOT_YET_USED_TAG = 'adminforth-candidate-for-cleanup';
8+
let outputImageFields = [];
99

1010

1111
export default class BulkAiFlowPlugin extends AdminForthPlugin {
@@ -53,7 +53,6 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
5353
private checkRateLimit(fieldNameRateLimit: string | undefined, headers: Record<string, string | string[] | undefined>): { error?: string } | void {
5454
if (fieldNameRateLimit) {
5555
// rate limit
56-
console.log('Checking rate limit', fieldNameRateLimit);
5756
const { error } = RateLimiter.checkRateLimit(
5857
this.pluginInstanceId,
5958
fieldNameRateLimit,
@@ -86,7 +85,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
8685
}
8786
}
8887

89-
let outputImageFields = [];
88+
outputImageFields = [];
9089
if (this.options.generateImages) {
9190
for (const [key, value] of Object.entries(this.options.generateImages)) {
9291
outputImageFields.push(key);
@@ -123,8 +122,6 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
123122
...(this.options.generateImages || {})
124123
};
125124

126-
// const compiledGenerationOptions = this.compileGenerationFieldTemplates(record);
127-
// console.log('Compiled generation options prompt:', compiledGenerationOptions);
128125

129126
const primaryKeyColumn = this.resourceConfig.columns.find((col) => col.primaryKey);
130127

@@ -250,15 +247,36 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
250247
handler: async ( body ) => {
251248
const selectedIds = body.body.selectedIds || [];
252249
const fieldsToUpdate = body.body.fields || {};
253-
console.log("Updating fields for IDs:", selectedIds, fieldsToUpdate);
254-
const updates = selectedIds.map((ID, idx) =>
255-
this.adminforth
256-
.resource(this.resourceConfig.resourceId)
257-
.update(ID, fieldsToUpdate[idx])
258-
);
259-
250+
const primaryKeyColumn = this.resourceConfig.columns.find((col) => col.primaryKey);
251+
const updates = selectedIds.map(async (ID, idx) => {
252+
const oldRecord = await this.adminforth.resource(this.resourceConfig.resourceId).get( [Filters.EQ(primaryKeyColumn.name, ID)] );
253+
for (const [key, value] of Object.entries(outputImageFields)) {
254+
const columnPlugin = this.adminforth.activatedPlugins.find(p =>
255+
p.resourceConfig!.resourceId === this.resourceConfig.resourceId &&
256+
p.pluginOptions.pathColumnName === value
257+
);
258+
if (columnPlugin) {
259+
if(columnPlugin.pluginOptions.storageAdapter.objectCanBeAccesedPublicly()) {
260+
if (oldRecord[value]) {
261+
// put tag to delete old file
262+
try {
263+
await columnPlugin.pluginOptions.storageAdapter.markKeyForDeletation(oldRecord[value]);
264+
} catch (e) {
265+
// file might be e.g. already deleted, so we catch error
266+
console.error(`Error setting tag to true for object ${oldRecord[value]}. File will not be auto-cleaned up`, e);
267+
}
268+
}
269+
if (fieldsToUpdate[idx][key] !== null) {
270+
// remove tag from new file
271+
// in this case we let it crash if it fails: this is a new file which just was uploaded.
272+
await columnPlugin.pluginOptions.storageAdapter.markKeyForNotDeletation(fieldsToUpdate[idx][value]);
273+
}
274+
}
275+
}
276+
}
277+
return this.adminforth.resource(this.resourceConfig.resourceId).update(ID, fieldsToUpdate[idx])
278+
});
260279
await Promise.all(updates);
261-
262280
return { ok: true };
263281
}
264282
});
@@ -323,7 +341,8 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
323341
let images;
324342
if (STUB_MODE) {
325343
await new Promise((resolve) => setTimeout(resolve, 2000));
326-
images = `https://picsum.photos/200/300?random=${Math.floor(Math.random() * 1000)}`;
344+
//images = `https://picsum.photos/200/300?random=${Math.floor(Math.random() * 1000)}`;
345+
images = "https://cdn.rafled.com/anime-icons/images/6d5f85159da70f7cb29c1121248031fb4a649588a9cd71ff1784653a1f64be31.jpg";
327346
} else {
328347
const resp = await this.options.generateImages[key].adapter.generate(
329348
{

0 commit comments

Comments
 (0)