POST /api/upload
Upload an image file to Azure Blob Storage. Returns the permanent URL of the uploaded file.Authentication
Requires a valid NextAuth session.Request Body
This endpoint acceptsmultipart/form-data with a file field.
Parameters
The image file to upload.Supported formats:
- PNG
- JPEG/JPG
- GIF
- WebP
- Any image format supported by browsers
Response
Success Response (200)
Permanent URL to the uploaded file in Azure Blob Storage
Error Responses
Example Request (JavaScript)
Example Request (React)
File Naming
Uploaded files are automatically named with the following pattern:1704067200000-photo.jpg
This ensures unique filenames and prevents conflicts.
Storage Details
Container:catafract
Storage Account: Configured via AZURE_STORAGE_CONNECTION_STRING
Access: Public read access (URLs are publicly accessible)
Processing Steps
-
Validation:
- Check authentication
- Verify file exists in request
-
File Processing:
- Convert file to Buffer
- Generate unique filename with timestamp
- Preserve original filename
-
Upload:
- Upload to Azure Blob Storage
- Set appropriate content type
- Return public URL
MIME Type Detection
The endpoint automatically detects and sets the correct MIME type based on the file’s content type from the browser.File Size Limits
File size limits are determined by:- Next.js body size limit (default: 4MB)
- Azure Blob Storage limits
- Browser capabilities
Best Practices
- Always validate file types on the client side
- Show upload progress to users
- Handle errors gracefully
- Store the returned URL for later use
- Consider implementing file size validation
Security Considerations
- Only authenticated users can upload files
- Files are stored in a public container
- No virus scanning is implemented
- No file type validation beyond browser checks
- Consider implementing additional security measures for production