Files
tehran_taj/apps/user-app-v2/src/utils/image.ts
T

17 lines
746 B
TypeScript
Raw Normal View History

// 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}`
}