Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : ORIX App Universe8 * File : src/lib/uploads.ts9 * Module : lib10 * Repository : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website : https://www.orixlab.dev12 * Created : 2026-11-2113 * Last Modified : 2026-11-2114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { mkdir, writeFile } from "node:fs/promises";44import { join } from "node:path";45import sharp from "sharp";46import { env } from "./env";47import { sha256 } from "./security/crypto";48import { ApiError } from "./security/http";4950const MAX_BYTES = 12 * 1024 * 1024;5152/**53 * Safe image upload: the bytes are decoded and re-encoded by sharp (which drops metadata and anything that is54 * not pixels), stored under a content-hash name inside the data directory, and referenced as "u/<hash>.<ext>".55 * The original file name and the declared content type are never trusted or kept.56 */57export async function storeImage(slug: string, data: Buffer, kind: "icon" | "screenshot"): Promise<{ file: string; width: number; height: number }> {58 if (data.byteLength === 0 || data.byteLength > MAX_BYTES) throw new ApiError(413, "too_large", "Images must be under 12 MB");59 let out: Buffer, info: { width: number; height: number }, ext: string;60 try {61 const img = sharp(data, { limitInputPixels: 60_000_000, failOn: "error" }).rotate();62 const meta = await img.metadata();63 if (!meta.format || !["png", "jpeg", "webp"].includes(meta.format)) throw new Error("format");64 if (kind === "icon") {65 if (!meta.width || meta.width !== meta.height || meta.width < 256) throw new ApiError(422, "bad_icon", "An icon must be square, at least 256 × 256");66 ({ data: out, info } = await img.resize(512, 512).png({ compressionLevel: 9 }).toBuffer({ resolveWithObject: true })); ext = "png";67 } else if (meta.format === "png" && data.byteLength < 900_000) {68 ({ data: out, info } = await img.png({ compressionLevel: 9 }).toBuffer({ resolveWithObject: true })); ext = "png";69 } else {70 ({ data: out, info } = await img.resize({ width: 2880, height: 2880, fit: "inside", withoutEnlargement: true }).jpeg({ quality: 86, mozjpeg: true }).toBuffer({ resolveWithObject: true })); ext = "jpg";71 }72 } catch (e) {73 if (e instanceof ApiError) throw e;74 throw new ApiError(422, "bad_image", "The file is not a valid PNG, JPEG or WebP image");75 }76 const name = `${sha256(out).slice(0, 32)}.${ext}`, dir = join(env.dataDir, "uploads", slug);77 await mkdir(dir, { recursive: true, mode: 0o700 });78 await writeFile(join(dir, name), out, { mode: 0o600 });79 return { file: `u/${name}`, width: info.width, height: info.height };80}81