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","isHeic","file","b","heicToJpegBlob","out","n","decode","blob","url","resolve","img","sizeOf","d","encode","maxEdge","type","quality","w","h","scale","width","height","c","ctx","res","renamed","name","ext","shrinkForUpload","source","jpeg","pic","keepPng","firstType","first","edge","isPicture"],"sources":["../../../../../../src/lib/studio/shrink.ts"],"sourcesContent":["/** Pictures made small enough to cross the host's request limit.\n *\n *  A studio upload is a function request, and the host caps a function's\n *  request body at 4.5 MB. Phone photos routinely exceed that, and the attach\n *  control answered them with a 413 that surfaced as a raw `SyntaxError` — 144\n *  rage clicks from 118 people on that one control since launch, most of them on\n *  phones. A reference is also the studio's biggest conversion lever, so the\n *  picture that fails is the one that mattered most.\n *\n *  So every picture goes through the canvas first: orientation baked in from the\n *  EXIF (a sideways reference is a sideways render), metadata dropped, and the\n *  long edge capped at 2048 px. That alone brings a 12 MB phone JPEG to about a\n *  megabyte. What it does NOT do is shrink a PNG much — a PNG photo stays\n *  lossless, and the failure that was reproduced was a 7.2 MB PNG — so anything\n *  still over the budget is re-encoded as JPEG, stepping the edge down until it\n *  fits.\n *\n *  Written here rather than borrowed from the site's own EXIF helper because the\n *  studio is meant to lift out of this app with one seam (host.ts), and a\n *  picture shrinker is not host business.\n */\n\n/** Under the 4.5 MB body limit with room for the multipart framing and the\n *  description that travels beside the file. */\nexport const UPLOAD_BUDGET_BYTES = 3.5 * 1024 * 1024;\n\nconst FIRST_EDGE = 2048;\n/** Tried in order when a picture is still over budget after the first pass. */\nconst FALLBACK_EDGES = [2048, 1600, 1280, 1024];\nconst JPEG_QUALITY = 0.88;\n\n/** HEIC/HEIF brands, read from the container rather than trusted from the name:\n *  an iPhone photo renamed to .jpg keeps its HEIC payload. */\nconst HEIC_BRANDS = new Set([\n\t'heic',\n\t'heix',\n\t'heim',\n\t'heis',\n\t'hevc',\n\t'hevx',\n\t'hevm',\n\t'hevs',\n\t'mif1',\n\t'msf1'\n]);\n\nasync function isHeic(file: File): Promise<boolean> {\n\tif (/\\.(heic|heif)$/i.test(file.name) || /heic|heif/i.test(file.type)) return true;\n\ttry {\n\t\tconst b = new Uint8Array(await file.slice(0, 12).arrayBuffer());\n\t\tif (b.length < 12) return false;\n\t\tif (String.fromCharCode(b[4], b[5], b[6], b[7]) !== 'ftyp') return false;\n\t\treturn HEIC_BRANDS.has(String.fromCharCode(b[8], b[9], b[10], b[11]));\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n/** HEIC to a JPEG blob. Only Safari decodes HEIC natively, so everywhere else\n *  this is the difference between a picture and nothing. Imported on demand:\n *  the decoder is a large download that almost no session needs. */\nasync function heicToJpegBlob(file: File): Promise<Blob | null> {\n\ttry {\n\t\tconst mod = (await import('heic2any')) as unknown as {\n\t\t\tdefault: (o: { blob: Blob; toType: string; quality: number }) => Promise<Blob | Blob[]>;\n\t\t};\n\t\tconst out = await mod.default({ blob: file, toType: 'image/jpeg', quality: 0.92 });\n\t\treturn Array.isArray(out) ? (out[0] ?? null) : out;\n\t} catch {\n\t\treturn null;\n\t}\n}\n\ntype Drawable = ImageBitmap | HTMLImageElement;\n\nasync function decode(blob: Blob): Promise<Drawable | null> {\n\tif (typeof createImageBitmap === 'function') {\n\t\ttry {\n\t\t\treturn await createImageBitmap(blob, { imageOrientation: 'from-image' });\n\t\t} catch {\n\t\t\t/* fall through to an <img>, which every browser can decode */\n\t\t}\n\t}\n\t// onload rather than img.decode(): decode() is deferred in a backgrounded\n\t// document, and someone who switches apps while a photo attaches is exactly\n\t// who this path is for.\n\tconst url = URL.createObjectURL(blob);\n\ttry {\n\t\treturn await new Promise<HTMLImageElement | null>((resolve) => {\n\t\t\tconst img = new Image();\n\t\t\timg.onload = () => resolve(img);\n\t\t\timg.onerror = () => resolve(null);\n\t\t\timg.src = url;\n\t\t});\n\t} finally {\n\t\tURL.revokeObjectURL(url);\n\t}\n}\n\nfunction sizeOf(d: Drawable): { w: number; h: number } {\n\treturn 'naturalWidth' in d\n\t\t? { w: d.naturalWidth, h: d.naturalHeight }\n\t\t: { w: d.width, h: d.height };\n}\n\nasync function encode(\n\td: Drawable,\n\tmaxEdge: number,\n\ttype: 'image/jpeg' | 'image/png',\n\tquality: number\n): Promise<Blob | null> {\n\tconst { w, h } = sizeOf(d);\n\tif (!w || !h) return null;\n\tconst scale = Math.min(1, maxEdge / Math.max(w, h));\n\tconst width = Math.max(1, Math.round(w * scale));\n\tconst height = Math.max(1, Math.round(h * scale));\n\t// OffscreenCanvas where it exists (Safari from 16.4), the DOM canvas where it\n\t// does not — older iPhones are exactly the people with the biggest photos.\n\tif (typeof OffscreenCanvas !== 'undefined') {\n\t\ttry {\n\t\t\tconst c = new OffscreenCanvas(width, height);\n\t\t\tconst ctx = c.getContext('2d');\n\t\t\tif (ctx) {\n\t\t\t\tctx.drawImage(d, 0, 0, width, height);\n\t\t\t\treturn await c.convertToBlob({ type, quality });\n\t\t\t}\n\t\t} catch {\n\t\t\t/* try the DOM canvas */\n\t\t}\n\t}\n\ttry {\n\t\tconst c = document.createElement('canvas');\n\t\tc.width = width;\n\t\tc.height = height;\n\t\tconst ctx = c.getContext('2d');\n\t\tif (!ctx) return null;\n\t\tctx.drawImage(d, 0, 0, width, height);\n\t\treturn await new Promise<Blob | null>((res) => c.toBlob(res, type, quality));\n\t} catch {\n\t\treturn null;\n\t}\n}\n\nfunction renamed(name: string, ext: 'jpg' | 'png'): string {\n\treturn `${name.replace(/\\.[^.]+$/, '') || 'photo'}.${ext}`;\n}\n\n/** A picture that fits the upload budget, or the best attempt at one.\n *\n *  Never throws: a picture that cannot be decoded comes back as it went in, and\n *  the caller's size check turns that into a sentence instead of a request that\n *  is going to fail. The returned name carries the type, because the server\n *  decides the stored content type from the extension. */\nexport async function shrinkForUpload(file: File): Promise<File> {\n\tlet source: Blob = file;\n\tif (await isHeic(file)) {\n\t\tconst jpeg = await heicToJpegBlob(file);\n\t\tif (jpeg) source = jpeg;\n\t}\n\n\tconst pic = await decode(source);\n\tif (!pic) return file;\n\ttry {\n\t\t// Keep a PNG a PNG on the first pass — transparency survives, and a small\n\t\t// PNG was never the problem. Everything else becomes a JPEG.\n\t\tconst keepPng = file.type === 'image/png' && source === file;\n\t\tconst firstType = keepPng ? 'image/png' : 'image/jpeg';\n\t\tconst first = await encode(pic, FIRST_EDGE, firstType, JPEG_QUALITY);\n\t\tif (first && first.size <= UPLOAD_BUDGET_BYTES) {\n\t\t\treturn new File([first], renamed(file.name, keepPng ? 'png' : 'jpg'), {\n\t\t\t\ttype: firstType,\n\t\t\t\tlastModified: Date.now()\n\t\t\t});\n\t\t}\n\t\tfor (const edge of FALLBACK_EDGES) {\n\t\t\tconst jpeg = await encode(pic, edge, 'image/jpeg', JPEG_QUALITY);\n\t\t\tif (jpeg && jpeg.size <= UPLOAD_BUDGET_BYTES) {\n\t\t\t\treturn new File([jpeg], renamed(file.name, 'jpg'), {\n\t\t\t\t\ttype: 'image/jpeg',\n\t\t\t\t\tlastModified: Date.now()\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\treturn file;\n\t} finally {\n\t\tif ('close' in pic) pic.close();\n\t}\n}\n\n/** Whether a picked file is something this path can carry at all. Videos are\n *  not: the image bucket accepts no video type, and a video is also the file\n *  most likely to be over the request limit, so it is refused here in words\n *  rather than sent to fail. */\nexport function isPicture(file: File): boolean {\n\tif (file.type.startsWith('image/')) return true;\n\treturn /\\.(jpe?g|png|webp|heic|heif)$/i.test(file.name);\n}\n"],"version":3}