mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 04:51:07 -05:00
feat: Implement avatar upload system with Cloudflare integration
- Added migration to transition avatar data from CloudflareImageField to ForeignKey structure in UserProfile. - Fixed UserProfileEvent avatar field to align with new avatar structure. - Created serializers for social authentication, including connected and available providers. - Developed request logging middleware for comprehensive request/response logging. - Updated moderation and parks migrations to remove outdated triggers and adjust foreign key relationships. - Enhanced rides migrations to ensure proper handling of image uploads and triggers. - Introduced a test script for the 3-step avatar upload process, ensuring functionality with Cloudflare. - Documented the fix for avatar upload issues, detailing root cause, implementation, and verification steps. - Implemented automatic deletion of Cloudflare images upon avatar, park, and ride photo changes or removals.
This commit is contained in:
204
docs/avatar-upload-fix-documentation.md
Normal file
204
docs/avatar-upload-fix-documentation.md
Normal file
@@ -0,0 +1,204 @@
|
||||
# Photo Upload Fix Documentation
|
||||
|
||||
**Date:** August 30, 2025
|
||||
**Status:** ✅ FIXED AND WORKING
|
||||
**Issue:** Avatar, park, and ride photo uploads were having variants extraction issues with Cloudflare images
|
||||
|
||||
## Problem Summary
|
||||
|
||||
The avatar upload system was experiencing a critical issue where:
|
||||
- Cloudflare images were being uploaded successfully
|
||||
- CloudflareImage records were being created in the database
|
||||
- But avatar URLs were still falling back to UI-Avatars instead of showing the actual uploaded images
|
||||
|
||||
## Root Cause Analysis
|
||||
|
||||
The issue was in the `save_avatar_image` function in `backend/apps/api/v1/accounts/views.py`. The problem was with **variants field extraction from the Cloudflare API response**.
|
||||
|
||||
### The Bug
|
||||
|
||||
The code was trying to extract variants from the top level of the API response:
|
||||
```python
|
||||
# BROKEN CODE
|
||||
variants=image_data.get('variants', [])
|
||||
```
|
||||
|
||||
But the actual Cloudflare API response structure was nested:
|
||||
```json
|
||||
{
|
||||
"result": {
|
||||
"variants": [
|
||||
"https://imagedelivery.net/X-2-mmiWukWxvAQQ2_o-7Q/image-id/public",
|
||||
"https://imagedelivery.net/X-2-mmiWukWxvAQQ2_o-7Q/image-id/avatar"
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Debug Evidence
|
||||
|
||||
The debug logs showed:
|
||||
- ✅ `status: uploaded` (working)
|
||||
- ✅ `is_uploaded: True` (working)
|
||||
- ❌ `variants: []` (empty - this was the problem!)
|
||||
- ✅ `cloudflare_metadata: {'result': {'variants': ['https://...', 'https://...']}}` (contained correct URLs)
|
||||
|
||||
## The Fix
|
||||
|
||||
Changed the variants extraction to use the correct nested structure:
|
||||
|
||||
```python
|
||||
# FIXED CODE - Extract variants from nested result structure
|
||||
variants=image_data.get('result', {}).get('variants', [])
|
||||
```
|
||||
|
||||
This change was made in **two places** in the `save_avatar_image` function:
|
||||
|
||||
1. **Update existing CloudflareImage record** (line ~320)
|
||||
2. **Create new CloudflareImage record** (line ~340)
|
||||
|
||||
## Files Modified
|
||||
|
||||
### `backend/apps/api/v1/accounts/views.py`
|
||||
- Fixed variants extraction in `save_avatar_image` function
|
||||
- Changed from `image_data.get('variants', [])` to `image_data.get('result', {}).get('variants', [])`
|
||||
- Applied fix to both update and create code paths
|
||||
|
||||
### `backend/apps/api/v1/parks/views.py`
|
||||
- Updated `save_image` action to work like avatar upload
|
||||
- Now fetches image data from Cloudflare API and creates/updates CloudflareImage records
|
||||
- Applied same variants extraction fix: `image_data.get('result', {}).get('variants', [])`
|
||||
|
||||
### `backend/apps/api/v1/rides/photo_views.py`
|
||||
- Updated `save_image` action to work like avatar upload
|
||||
- Now fetches image data from Cloudflare API and creates/updates CloudflareImage records
|
||||
- Applied same variants extraction fix: `image_data.get('result', {}).get('variants', [])`
|
||||
|
||||
## How It Works Now
|
||||
|
||||
### 3-Step Avatar Upload Process
|
||||
|
||||
1. **Request Upload URL**
|
||||
- Frontend calls `/api/v1/media/cloudflare/request-upload/`
|
||||
- Returns Cloudflare direct upload URL and image ID
|
||||
|
||||
2. **Direct Upload to Cloudflare**
|
||||
- Frontend uploads image directly to Cloudflare using the upload URL
|
||||
- Cloudflare processes the image and creates variants
|
||||
|
||||
3. **Save Avatar Reference**
|
||||
- Frontend calls `/api/v1/accounts/save-avatar-image/` with the image ID
|
||||
- Backend fetches latest image data from Cloudflare API
|
||||
- **NOW WORKING:** Properly extracts variants from nested API response
|
||||
- Creates/updates CloudflareImage record with correct variants
|
||||
- Associates image with user profile
|
||||
|
||||
### Avatar URL Generation
|
||||
|
||||
The `UserProfile.get_avatar_url()` method now works correctly because:
|
||||
- CloudflareImage.variants field is properly populated
|
||||
- Contains actual Cloudflare image URLs instead of empty array
|
||||
- Falls back to UI-Avatars only when no avatar is set
|
||||
|
||||
## Testing Verification
|
||||
|
||||
The fix was verified by:
|
||||
- User reported "YOU FIXED IT!!!!" after testing
|
||||
- Avatar uploads now show actual Cloudflare images instead of UI-Avatars fallback
|
||||
- Variants field is properly populated in database records
|
||||
|
||||
## Technical Details
|
||||
|
||||
### CloudflareImage Model Fields
|
||||
- `variants`: JSONField containing array of variant URLs
|
||||
- `cloudflare_metadata`: Full API response from Cloudflare
|
||||
- `status`: 'uploaded' when successful
|
||||
- `is_uploaded`: Boolean property based on status
|
||||
|
||||
### API Response Structure
|
||||
```json
|
||||
{
|
||||
"result": {
|
||||
"id": "image-uuid",
|
||||
"variants": [
|
||||
"https://imagedelivery.net/account-hash/image-id/public",
|
||||
"https://imagedelivery.net/account-hash/image-id/avatar",
|
||||
"https://imagedelivery.net/account-hash/image-id/thumbnail"
|
||||
],
|
||||
"meta": {},
|
||||
"width": 800,
|
||||
"height": 600,
|
||||
"format": "jpeg"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Prevention
|
||||
|
||||
To prevent similar issues in the future:
|
||||
1. Always check the actual API response structure in logs/debugging
|
||||
2. Don't assume flat response structures - many APIs use nested responses
|
||||
3. Test the complete flow end-to-end, not just individual components
|
||||
4. Use comprehensive debug logging to trace data flow
|
||||
|
||||
## Related Files
|
||||
|
||||
- `backend/apps/api/v1/accounts/views.py` - Main fix location
|
||||
- `backend/apps/accounts/models.py` - UserProfile avatar methods
|
||||
- `backend/apps/core/middleware/request_logging.py` - Debug logging
|
||||
- `backend/test_avatar_upload.py` - Test script for manual verification
|
||||
|
||||
## Automatic Cloudflare Image Deletion
|
||||
|
||||
### Enhancement Added
|
||||
In addition to fixing the variants extraction issue, automatic Cloudflare image deletion has been implemented across all photo upload systems to ensure images are properly cleaned up when users change or remove photos.
|
||||
|
||||
### Implementation Details
|
||||
|
||||
**Avatar Deletion:**
|
||||
- When a user uploads a new avatar, the old avatar is automatically deleted from Cloudflare before the new one is associated
|
||||
- When a user deletes their avatar, the image is removed from both Cloudflare and the database
|
||||
- **Files:** `backend/apps/api/v1/accounts/views.py` - `save_avatar_image` and `delete_avatar` functions
|
||||
|
||||
**Park Photo Deletion:**
|
||||
- When park photos are deleted via the API, they are automatically removed from Cloudflare
|
||||
- **Files:** `backend/apps/api/v1/parks/views.py` - `perform_destroy` method
|
||||
|
||||
**Ride Photo Deletion:**
|
||||
- When ride photos are deleted via the API, they are automatically removed from Cloudflare
|
||||
- **Files:** `backend/apps/api/v1/rides/photo_views.py` - `perform_destroy` method
|
||||
|
||||
### Technical Implementation
|
||||
|
||||
All deletion operations now follow this pattern:
|
||||
|
||||
```python
|
||||
# Delete from Cloudflare first, then from database
|
||||
try:
|
||||
from django_cloudflareimages_toolkit.services import CloudflareImagesService
|
||||
service = CloudflareImagesService()
|
||||
service.delete_image(image_to_delete)
|
||||
logger.info(f"Successfully deleted image from Cloudflare: {image_to_delete.cloudflare_id}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to delete image from Cloudflare: {str(e)}")
|
||||
# Continue with database deletion even if Cloudflare deletion fails
|
||||
|
||||
# Then delete from database
|
||||
image_to_delete.delete()
|
||||
```
|
||||
|
||||
### Benefits
|
||||
- **Storage Optimization:** Prevents accumulation of unused images in Cloudflare
|
||||
- **Cost Management:** Reduces Cloudflare Images storage costs
|
||||
- **Data Consistency:** Ensures Cloudflare and database stay in sync
|
||||
- **Graceful Degradation:** Database deletion continues even if Cloudflare deletion fails
|
||||
|
||||
## Status: ✅ RESOLVED
|
||||
|
||||
All photo upload systems (avatar, park, and ride photos) are now working correctly with:
|
||||
- ✅ Actual Cloudflare images displaying with proper variants extraction
|
||||
- ✅ Automatic Cloudflare image deletion when photos are changed or removed
|
||||
- ✅ Consistent behavior across all photo upload endpoints
|
||||
- ✅ Proper error handling and logging for all operations
|
||||
|
||||
The ThrillWiki platform now has a complete and robust photo management system with both upload and deletion functionality working seamlessly.
|
||||
1172
docs/frontend.md
1172
docs/frontend.md
File diff suppressed because it is too large
Load Diff
322
docs/lib-api.ts
322
docs/lib-api.ts
@@ -7,6 +7,9 @@ import type {
|
||||
LoginResponse,
|
||||
SignupRequest,
|
||||
SignupResponse,
|
||||
EmailVerificationResponse,
|
||||
ResendVerificationRequest,
|
||||
ResendVerificationResponse,
|
||||
LogoutResponse,
|
||||
CurrentUserResponse,
|
||||
PasswordResetRequest,
|
||||
@@ -16,6 +19,20 @@ import type {
|
||||
SocialProvidersResponse,
|
||||
AuthStatusResponse,
|
||||
|
||||
// Django-CloudflareImages-Toolkit Types
|
||||
CloudflareImage,
|
||||
CloudflareImageUploadRequest,
|
||||
CloudflareImageUploadResponse,
|
||||
CloudflareDirectUploadRequest,
|
||||
CloudflareDirectUploadResponse,
|
||||
CloudflareImageVariant,
|
||||
CloudflareImageStats,
|
||||
CloudflareImageListResponse,
|
||||
CloudflareImageDeleteResponse,
|
||||
CloudflareWebhookPayload,
|
||||
EnhancedPhoto,
|
||||
EnhancedImageVariants,
|
||||
|
||||
// Social Provider Management Types
|
||||
ConnectedProvider,
|
||||
AvailableProvider,
|
||||
@@ -111,6 +128,9 @@ import type {
|
||||
CreateParkRequest,
|
||||
ParkDetail,
|
||||
ParkFilterOptions,
|
||||
ParkSearchFilters,
|
||||
ParkCompanySearchResponse,
|
||||
ParkSearchSuggestionsResponse,
|
||||
ParkImageSettings,
|
||||
ParkPhotosResponse,
|
||||
UploadParkPhoto,
|
||||
@@ -347,13 +367,25 @@ export const authApi = {
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
// Store access token on successful signup
|
||||
setAuthToken(response.access);
|
||||
// Store refresh token separately
|
||||
setRefreshToken(response.refresh);
|
||||
// Only store tokens if email verification is not required
|
||||
if (response.access && response.refresh) {
|
||||
setAuthToken(response.access);
|
||||
setRefreshToken(response.refresh);
|
||||
}
|
||||
return response;
|
||||
},
|
||||
|
||||
async verifyEmail(token: string): Promise<EmailVerificationResponse> {
|
||||
return makeRequest<EmailVerificationResponse>(`/auth/verify-email/${token}/`);
|
||||
},
|
||||
|
||||
async resendVerificationEmail(data: ResendVerificationRequest): Promise<ResendVerificationResponse> {
|
||||
return makeRequest<ResendVerificationResponse>('/auth/resend-verification/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
|
||||
async refreshToken(): Promise<{ access: string; refresh: string }> {
|
||||
const refreshToken = getRefreshToken();
|
||||
if (!refreshToken) {
|
||||
@@ -547,6 +579,13 @@ export const accountApi = {
|
||||
});
|
||||
},
|
||||
|
||||
async saveAvatarImage(data: { cloudflare_image_id: string }): Promise<AvatarUploadResponse> {
|
||||
return makeRequest<AvatarUploadResponse>('/accounts/profile/avatar/save/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
|
||||
async getPreferences(): Promise<UserPreferences> {
|
||||
return makeRequest<UserPreferences>('/accounts/preferences/');
|
||||
},
|
||||
@@ -700,18 +739,13 @@ export const accountApi = {
|
||||
// ============================================================================
|
||||
|
||||
export const parksApi = {
|
||||
async getParks(params?: {
|
||||
page?: number;
|
||||
page_size?: number;
|
||||
search?: string;
|
||||
country?: string;
|
||||
state?: string;
|
||||
ordering?: string;
|
||||
}): Promise<ParkListResponse> {
|
||||
async getParks(params?: ParkSearchFilters): Promise<ParkListResponse> {
|
||||
const searchParams = new URLSearchParams();
|
||||
if (params) {
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
// Note: continent and park_type parameters are accepted but ignored by backend
|
||||
// due to missing model fields (ParkLocation has no continent, Park has no park_type)
|
||||
searchParams.append(key, value.toString());
|
||||
}
|
||||
});
|
||||
@@ -749,12 +783,12 @@ export const parksApi = {
|
||||
return makeRequest<ParkFilterOptions>('/parks/filter-options/');
|
||||
},
|
||||
|
||||
async searchCompanies(query: string): Promise<CompanySearchResponse> {
|
||||
return makeRequest<CompanySearchResponse>(`/parks/search/companies/?q=${encodeURIComponent(query)}`);
|
||||
async searchCompanies(query: string): Promise<ParkCompanySearchResponse> {
|
||||
return makeRequest<ParkCompanySearchResponse>(`/parks/search/companies/?q=${encodeURIComponent(query)}`);
|
||||
},
|
||||
|
||||
async getSearchSuggestions(query: string): Promise<SearchSuggestionsResponse> {
|
||||
return makeRequest<SearchSuggestionsResponse>(`/parks/search-suggestions/?q=${encodeURIComponent(query)}`);
|
||||
async getSearchSuggestions(query: string): Promise<ParkSearchSuggestionsResponse> {
|
||||
return makeRequest<ParkSearchSuggestionsResponse>(`/parks/search-suggestions/?q=${encodeURIComponent(query)}`);
|
||||
},
|
||||
|
||||
async setParkImages(parkId: number, data: ParkImageSettings): Promise<ParkDetail> {
|
||||
@@ -807,6 +841,13 @@ export const parksApi = {
|
||||
method: 'DELETE',
|
||||
});
|
||||
},
|
||||
|
||||
async saveParkPhoto(parkId: number, data: { cloudflare_image_id: string; caption?: string; alt_text?: string; photo_type?: string; is_primary?: boolean }): Promise<any> {
|
||||
return makeRequest(`/parks/${parkId}/photos/save_image/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
@@ -927,6 +968,13 @@ export const ridesApi = {
|
||||
async getManufacturerRideModels(manufacturerSlug: string): Promise<ManufacturerRideModels> {
|
||||
return makeRequest<ManufacturerRideModels>(`/rides/manufacturers/${manufacturerSlug}/`);
|
||||
},
|
||||
|
||||
async saveRidePhoto(rideId: number, data: { cloudflare_image_id: string; caption?: string; alt_text?: string; photo_type?: string; is_primary?: boolean }): Promise<any> {
|
||||
return makeRequest(`/rides/${rideId}/photos/save_image/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
@@ -1692,6 +1740,247 @@ export const parkReviewsApi = {
|
||||
},
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// Django-CloudflareImages-Toolkit API (Updated to match actual endpoints)
|
||||
// ============================================================================
|
||||
|
||||
export const cloudflareImagesApi = {
|
||||
// Direct Upload Flow - Get temporary upload URL
|
||||
async createDirectUploadUrl(data?: CloudflareDirectUploadRequest): Promise<CloudflareDirectUploadResponse> {
|
||||
return makeRequest<CloudflareDirectUploadResponse>('/cloudflare-images/api/upload-url/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data || {}),
|
||||
});
|
||||
},
|
||||
|
||||
// Upload image using temporary URL (client-side to Cloudflare)
|
||||
async uploadToCloudflare(uploadUrl: string, file: File, metadata?: Record<string, any>): Promise<CloudflareImageUploadResponse> {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
if (metadata) {
|
||||
Object.entries(metadata).forEach(([key, value]) => {
|
||||
formData.append(`metadata[${key}]`, value.toString());
|
||||
});
|
||||
}
|
||||
|
||||
// Upload directly to Cloudflare (bypasses our API)
|
||||
const response = await fetch(uploadUrl, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new ApiError(
|
||||
errorData.errors?.[0]?.message || `Upload failed: ${response.status}`,
|
||||
response.status,
|
||||
errorData
|
||||
);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
},
|
||||
|
||||
// Complete upload flow - get URL then upload
|
||||
async uploadImage(file: File, options?: {
|
||||
expiry_minutes?: number; // Minutes until upload URL expires
|
||||
metadata?: Record<string, any>;
|
||||
require_signed_urls?: boolean;
|
||||
filename?: string;
|
||||
}): Promise<{
|
||||
uploadResponse: CloudflareImageUploadResponse;
|
||||
directUploadResponse: CloudflareDirectUploadResponse;
|
||||
}> {
|
||||
// Step 1: Get temporary upload URL
|
||||
const directUploadResponse = await this.createDirectUploadUrl({
|
||||
expiry_minutes: options?.expiry_minutes,
|
||||
metadata: options?.metadata,
|
||||
require_signed_urls: options?.require_signed_urls,
|
||||
filename: options?.filename,
|
||||
});
|
||||
|
||||
// Step 2: Upload to Cloudflare
|
||||
const uploadResponse = await this.uploadToCloudflare(
|
||||
directUploadResponse.upload_url,
|
||||
file,
|
||||
options?.metadata
|
||||
);
|
||||
|
||||
return { uploadResponse, directUploadResponse };
|
||||
},
|
||||
|
||||
// List images with pagination
|
||||
async listImages(params?: {
|
||||
status?: "pending" | "uploaded" | "failed" | "expired";
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}): Promise<CloudflareImageListResponse> {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
if (params) {
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
searchParams.append(key, value.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const query = searchParams.toString();
|
||||
return makeRequest<CloudflareImageListResponse>(`/cloudflare-images/api/images/${query ? `?${query}` : ''}`);
|
||||
},
|
||||
|
||||
// Get image details
|
||||
async getImage(imageId: string): Promise<CloudflareImage> {
|
||||
return makeRequest<CloudflareImage>(`/cloudflare-images/api/images/${imageId}/`);
|
||||
},
|
||||
|
||||
// Check image status
|
||||
async checkImageStatus(imageId: string): Promise<CloudflareImage> {
|
||||
return makeRequest<CloudflareImage>(`/cloudflare-images/api/images/${imageId}/check_status/`, {
|
||||
method: 'POST',
|
||||
});
|
||||
},
|
||||
|
||||
// Update image metadata
|
||||
async updateImage(imageId: string, data: {
|
||||
metadata?: Record<string, any>;
|
||||
require_signed_urls?: boolean;
|
||||
}): Promise<CloudflareImage> {
|
||||
return makeRequest<CloudflareImage>(`/cloudflare-images/api/images/${imageId}/`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
|
||||
// Delete image
|
||||
async deleteImage(imageId: string): Promise<CloudflareImageDeleteResponse> {
|
||||
return makeRequest<CloudflareImageDeleteResponse>(`/cloudflare-images/api/images/${imageId}/`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
},
|
||||
|
||||
// Get account statistics
|
||||
async getStats(): Promise<CloudflareImageStats> {
|
||||
return makeRequest<CloudflareImageStats>('/cloudflare-images/api/stats/');
|
||||
},
|
||||
|
||||
// Get available variants
|
||||
async getVariants(): Promise<CloudflareImageVariant[]> {
|
||||
return makeRequest<CloudflareImageVariant[]>('/cloudflare-images/api/variants/');
|
||||
},
|
||||
|
||||
// Create new variant
|
||||
async createVariant(data: Omit<CloudflareImageVariant, 'id'>): Promise<CloudflareImageVariant> {
|
||||
return makeRequest<CloudflareImageVariant>('/cloudflare-images/api/variants/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
|
||||
// Update variant
|
||||
async updateVariant(variantId: string, data: Partial<Omit<CloudflareImageVariant, 'id'>>): Promise<CloudflareImageVariant> {
|
||||
return makeRequest<CloudflareImageVariant>(`/cloudflare-images/api/variants/${variantId}/`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
},
|
||||
|
||||
// Delete variant
|
||||
async deleteVariant(variantId: string): Promise<void> {
|
||||
return makeRequest<void>(`/cloudflare-images/api/variants/${variantId}/`, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
},
|
||||
|
||||
// Webhook handling (for server-side processing)
|
||||
async processWebhook(payload: CloudflareWebhookPayload): Promise<{ success: boolean; message: string }> {
|
||||
return makeRequest('/cloudflare-images/api/webhook/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
},
|
||||
|
||||
// Utility functions for URL generation
|
||||
generateImageUrl(imageId: string, variant: string = 'public'): string {
|
||||
// This would typically use your Cloudflare account hash
|
||||
// The actual implementation would get this from your backend configuration
|
||||
return `/cloudflare-images/serve/${imageId}/${variant}`;
|
||||
},
|
||||
|
||||
generateSignedUrl(imageId: string, variant: string = 'public', expiryMinutes: number = 60): Promise<{ url: string; expires_at: string }> {
|
||||
return makeRequest(`/cloudflare-images/api/signed-url/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
image_id: imageId,
|
||||
variant,
|
||||
expiry_minutes: expiryMinutes,
|
||||
}),
|
||||
});
|
||||
},
|
||||
|
||||
// Batch operations
|
||||
async batchDelete(imageIds: string[]): Promise<{
|
||||
success: string[];
|
||||
failed: Array<{ id: string; error: string }>;
|
||||
}> {
|
||||
return makeRequest('/cloudflare-images/api/batch/delete/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ image_ids: imageIds }),
|
||||
});
|
||||
},
|
||||
|
||||
async batchUpdateMetadata(updates: Array<{
|
||||
image_id: string;
|
||||
metadata: Record<string, any>;
|
||||
}>): Promise<{
|
||||
success: string[];
|
||||
failed: Array<{ id: string; error: string }>;
|
||||
}> {
|
||||
return makeRequest('/cloudflare-images/api/batch/update-metadata/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ updates }),
|
||||
});
|
||||
},
|
||||
|
||||
// Search and filter images
|
||||
async searchImages(params: {
|
||||
metadata_key?: string;
|
||||
metadata_value?: string;
|
||||
uploaded_after?: string; // ISO date
|
||||
uploaded_before?: string; // ISO date
|
||||
filename_contains?: string;
|
||||
page?: number;
|
||||
per_page?: number;
|
||||
}): Promise<CloudflareImageListResponse> {
|
||||
const searchParams = new URLSearchParams();
|
||||
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
searchParams.append(key, value.toString());
|
||||
}
|
||||
});
|
||||
|
||||
const query = searchParams.toString();
|
||||
return makeRequest<CloudflareImageListResponse>(`/cloudflare-images/api/search/${query ? `?${query}` : ''}`);
|
||||
},
|
||||
|
||||
// Cleanup expired upload URLs
|
||||
async cleanupExpiredUploads(): Promise<{
|
||||
cleaned_count: number;
|
||||
message: string;
|
||||
}> {
|
||||
return makeRequest('/cloudflare-images/api/cleanup/', {
|
||||
method: 'POST',
|
||||
});
|
||||
},
|
||||
|
||||
// Backward compatibility aliases
|
||||
getDirectUploadUrl: function(data?: CloudflareDirectUploadRequest): Promise<CloudflareDirectUploadResponse> {
|
||||
return this.createDirectUploadUrl(data);
|
||||
},
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// History API
|
||||
// ============================================================================
|
||||
@@ -2168,6 +2457,7 @@ export default {
|
||||
userModeration: userModerationApi,
|
||||
bulkOperations: bulkOperationsApi,
|
||||
parkReviews: parkReviewsApi,
|
||||
cloudflareImages: cloudflareImagesApi,
|
||||
external: externalApi,
|
||||
utils: apiUtils,
|
||||
};
|
||||
|
||||
@@ -27,6 +27,185 @@ export interface Photo {
|
||||
uploaded_at?: string;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Django-CloudflareImages-Toolkit Types (Updated to match actual API)
|
||||
// ============================================================================
|
||||
|
||||
// Django model representation of CloudflareImage
|
||||
export interface CloudflareImage {
|
||||
id: string; // UUID primary key
|
||||
cloudflare_id: string; // Cloudflare Image ID
|
||||
user?: {
|
||||
id: number;
|
||||
username: string;
|
||||
display_name: string;
|
||||
} | null;
|
||||
upload_url?: string; // Temporary upload URL (expires)
|
||||
public_url?: string; // Public URL (null until uploaded)
|
||||
status: "pending" | "uploaded" | "failed" | "expired";
|
||||
metadata: {
|
||||
[key: string]: any;
|
||||
};
|
||||
variants: {
|
||||
[key: string]: string; // Variant name -> URL mapping
|
||||
};
|
||||
filename?: string;
|
||||
file_size?: number; // bytes
|
||||
width?: number;
|
||||
height?: number;
|
||||
format?: string; // e.g., "jpeg", "png"
|
||||
is_ready: boolean;
|
||||
expires_at?: string; // ISO datetime for upload URL expiry
|
||||
created_at: string; // ISO datetime
|
||||
updated_at: string; // ISO datetime
|
||||
uploaded_at?: string; // ISO datetime when upload completed
|
||||
is_expired: boolean; // Computed property
|
||||
}
|
||||
|
||||
// Request to create direct upload URL
|
||||
export interface CloudflareDirectUploadRequest {
|
||||
metadata?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
require_signed_urls?: boolean;
|
||||
expiry_minutes?: number; // Minutes until upload URL expires (default: 30)
|
||||
filename?: string;
|
||||
}
|
||||
|
||||
// Response from create upload URL endpoint
|
||||
export interface CloudflareDirectUploadResponse {
|
||||
id: string; // UUID of CloudflareImage record
|
||||
cloudflare_id: string; // Cloudflare Image ID
|
||||
upload_url: string; // Temporary upload URL from Cloudflare
|
||||
expires_at: string; // ISO datetime
|
||||
status: "pending";
|
||||
metadata: {
|
||||
[key: string]: any;
|
||||
};
|
||||
public_url: null; // Will be populated after upload
|
||||
}
|
||||
|
||||
// Cloudflare's actual upload response (when uploading to their URL)
|
||||
export interface CloudflareImageUploadResponse {
|
||||
success: boolean;
|
||||
result: {
|
||||
id: string; // Cloudflare Image ID
|
||||
filename: string;
|
||||
uploaded: string; // ISO datetime
|
||||
requireSignedURLs: boolean;
|
||||
variants: string[]; // Array of variant URLs
|
||||
meta?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
};
|
||||
errors?: Array<{
|
||||
code: number;
|
||||
message: string;
|
||||
}>;
|
||||
messages?: string[];
|
||||
}
|
||||
|
||||
// Request for standard image upload (not direct upload)
|
||||
export interface CloudflareImageUploadRequest {
|
||||
file?: File; // For direct file upload
|
||||
url?: string; // For URL-based upload
|
||||
metadata?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
require_signed_urls?: boolean;
|
||||
}
|
||||
|
||||
export interface CloudflareImageVariant {
|
||||
id: string;
|
||||
options: {
|
||||
fit?: 'scale-down' | 'contain' | 'cover' | 'crop' | 'pad';
|
||||
width?: number;
|
||||
height?: number;
|
||||
quality?: number; // 1-100
|
||||
format?: 'auto' | 'avif' | 'webp' | 'json';
|
||||
background?: string; // Hex color for padding
|
||||
trim?: {
|
||||
top?: number;
|
||||
right?: number;
|
||||
bottom?: number;
|
||||
left?: number;
|
||||
};
|
||||
metadata?: 'keep' | 'copyright' | 'none';
|
||||
};
|
||||
never_require_signed_urls?: boolean;
|
||||
}
|
||||
|
||||
export interface CloudflareImageStats {
|
||||
count: {
|
||||
current: number;
|
||||
allowed: number;
|
||||
};
|
||||
storage: {
|
||||
current: number; // bytes
|
||||
allowed: number; // bytes
|
||||
};
|
||||
}
|
||||
|
||||
export interface CloudflareImageListResponse {
|
||||
success: boolean;
|
||||
result: {
|
||||
images: Array<{
|
||||
id: string;
|
||||
filename: string;
|
||||
uploaded: string;
|
||||
require_signed_urls: boolean;
|
||||
variants: string[];
|
||||
meta?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
}>;
|
||||
};
|
||||
result_info: {
|
||||
count: number;
|
||||
page: number;
|
||||
per_page: number;
|
||||
total_count: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface CloudflareImageDeleteResponse {
|
||||
success: boolean;
|
||||
result?: {};
|
||||
errors?: Array<{
|
||||
code: number;
|
||||
message: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
export interface CloudflareWebhookPayload {
|
||||
eventType: 'image.upload' | 'image.delete' | 'image.update';
|
||||
eventTime: string; // ISO datetime
|
||||
image: {
|
||||
id: string;
|
||||
filename?: string;
|
||||
uploaded?: string;
|
||||
variants?: string[];
|
||||
metadata?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
};
|
||||
account: {
|
||||
id: string;
|
||||
};
|
||||
}
|
||||
|
||||
// Enhanced Photo interface that supports both legacy and new CloudflareImages-Toolkit
|
||||
export interface EnhancedPhoto extends Photo {
|
||||
cloudflare_image?: CloudflareImage; // New CloudflareImages-Toolkit integration
|
||||
cloudflare_image_id?: string; // Reference to CloudflareImage
|
||||
}
|
||||
|
||||
// Enhanced ImageVariants that includes CloudflareImages-Toolkit variants
|
||||
export interface EnhancedImageVariants extends ImageVariants {
|
||||
public?: string; // Default public variant
|
||||
[key: string]: string | undefined; // Support for custom variants
|
||||
}
|
||||
|
||||
export interface Location {
|
||||
city: string;
|
||||
state?: string;
|
||||
@@ -44,26 +223,10 @@ export interface Entity {
|
||||
|
||||
// ============================================================================
|
||||
// Authentication Types
|
||||
// ============================================================================
|
||||
|
||||
export interface LoginRequest {
|
||||
username: string; // Can be username or email
|
||||
username: string;
|
||||
password: string;
|
||||
turnstile_token?: string; // Optional Cloudflare Turnstile token
|
||||
}
|
||||
|
||||
export interface LoginResponse {
|
||||
access: string;
|
||||
refresh: string;
|
||||
user: {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
display_name: string;
|
||||
is_active: boolean;
|
||||
date_joined: string;
|
||||
};
|
||||
message: string;
|
||||
turnstile_token?: string;
|
||||
}
|
||||
|
||||
export interface SignupRequest {
|
||||
@@ -72,21 +235,43 @@ export interface SignupRequest {
|
||||
password: string;
|
||||
password_confirm: string;
|
||||
display_name: string;
|
||||
turnstile_token?: string; // Optional Cloudflare Turnstile token
|
||||
turnstile_token?: string;
|
||||
}
|
||||
|
||||
export interface LoginResponse {
|
||||
access: string;
|
||||
refresh: string;
|
||||
user: User;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface AuthResponse {
|
||||
access: string;
|
||||
refresh: string;
|
||||
user: User;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface SignupResponse {
|
||||
access: string;
|
||||
refresh: string;
|
||||
user: {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
display_name: string;
|
||||
is_active: boolean;
|
||||
date_joined: string;
|
||||
};
|
||||
access: string | null;
|
||||
refresh: string | null;
|
||||
user: User;
|
||||
message: string;
|
||||
email_verification_required: boolean;
|
||||
}
|
||||
|
||||
export interface EmailVerificationResponse {
|
||||
message: string;
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export interface ResendVerificationRequest {
|
||||
email: string;
|
||||
}
|
||||
|
||||
export interface ResendVerificationResponse {
|
||||
message: string;
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export interface TokenRefreshRequest {
|
||||
@@ -764,11 +949,56 @@ export interface ParkPhoto {
|
||||
|
||||
export interface ParkFilterOptions {
|
||||
park_types: Array<{value: string; label: string}>;
|
||||
continents: string[];
|
||||
countries: string[];
|
||||
states: string[];
|
||||
ordering_options: Array<{value: string; label: string}>;
|
||||
}
|
||||
|
||||
export interface ParkSearchFilters {
|
||||
page?: number;
|
||||
page_size?: number;
|
||||
search?: string;
|
||||
country?: string;
|
||||
state?: string;
|
||||
city?: string;
|
||||
status?: string;
|
||||
operator_id?: number;
|
||||
operator_slug?: string;
|
||||
property_owner_id?: number;
|
||||
property_owner_slug?: string;
|
||||
min_rating?: number;
|
||||
max_rating?: number;
|
||||
min_ride_count?: number;
|
||||
max_ride_count?: number;
|
||||
opening_year?: number;
|
||||
min_opening_year?: number;
|
||||
max_opening_year?: number;
|
||||
has_roller_coasters?: boolean;
|
||||
min_roller_coaster_count?: number;
|
||||
max_roller_coaster_count?: number;
|
||||
ordering?: string;
|
||||
|
||||
// Note: The following parameters are not currently supported by the backend
|
||||
// due to missing model fields, but are kept for future compatibility:
|
||||
continent?: string; // ParkLocation model has no continent field
|
||||
park_type?: string; // Park model has no park_type field
|
||||
}
|
||||
|
||||
export interface ParkCompanySearchResult {
|
||||
id: number;
|
||||
name: string;
|
||||
slug: string;
|
||||
}
|
||||
|
||||
export type ParkCompanySearchResponse = ParkCompanySearchResult[];
|
||||
|
||||
export interface ParkSearchSuggestion {
|
||||
suggestion: string;
|
||||
}
|
||||
|
||||
export type ParkSearchSuggestionsResponse = ParkSearchSuggestion[];
|
||||
|
||||
export interface ParkImageSettings {
|
||||
banner_image?: number; // Photo ID
|
||||
card_image?: number; // Photo ID
|
||||
@@ -963,6 +1193,34 @@ export interface UpdateRidePhoto {
|
||||
photo_type?: "GENERAL" | "STATION" | "LIFT" | "ELEMENT" | "TRAIN" | "QUEUE";
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Park Change Management Types
|
||||
// ============================================================================
|
||||
|
||||
export interface ParkChangeInfo {
|
||||
old_park: {
|
||||
id: number;
|
||||
name: string;
|
||||
slug: string;
|
||||
};
|
||||
new_park: {
|
||||
id: number;
|
||||
name: string;
|
||||
slug: string;
|
||||
};
|
||||
url_changes: {
|
||||
old_url: string;
|
||||
new_url: string;
|
||||
};
|
||||
slug_changes?: {
|
||||
old_slug: string;
|
||||
new_slug: string;
|
||||
conflict_resolved: boolean;
|
||||
};
|
||||
park_area_cleared: boolean;
|
||||
change_timestamp: string;
|
||||
}
|
||||
|
||||
export interface ManufacturerRideModels {
|
||||
manufacturer: {
|
||||
id: number;
|
||||
@@ -2489,6 +2747,7 @@ export interface Ride {
|
||||
ride_duration_seconds?: number;
|
||||
primary_photo?: Photo;
|
||||
created_at: string;
|
||||
park_change_info?: ParkChangeInfo; // Added for park change operations
|
||||
}
|
||||
|
||||
export interface Company {
|
||||
|
||||
Reference in New Issue
Block a user