2727from botocore .config import Config
2828from django .contrib .gis .geos import Point
2929from django .db import close_old_connections , connection
30+ import shutil
3031
3132
3233tf = TimezoneFinder ()
@@ -159,7 +160,7 @@ async def consume_queue(queue, name: str):
159160 try :
160161 await process_message (message )
161162 except Exception as e :
162- logger . error (f"Error processing message from { name } : { e } " )
163+ logging . exception (f"Error processing message from { name } : { e } " )
163164
164165async def consume_from (rb_url : str , name : str ):
165166 while not stop_event .is_set ():
@@ -380,7 +381,7 @@ def watermark(webcam: any, image_data: bytes, tz: str, timestamp: str) -> bytes:
380381 return buffer .read ()
381382
382383 except Exception as e :
383- logger . error (f"Error processing image from camera: { e } " )
384+ logging . exception (f"Error processing image from camera: { e } " )
384385 return None
385386
386387def blank_out_image (webcam : any , image_data : bytes , tz : str , timestamp : str ) -> bytes :
@@ -426,7 +427,7 @@ def blank_out_image(webcam: any, image_data: bytes, tz: str, timestamp: str) ->
426427 return buffer .read ()
427428
428429 except Exception as e :
429- logger . error (f"Error processing image from camera: { e } " )
430+ logging . exception (f"Error processing image from camera: { e } " )
430431 return None
431432
432433
@@ -441,7 +442,7 @@ def save_original_image_to_pvc(camera_id: str, image_bytes: bytes):
441442 with open (filepath , "wb" ) as f :
442443 f .write (image_bytes )
443444 except Exception as e :
444- logger . error (f"Error saving original image to PVC { filepath } : { e } " )
445+ logging . exception (f"Error saving original image to PVC { filepath } : { e } " )
445446 logger .info (f"Original image saved to PVC at { filepath } " )
446447
447448def save_watermarked_image_to_pvc (camera_id : str , image_bytes : bytes , timestamp : str , is_on : bool ):
@@ -460,23 +461,44 @@ def save_watermarked_image_to_pvc(camera_id: str, image_bytes: bytes, timestamp:
460461 else :
461462 logger .info (f"Blank out image saved to PVC at { filepath } " )
462463 except Exception as e :
463- logger . error (f"Error saving image to PVC { filepath } : { e } " )
464+ logging . exception (f"Error saving image to PVC { filepath } : { e } " )
464465
465- def save_watermarked_image_to_drivebc_pvc (camera_id : str , image_bytes : bytes , is_on : bool ):
466- os .makedirs (os .path .dirname (f'{ DRIVEBC_PVC_WATERMARKED_PATH } ' ), exist_ok = True )
466+ def check_backup_exists (camera_id : str ) -> bool :
467+ backup_dir = os .path .join (DRIVEBC_PVC_WATERMARKED_PATH , "backup" )
468+ backup_filepath = os .path .join (backup_dir , f"{ camera_id } .jpg" )
469+ return os .path .exists (backup_filepath )
470+
471+ def save_watermarked_image_to_drivebc_pvc (camera_id : str , image_bytes : bytes , is_on : bool ):
472+ save_dir = DRIVEBC_PVC_WATERMARKED_PATH
473+ backup_dir = os .path .join (save_dir , "backup" )
467474
468- save_dir = os .path .join (DRIVEBC_PVC_WATERMARKED_PATH )
469475 os .makedirs (save_dir , exist_ok = True )
476+ os .makedirs (backup_dir , exist_ok = True )
477+
470478 filename = f"{ camera_id } .jpg"
471479 filepath = os .path .join (save_dir , filename )
480+ backup_filepath = os .path .join (backup_dir , filename )
472481
473482 try :
483+ if os .path .exists (filepath ) and not is_on :
484+ if not check_backup_exists (camera_id ):
485+ logger .info (
486+ f"Backing up existing image for camera { camera_id } to { backup_filepath } "
487+ )
488+ shutil .move (filepath , backup_filepath )
489+ else :
490+ logger .debug (
491+ f"Backup image already exists for camera { camera_id } . Skipping backup."
492+ )
493+ # Save the new image
474494 with open (filepath , "wb" ) as f :
475495 f .write (image_bytes )
496+
476497 if is_on :
477- logger .info (f"Watermarked image saved to drivebc PVC at { filepath } " )
498+ logger .info (f"Watermarked image saved to DriveBC PVC at { filepath } " )
499+
478500 except Exception as e :
479- logger . error (f"Error saving image to drivebc PVC { filepath } : { e } " )
501+ logging . exception (f"Error saving image to DriveBC PVC { filepath } : { e } " )
480502
481503def delete_watermarked_image_from_pvc (camera_id : str ):
482504 save_dir = os .path .join (PVC_WATERMARKED_PATH , camera_id )
@@ -491,7 +513,7 @@ def delete_watermarked_image_from_pvc(camera_id: str):
491513 if os .path .isfile (filepath ):
492514 os .remove (filepath )
493515 except Exception as e :
494- logger . error (f"Error deleting watermarked images from PVC { save_dir } : { e } " )
516+ logging . exception (f"Error deleting watermarked images from PVC { save_dir } : { e } " )
495517
496518async def get_images_within (camera_id : str , hours : int = 720 ) -> list :
497519 cutoff = datetime .now (timezone .utc ) - timedelta (hours = hours )
0 commit comments