33use crate :: { Config , traps_utils} ;
44use crate :: plugins:: image_store_plugin:: { ImageStorePlugin , StoreAction , StoreParms } ;
55use crate :: events_generated:: gen_events:: ImageScoredEvent ;
6- use crate :: { config:: errors:: Errors } ;
7- use anyhow :: { Result , anyhow } ;
6+ use crate :: { events , config:: errors:: Errors } ;
7+ use event_engine :: { plugins :: Plugin } ;
88
9+ use anyhow:: { Result , anyhow} ;
10+ use std:: fs;
11+ use serde_json;
912
1013use log:: { info, error} ;
1114
1215// The search string prefix for this plugin.
1316const PREFIX : & str = "image_store_" ;
1417
18+ // The score file suffix.
19+ const SCORE_SUFFIX : & str = "score" ;
20+
1521// ***************************************************************************
1622// PUBLIC FUNCTIONS
1723// ***************************************************************************
@@ -96,10 +102,10 @@ pub fn image_store_file_action(plugin: &ImageStorePlugin, event: &ImageScoredEve
96102
97103 // Perform the action.
98104 match store_action {
99- StoreAction :: Delete => action_delete ( event) ,
100- StoreAction :: Noop => action_noop ( event ) ,
101- StoreAction :: ReduceSave => action_reduce_save ( event) ,
102- StoreAction :: Save => action_save ( event) ,
105+ StoreAction :: Delete => action_delete ( plugin , event) ,
106+ StoreAction :: Noop => { } ,
107+ StoreAction :: ReduceSave => action_reduce_save ( plugin , event) ,
108+ StoreAction :: Save => action_save ( plugin , event) ,
103109 }
104110
105111 // Return the action taken.
@@ -150,39 +156,149 @@ fn get_action_for_score(store_parms_ref: &StoreParms, score: f32) -> StoreAction
150156}
151157
152158// ---------------------------------------------------------------------------
153- // create_image_filepath :
159+ // make_image_filepath :
154160// ---------------------------------------------------------------------------
155161/** Create absolute file path for the image. */
156- fn create_image_filepath ( plugin : & ImageStorePlugin , uuid_str : & str , suffix : & str ) -> String {
157- return traps_utils:: create_image_filepath ( & plugin. get_runctx ( ) . abs_image_dir ,
158- & plugin. get_runctx ( ) . parms . config . image_file_prefix ,
159- uuid_str,
160- suffix) ;
162+ fn make_image_filepath ( plugin : & ImageStorePlugin , event : & ImageScoredEvent ) -> Option < String > {
163+ // Get the uuid string for use in the file name.
164+ let uuid_str = match event. image_uuid ( ) {
165+ Some ( s) => s,
166+ None => {
167+ // Log the error and just return.
168+ let msg = format ! ( "{}" , Errors :: PluginEventAccessUuidError (
169+ plugin. get_name( ) , "NewImageEvent" . to_string( ) ) ) ;
170+ error ! ( "{}" , msg) ;
171+ return None ;
172+ }
173+ } ;
174+
175+ // Standardize image type suffixes to lowercase.
176+ let suffix = match event. image_format ( ) {
177+ Some ( s) => s. to_string ( ) . to_lowercase ( ) ,
178+ None => {
179+ // Log the error and just return.
180+ let msg = format ! ( "{}" , Errors :: ActionImageFormatTypeError (
181+ plugin. get_name( ) , "NewImageEvent" . to_string( ) ) ) ;
182+ error ! ( "{}" , msg) ;
183+ return None ;
184+ }
185+ } ;
186+
187+ // Get the path.
188+ let path = traps_utils:: create_image_filepath ( & plugin. get_runctx ( ) . abs_image_dir ,
189+ & plugin. get_runctx ( ) . parms . config . image_file_prefix ,
190+ uuid_str,
191+ suffix. as_str ( ) ) ;
192+
193+ Option :: Some ( path)
161194}
162195
163196// ---------------------------------------------------------------------------
164- // action_delete :
197+ // make_score_filepath :
165198// ---------------------------------------------------------------------------
166- fn action_delete ( event : & ImageScoredEvent ) {
167- let uuid = event. image_uuid ( ) ;
199+ /** Create absolute file path for the image. */
200+ fn make_score_filepath ( plugin : & ImageStorePlugin , event : & ImageScoredEvent ) -> Option < String > {
201+ // Get the uuid string for use in the file name.
202+ let uuid_str = match event. image_uuid ( ) {
203+ Some ( s) => s,
204+ None => {
205+ // Log the error and just return.
206+ let msg = format ! ( "{}" , Errors :: PluginEventAccessUuidError (
207+ plugin. get_name( ) , "NewImageEvent" . to_string( ) ) ) ;
208+ error ! ( "{}" , msg) ;
209+ return None ;
210+ }
211+ } ;
212+
213+ // Get the path.
214+ let path = traps_utils:: create_image_filepath ( & plugin. get_runctx ( ) . abs_image_dir ,
215+ & plugin. get_runctx ( ) . parms . config . image_file_prefix ,
216+ uuid_str,
217+ SCORE_SUFFIX ) ;
218+
219+ Option :: Some ( path)
168220}
169221
170222// ---------------------------------------------------------------------------
171- // action_noop :
223+ // action_delete :
172224// ---------------------------------------------------------------------------
173- fn action_noop ( event : & ImageScoredEvent ) {
225+ /** Delete the image file and don't save the scores.
226+ */
227+ fn action_delete ( plugin : & ImageStorePlugin , event : & ImageScoredEvent ) {
228+ // Create absolute file path for the image. Errors have alread been logged.
229+ let filepath = match make_image_filepath ( plugin, event) {
230+ Some ( p) => p,
231+ None => return ,
232+ } ;
174233
234+ // Delete the file.
235+ match fs:: remove_file ( filepath. clone ( ) ) {
236+ Ok ( _) => { } ,
237+ Err ( e) => {
238+ // Log error.
239+ let msg = format ! ( "{}" , Errors :: FileDeleteError (
240+ filepath, e. to_string( ) ) ) ;
241+ error ! ( "{}" , msg) ;
242+ }
243+ } ;
175244}
176245
177246// ---------------------------------------------------------------------------
178247// action_reduce_save:
179248// ---------------------------------------------------------------------------
180- fn action_reduce_save ( event : & ImageScoredEvent ) {
249+ /** Reduce the image resolution and then save it and it's scores.
250+ */
251+ fn action_reduce_save ( plugin : & ImageStorePlugin , event : & ImageScoredEvent ) {
252+ // TODO: reduce image size
181253
254+ action_save ( plugin, event)
182255}
256+
183257// ---------------------------------------------------------------------------
184258// action_save:
185259// ---------------------------------------------------------------------------
186- fn action_save ( event : & ImageScoredEvent ) {
260+ /** Leave the image file as-is and save its scores. On error, just log and return.
261+ */
262+ fn action_save ( plugin : & ImageStorePlugin , event : & ImageScoredEvent ) {
263+ // Extract the image uuid from the new image event.
264+ let image_scored_event = match events:: ImageScoredEvent :: new_from_gen ( * event) {
265+ Ok ( ev) => ev,
266+ Err ( e) => {
267+ let msg = format ! ( "{}" , Errors :: PluginEventDeserializationError (
268+ plugin. get_name( ) , "ImageScoredEvent" . to_string( ) ) ) ;
269+ error ! ( "{}: {}" , msg, e. to_string( ) ) ;
270+ return
271+ }
272+ } ;
187273
274+ // Convert the event scores to json.
275+ let json_str = match serde_json:: to_string ( & image_scored_event) {
276+ Ok ( s) => s,
277+ Err ( e) => {
278+ let msg = format ! ( "{}" , Errors :: EventToJsonError (
279+ plugin. get_name( ) , "ImageScoredEvent" . to_string( ) , e. to_string( ) ) ) ;
280+ error ! ( "{}" , msg) ;
281+ return ;
282+ }
283+ } ;
284+
285+ // Construct the score output path name.
286+ let filepath = match make_score_filepath ( plugin, event) {
287+ Some ( fp) => fp,
288+ None => {
289+ // Error already logged.
290+ return ;
291+ }
292+ } ;
293+
294+ // Write the json to the score output file.
295+ match traps_utils:: create_or_replace_file ( & filepath, json_str. as_bytes ( ) ) {
296+ Ok ( _) => ( ) ,
297+ Err ( e) => {
298+ let msg = format ! ( "{}" , Errors :: ActionWriteFileError ( plugin. get_name( ) ,
299+ "action_save" . to_string( ) , filepath, e. to_string( ) ) ) ;
300+ error ! ( "{}" , msg) ;
301+ return ;
302+ }
303+ } ;
188304}
0 commit comments