@@ -5,7 +5,7 @@ import { json } from "stream/consumers";
55import Handlebars , { compile } from 'handlebars' ;
66import { RateLimiter } from "adminforth" ;
77
8- const ADMINFORTH_NOT_YET_USED_TAG = 'adminforth-candidate-for-cleanup' ;
8+ let outputImageFields = [ ] ;
99
1010
1111export 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