@@ -9,8 +9,9 @@ use event_engine::{plugins::Plugin};
99use anyhow:: { Result , anyhow} ;
1010use std:: fs;
1111use serde_json;
12+ use glob:: glob;
1213
13- use log:: { info, error} ;
14+ use log:: { info, error, warn , debug } ;
1415
1516// The search string prefix for this plugin.
1617const PREFIX : & str = "image_store_" ;
@@ -170,6 +171,7 @@ fn get_action_for_score(store_parms_ref: &StoreParms, score: f32) -> StoreAction
170171 * the empty string. Both of these values are part of the application configuration.
171172 * The image uuid and format are returned in the NewImageEvent.
172173 */
174+ #[ allow( dead_code) ]
173175fn make_image_filepath ( plugin : & ImageStorePlugin , event : & ImageScoredEvent ) -> Option < String > {
174176 // Get the uuid string for use in the file name.
175177 let uuid_str = match event. image_uuid ( ) {
@@ -240,28 +242,71 @@ fn make_score_filepath(plugin: &ImageStorePlugin, event: &ImageScoredEvent) -> O
240242// ---------------------------------------------------------------------------
241243// action_delete:
242244// ---------------------------------------------------------------------------
243- /** Delete the image file and don't save the scores.
245+ /** Delete all image related files and don't save the scores. Image related
246+ * files are all file that match this format:
247+ *
248+ * <image_directory_path>/<image_file_prefix><image_uuid>*
249+ *
244250 */
245251fn action_delete ( plugin : & ImageStorePlugin , event : & ImageScoredEvent ) -> StoreAction {
246- // Create absolute file path for the image. Errors have alread been logged.
247- let filepath = match make_image_filepath ( plugin, event) {
248- Some ( p) => p,
249- None => return StoreAction :: ErrorOut ,
252+ // Get the uuid string for use in the file name.
253+ let uuid_str = match event. image_uuid ( ) {
254+ Some ( s) => s,
255+ None => {
256+ // Log the error and just return.
257+ let msg = format ! ( "{}" , Errors :: PluginEventAccessUuidError (
258+ plugin. get_name( ) , "NewImageEvent" . to_string( ) ) ) ;
259+ error ! ( "{}" , msg) ;
260+ return StoreAction :: ErrorOut
261+ }
250262 } ;
251263
252- // Delete the file.
253- match fs:: remove_file ( filepath. clone ( ) ) {
254- Ok ( _) => { } ,
264+ // Get the path iterator that matches the wildcard path.
265+ let wildcard_path =
266+ traps_utils:: create_image_wildcard_path ( & plugin. get_runctx ( ) . abs_image_dir ,
267+ & plugin. get_runctx ( ) . parms . config . image_file_prefix ,
268+ uuid_str) ;
269+
270+ // Get path iterator and process its entries.
271+ match glob ( & wildcard_path) {
272+ // Get an iterator to paths that match the filter.
255273 Err ( e) => {
256274 // Log error.
257275 let msg = format ! ( "{}" , Errors :: FileDeleteError (
258- filepath , e. to_string( ) ) ) ;
276+ wildcard_path . clone ( ) , e. to_string( ) ) ) ;
259277 error ! ( "{}" , msg) ;
260278 return StoreAction :: ErrorOut
279+ } ,
280+ Ok ( path_iter) => {
281+ // Process each matching filepath.
282+ for entry in path_iter {
283+ match entry {
284+ // Record the error and continue.
285+ Err ( e) => {
286+ let msg = format ! ( "{}" , Errors :: FileDeleteError (
287+ wildcard_path. clone( ) , e. to_string( ) ) ) ;
288+ warn ! ( "{}" , msg) ;
289+ } ,
290+ Ok ( path) => {
291+ let filepath = path. as_os_str ( ) . to_str ( ) . unwrap ( ) ;
292+ match fs:: remove_file ( filepath) {
293+ Ok ( _) => {
294+ debug ! ( "{}" , format!( "{}" , Errors :: FileDeleted ( filepath. to_string( ) ) ) ) ;
295+ } ,
296+ Err ( e) => {
297+ // Log error.
298+ let msg = format ! ( "{}" , Errors :: FileDeleteError (
299+ filepath. to_string( ) , e. to_string( ) ) ) ;
300+ warn ! ( "{}" , msg) ;
301+ }
302+ }
303+ }
304+ }
305+ }
261306 }
262- } ;
307+ }
263308
264- // Success.
309+ // Success though warnings may have been logged .
265310 StoreAction :: Delete
266311}
267312
0 commit comments