courseworx/frontend/src/utils/imageUtils.js
mmabdalla c06600f263 v2.0.1 - CRITICAL FIX: Video Upload Bug - Content Creation File Upload Issue
- Fixed parameter shadowing in useContentManagement.js handleAddContent function
- Changed selectedFile parameter to selectedFileParam to avoid state variable shadowing
- Added fallback logic: fileToUpload = selectedFileParam || selectedFile
- Updated all upload logic references to use fileToUpload instead of selectedFile
- Enhanced debugging with useEffect tracking and stack traces
- Fixed React error in LessonDetail.js with null checks for nextSibling
- Fixed media authentication by adding token to query parameters in imageUtils.js
- Updated dependency arrays for proper state management
- Resolved video upload issue during initial content creation

Files modified:
- frontend/src/hooks/useContentManagement.js
- frontend/src/hooks/useFileUpload.js
- frontend/src/pages/CourseContentViewer.js
- frontend/src/pages/LessonDetail.js
- frontend/src/utils/imageUtils.js
- backend/routes/courseContent.js
- version.txt
2025-09-14 04:12:23 +03:00

52 lines
1.8 KiB
JavaScript

/**
* Media Utilities
* Clean, secure media URL handling for CourseWorx
* Supports future video security and anti-theft measures
*/
// Utility function to get secure media URL from relative path
export const getImageUrl = (imagePath) => {
if (!imagePath) return null;
// If it's already a full URL, return as is
if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) {
return imagePath;
}
// Get authentication token for media access
const token = localStorage.getItem('token');
// Convert to secure media endpoint
if (imagePath.startsWith('/uploads/')) {
// Use relative path from frontend origin
const relativePath = imagePath.replace('/uploads/', '');
const baseUrl = `/api/media/${relativePath}`;
// Add token as query parameter for authentication
return token ? `${baseUrl}?token=${token}` : baseUrl;
}
// If it's just a filename, construct secure media URL
const baseUrl = `/api/media/courses/${imagePath}`;
// Add token as query parameter for authentication
return token ? `${baseUrl}?token=${token}` : baseUrl;
};
// Utility function for all media types (images, videos, documents)
export const getMediaUrl = (mediaPath) => {
return getImageUrl(mediaPath); // Same logic for all media types
};
// Legacy compatibility functions (to fix compilation errors)
export const getFileServingUrl = (filePath) => {
return getImageUrl(filePath); // Use same secure endpoint for all files
};
export const getBestImageUrl = (imagePath) => {
return getImageUrl(imagePath); // Use same secure endpoint for images
};
// Utility function to get thumbnail URL from thumbnail string
export const getThumbnailUrl = (thumbnailPath) => {
if (!thumbnailPath) return null;
return getImageUrl(thumbnailPath);
};