During the development of a JSF-based property rental project, I encountered critical issues managing photo uploads. The system relied on a database and a directory for storing and displaying property images. Over time, inconsistencies emerged, such as:
- Non-existent photo references in the database: Files referenced in the database were missing from the directory.
- Unused photos in the directory: Files present in the directory but not referenced in the database.
- Corrupted filenames: Filenames with incorrect extensions or invalid formats.
- Performance issues: The
advertisementImagesfolder became so large that the development environment (Eclipse) slowed down significantly.
To address these challenges, I created a Bash script to clean, synchronize, and manage the advertisementImages folder and its relationship with the database.
-
Identify and Remove Unused Photos:
- Detects photos in the
advertisementImagesfolder that are not referenced in the database. - Removes these "junk files" to save space and improve performance.
- Detects photos in the
-
Handle Missing Photos:
- Identifies photos referenced in the database but missing from the folder.
- Prompts the user to upload the missing files.
-
Rename Corrupted Photos:
- Detects files with invalid or missing extensions.
- Renames them using a timestamp and their detected MIME type.
-
Synchronize Database and Folder:
- Updates the database with new filenames when files are renamed.
- Ensures the folder and database are in sync.
-
Relocate Unused Files:
- Moves unused photos to a temporary directory for manual review.
- Linux Environment: The script uses Bash shell commands.
- MySQL Database: Ensure the database connection is configured in
~/.my.cnffor authentication. - Required Tools:
file: For detecting MIME types.sed: For processing database query results.
The script expects two arguments:
imagesFolder: Path to the mainadvertisementImagesdirectory.imagesFolderToAdd: Path to a folder containing additional photos to be checked and added.
chmod +x test.sh
./test.sh /path/to/advertisementImages /path/to/imagesFolderToAddThe script will:
- Print logs of detected issues.
- Display actions taken (e.g., renaming, moving files).
- Indicate when manual intervention is required.
-
Initialization:
- Reads images from the folder and the database.
- Initializes arrays to track files and issues.
-
Corrupted Filenames:
- Identifies files with missing or invalid extensions.
- Renames them with valid extensions based on their MIME type.
-
Unused Files:
- Compares folder contents with database records.
- Removes files not referenced in the database.
-
Missing Files:
- Prompts the user to upload missing photos.
- Updates the database once files are added.
-
Final Synchronization:
- Ensures the folder and database are fully synchronized.
lsCorruptedNames: Detects files with invalid extensions.excludeJunkFiles: Identifies and removes unused photos.addMissingDbFiles: Tracks database references without corresponding files.renamCorruptedNames: Renames corrupted filenames in the folder and updates the database.moveExtraPhotoFolder: Moves unused photos to a temporary folder for review.
- Automation: The script automates tedious tasks, reducing manual effort and errors.
- Error Handling: Improved error detection (e.g., invalid SQL syntax, missing files).
- Performance: Cleaning the folder significantly reduced the load on the development environment.
- Scalability: Designed to handle large numbers of files efficiently.
- Logging: Add structured logs for better traceability.
- GUI Integration: Create a web interface for non-technical users.
- Cross-Platform Support: Adapt the script for Windows and macOS.
Feel free to contribute by reporting issues or submitting pull requests on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.
