Overview
Thesecurity package provides comprehensive safety mechanisms for the Camera Workflow application, including disk space verification, file integrity checks, processing markers, and automatic recovery from interrupted operations.
Types
SecurityChecker
Main security checker that performs validation and safety operations.Minimum size ratio for general output validation (default: 0.005)
Minimum size ratio for AVIF files (default: 0.001)
Minimum size ratio for WebP files (default: 0.003)
Functions
NewSecurityChecker
Creates a new SecurityChecker instance.Minimum acceptable output size ratio (output_size / input_size)
Format-specific ratio for AVIF files
Format-specific ratio for WebP files
Example
Methods
CheckDiskSpace
Verifies that sufficient disk space is available for conversion.Path to source directory
Path to destination directory
Returns an error if insufficient disk space is available
Example
Space Calculation
- Estimates needed space as 50% of source directory size
- Compares against available space in destination
- Platform-specific implementations for accurate free space detection
VerifyOutputFile
Performs comprehensive validation of a converted output file.Path to original input file
Path to converted output file
Type of file (“photo” or “video”)
Output format (“avif”, “webp”, “mp4”, etc.)
Returns an error if validation fails (file is corrupted or too small)
Validation Steps
- Existence Check: Verify output file exists
- Size Check: Ensure file is not empty
- Ratio Check: Validate minimum size ratio (format-specific)
- Integrity Check: Verify file can be opened by appropriate tool
- Photos: Uses ImageMagick
identify - Videos: Uses FFmpeg
ffprobe
- Photos: Uses ImageMagick
Example
SafeDelete
Safely deletes an original file after triple verification.Path to original file to delete
Path to converted output file (must exist and be valid)
Returns an error if deletion is unsafe or fails
Safety Checks
- Output file exists
- Output file is not empty
- Output file size > 1000 bytes (minimum threshold)
IsFileCorrupted
Checks if an existing file is corrupted or incomplete.Path to file to check
Type of file (“photo” or “video”)
Returns true if file is corrupted, missing, or invalid
Example
CreateProcessingMarker
Creates a marker file to track active conversions.Path to file being converted
Returns an error if marker creation fails
Marker Format
Creates a.processing file containing:
RemoveProcessingMarker
Removes the processing marker after conversion completes.Path to file (marker is filePath + “.processing”)
FindAbandonedMarkers
Finds processing markers from previous runs where the process has died.Directory to search for abandoned markers
List of abandoned marker file paths
Returns an error if directory traversal fails
Detection Logic
- Finds all
.processingfiles - Reads PID from marker file
- Checks if process with that PID still exists
- Returns markers where process is dead
CleanupAbandonedFiles
Removes temporary and abandoned files from interrupted conversions.Directory to clean up
Returns an error if cleanup fails
Cleanup Targets
.tmpfiles (temporary conversion outputs).processingmarkers from dead processes
VerifyFileIntegrity
Performs comprehensive integrity check on a file.Path to file to verify
Type of file (“photo” or “video”)
Returns an error if file is corrupted or inaccessible
Verification Steps
- File exists
- File is not empty
- File can be opened
- First 1KB can be read
- Type-specific validation (ImageMagick or FFprobe)
Platform-Specific Functions
The package includes platform-specific implementations for:Disk Space Detection
- Unix/Linux:
diskspace_unix.go- Usessyscall.Statfs - Windows:
diskspace_windows.go- Uses Windows API
Process Checking
- Unix/Linux:
process_check_unix.go- Checks/proc/{pid} - Windows:
process_check_windows.go- Uses Windows process API
Safety Guarantees
The security package ensures:- No data loss: Triple verification before deletion
- Atomic operations: Files converted to
.tmpthen renamed - Crash recovery: Processing markers enable automatic recovery
- Integrity validation: All outputs verified with external tools
- Space safety: Pre-flight disk space checks