<https://github.com/Disfactory/Disfactory/pull/660|#660 Support multiple image upload backends with ImageBB and automatic fallback>
This PR implements a comprehensive solution for multiple image upload backends to address Imgur API being blocked in Taiwan.
## Problem
The current implementation only supports Imgur for image uploads, which is problematic because:
• Imgur's API is blocked in Taiwan
• No fallback mechanism when external services fail
• Frontend handles image upload, limiting backend control
## Solution
### :wrench: New Multi-Backend Image Upload Service
Created `api/services/image_upload.py` with:
• *Abstract backend interface* for pluggable upload providers
• *ImgurBackend* - Enhanced existing Imgur support with better error handling
• *ImageBBBackend* - New backend supporting ImageBB API as Imgur alternative
• *LocalBackend* - Fallback local storage when external services fail
• *ImageUploadService* - Orchestrates backends with automatic failover
### :rocket: New Direct Upload API Endpoint
Added `POST /factories/{id}/images/upload` supporting:
• Direct multipart file uploads (no more frontend pre-upload)
• All existing metadata: GPS coordinates, datetime, contact info
• Automatic backend selection with graceful fallback
• Same response format as existing endpoint
### :gear: Enhanced Configuration
# Existing Imgur support
DISFACTORY_IMGUR_CLIENT_ID=your_imgur_client_id
# New ImageBB support
DISFACTORY_IMAGEBB_API_KEY=your_imagebb_api_key
Service automatically detects available backends and provides fallback order:
*Imgur → ImageBB → Local Storage*
### :arrows_counterclockwise: Backward Compatibility
• Existing `/factories/{id}/images` URL-based endpoint unchanged
• Legacy `upload_image` task enhanced to use new multi-backend service
• All existing functionality preserved
• No database schema changes required
### :test_tube: Comprehensive Testing
• Unit tests for all backend implementations
• Integration tests for new file upload endpoint
• Mock validation confirms service logic
• Management commands for testing and maintenance
### :books: Documentation & Tools
• Complete user guide with API examples
• Migration guide for frontend developers
• Enhanced `reupload` management command with dry-run support
• New `test_image_upload` command for service validation
## Usage Examples
### Frontend Integration (New Direct Upload)
const formData = new FormData();
formData.append('image', imageFile);
formData.append('nickname', 'Reporter');
formData.append('Latitude', '23.5');
fetch(`/api/factories/${factoryId}/images/upload`, {
method: 'POST',
body: formData
});
### Backend Service Usage
from api.services.image_upload import ImageUploadService
service = ImageUploadService()
result = service.upload_image(image_buffer)
# Automatically tries Imgur → ImageBB → Local with detailed logging
## Benefits
:white_check_mark: *Reliability*: Multiple providers ensure upload success even when services are blocked
:white_check_mark: *Flexibility*: Easy to add new backends (Cloudinary, AWS S3, etc.)
:white_check_mark: *Control*: Backend handles uploads with better error handling and logging
:white_check_mark: *Compatibility*: Existing integrations continue working unchanged
:white_check_mark: *Monitoring*: Detailed logging shows which backend succeeded/failed
This implementation ensures robust image upload functionality regardless of regional service restrictions while maintaining full backward compatibility.
Fixes <https://github.com/Disfactory/Disfactory/issues/659|#659>.
Warning
Firewall rules blocked me from connecting to one or more addresses
#### I tried to connect to the following addresses, but was blocked by firewall rules:
• `<http://esm.ubuntu.com|esm.ubuntu.com>`
• Triggering command: `/usr/lib/apt/methods/https ` (dns block)
If you need me to access, download, or install something from one of these locations, you can either:
• Configure <https://gh.io/copilot/actions-setup-steps|Actions setup steps> to set up my environment, which run before the firewall is enabled
• Add the appropriate URLs or hosts to my <https://gh.io/copilot/firewall-config|firewall allow list>
---
:speech_balloon: Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click <https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent|here> to start the survey.