loftwah / linkarooie

Simplify your online presence with a single link.
https://linkarooie.com
MIT License
24 stars 4 forks source link

Enhance Image Validation and Handling #155

Open loftwah opened 3 days ago

loftwah commented 3 days ago

We need to improve our image validation and handling process to ensure that only valid image URLs are accepted and processed correctly. This involves updating the User model validation and the OpenGraphImageGenerator class. Additionally, we need to add comprehensive tests to cover these changes. Current issues:

The application doesn't handle non-image URLs gracefully when they're provided in image URL fields. The OpenGraphImageGenerator may encounter errors when processing invalid image URLs.

Tasks:

Update User Model Validation:

Implement a custom validator for the avatar_url field to ensure it points to a valid image. Add error handling to provide meaningful feedback when an invalid URL is provided.

Enhance OpenGraphImageGenerator:

Improve the valid_image_url? method to perform more robust checks. Implement better error handling and fallback mechanisms when processing images.

Implement Comprehensive Testing:

Write unit tests for the User model's image URL validation. Create integration tests for the OpenGraphImageGenerator to cover various scenarios (valid images, invalid URLs, non-image URLs, etc.). Implement system tests to ensure the entire image processing flow works as expected.

Refactor Existing Code:

Review and update any other parts of the codebase that handle image URLs to ensure consistent validation and error handling.

Update Error Messages and User Feedback:

Ensure clear and helpful error messages are provided to users when they input invalid image URLs.

Documentation:

Update relevant documentation to reflect the new image validation process. Add comments to the code explaining the validation logic and error handling.

Detailed Tasks:

User Model Updates:

Implement a validate_image_url method in the User model. Use ActiveRecord callbacks to validate the avatar_url before save.

OpenGraphImageGenerator Updates:

Enhance the valid_image_url? method to check content type and image dimensions. Implement more robust error handling in the download_image method.

Testing:

Write RSpec tests for the User model's image URL validation. Create tests for the OpenGraphImageGenerator covering various scenarios. Implement system tests using Capybara to test the entire image upload and processing flow.

Error Handling:

Implement custom error classes for different types of image validation failures. Update controllers to handle these custom errors and provide appropriate user feedback.

This enhancement will improve the reliability of our image handling process and provide a better user experience when dealing with avatar and other image uploads.

loftwah commented 10 hours ago

I might have sorted this out. Check it first.