TurboStack provides a unified media service with a provider pattern that allows you to easily switch between different media providers like UploadThing and Cloudinary.
Important: When adding or modifying media endpoints, always update the
OpenAPI/Swagger documentation! This ensures the API documentation stays in
sync with your code.
# Media Provider (uploadthing | cloudinary)MEDIA_PROVIDER=uploadthing# UploadThingUPLOADTHING_TOKEN=your_token_here# CloudinaryCLOUDINARY_CLOUD_NAME=your_cloud_nameCLOUDINARY_API_KEY=your_api_keyCLOUDINARY_API_SECRET=your_api_secret
2
Provider Setup
Configure the media service in lib/media/media.service.ts:
Copy
import { MediaService } from "@/lib/media/media.service";import { UploadThingProvider } from "@/lib/media/providers/uploadthing";import { CloudinaryProvider } from "@/lib/media/providers/cloudinary";// Select provider based on environmentconst provider = process.env.MEDIA_PROVIDER === "cloudinary" ? new CloudinaryProvider() : new UploadThingProvider();export const mediaService = new MediaService(provider);
When you create or modify a media endpoint, add comprehensive OpenAPI
documentation with: - Tags (use “Media”) - Summary and description -
Request/response examples - Error responses (400, 404, 500)
Validate file types
Always validate file types and sizes on the backend before uploading.
Use transformations
Apply transformations to optimize images for web delivery (resize, compress,
format conversion).
Handle errors gracefully
Provide clear error messages when uploads fail or files are not found.