feat: compress uploads and serve resized image variants via /api/img
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
// Uploaded images through the API's resize endpoint (GET /api/img/:file?w=).
|
||||
// Anything that is not a plain upload path (blob:, data:, bundled assets,
|
||||
// absolute URLs) is returned untouched.
|
||||
const UPLOAD_PREFIXES = ['/uploads/', '/api/uploads/']
|
||||
|
||||
/** Must match IMAGE_WIDTHS in apps/api image-variants.service.ts. */
|
||||
export type ImageWidth = 200 | 400 | 600 | 800 | 1200 | 1600
|
||||
|
||||
export function imageUrl(url: string | null | undefined, width: ImageWidth): string | undefined {
|
||||
if (!url) return undefined
|
||||
const prefix = UPLOAD_PREFIXES.find((p) => url.startsWith(p))
|
||||
if (!prefix) return url
|
||||
const name = url.slice(prefix.length)
|
||||
if (!name || name.includes('/')) return url
|
||||
return `/api/img/${encodeURIComponent(name)}?w=${width}`
|
||||
}
|
||||
Reference in New Issue
Block a user