Skip to content

Commit 46fa0d9

Browse files
committed
image_store_plugin: delete all files related to an image
1 parent 2c45c12 commit 46fa0d9

File tree

5 files changed

+103
-13
lines changed

5 files changed

+103
-13
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ chrono = "0.4.22"
1111
#event-engine = { path = "../event-engine/event-engine/"}
1212
event-engine = "0.2.0"
1313
flatbuffers = "2.1.2"
14+
glob = "0.3.1"
1415
lazy_static = "1.4.0"
1516
log = "0.4.17"
1617
log4rs = "1.1.1"

src/config/errors.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,13 @@ pub enum Errors {
7878
#[error("Expected event type {}, but received event {} instead.", .0, .1)]
7979
EventUnexpectedError(String, String),
8080

81+
#[error("Deleted file {0}")]
82+
FileDeleted(String),
83+
8184
#[error("Failed to delete file {0}: {1}")]
8285
FileDeleteError(String, String),
8386

84-
#[error("File IO error: {}", .0)]
87+
#[error("File IO error: {}", .0)]
8588
FileIOError(String),
8689

8790
/// Input parameter logging.

src/plugins/actions/image_store_actions.rs

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use event_engine::{plugins::Plugin};
99
use anyhow::{Result, anyhow};
1010
use std::fs;
1111
use 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.
1617
const 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)]
173175
fn 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
*/
245251
fn 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

src/traps_utils.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,40 @@ pub fn create_image_filepath(abs_dir: &str, image_file_prefix: &Option<String>,
105105
filepath
106106
}
107107

108+
// ---------------------------------------------------------------------------
109+
// create_image_wildcard_path:
110+
// ---------------------------------------------------------------------------
111+
/** Create a file path using the glob wildcard characters that would that
112+
* capture all files related to an image. The result is string that can be
113+
* used to search for all files in a directory that are related to or stem from
114+
* an image file, including the image file itself. The format of the result
115+
* string is:
116+
*
117+
* <image_directory_path>/<image_file_prefix><image_uuid>*
118+
*
119+
* Using above constructed strings with the glob search crate allows all
120+
* imaged related files to be discovered.
121+
*
122+
*/
123+
#[allow(dead_code)]
124+
pub fn create_image_wildcard_path(abs_dir: &str, image_file_prefix: &Option<String>,
125+
uuid_str: &str) -> String {
126+
// Get absolute path of the image directory with a trailing slash.
127+
let slash = if abs_dir.ends_with('/') {""} else {"/"};
128+
let mut filepath = abs_dir.to_owned();
129+
filepath.push_str(slash);
130+
131+
// Prepend the file prefix if one is specified.
132+
match image_file_prefix {
133+
Some(s) => filepath.push_str(s),
134+
None => {}
135+
}
136+
137+
// Append the image uuid as the file name and the wildcare.
138+
filepath.push_str(uuid_str);
139+
filepath.push('*');
140+
filepath
141+
}
108142

109143
// ---------------------------------------------------------------------------
110144
// validate_image_dir:

0 commit comments

Comments
 (0)