import { closeSync, constants as fsConstants, existsSync, fstatSync, lstatSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import sharp, { type Metadata } from 'sharp'; import { DATABASE_FILE } from './db/index.ts'; export const MAX_IMAGE_BYTES = 10 * 1024 * 1024; export const MAX_IMAGE_PIXELS = 64_000_000; export const DISPLAY_EDGE = 1600; /** Subdirectory of `uploads/` that holds the files writers imported. */ export const IMPORTS_DIRECTORY = 'imports'; export type ImageFacts = { imageId: string; mime: string; bytes: number; width: number; height: number; }; export class ImageRefusedError extends Error { constructor(message: string) { super(message); this.name = 'ImageRefusedError'; } } const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; const SUPPORTED_FORMATS = new Set(['png', 'jpeg', 'webp', 'avif', 'gif']); /** `O_NOFOLLOW` is absent on Windows, where a link needs a privilege to make one. */ const NO_FOLLOW = fsConstants.O_NOFOLLOW ?? 0; let testUploadsDir: string | null = null; export function setUploadsDirForTest(dir: string | null): void { testUploadsDir = dir; } export function uploadsDir(): string { return testUploadsDir ?? join(dirname(DATABASE_FILE), 'uploads'); } function detectMime(bytes: Uint8Array): string { if ( bytes.length >= 8 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47 ) { return 'image/png'; } if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) { return 'image/jpeg'; } if ( bytes.length >= 12 && bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 && bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50 ) { return 'image/webp'; } if ( bytes.length >= 6 && bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38 ) { return 'image/gif'; } if ( bytes.length >= 12 && bytes[4] === 0x66 && bytes[5] === 0x74 && bytes[6] === 0x79 && bytes[7] === 0x70 && bytes[8] === 0x61 && bytes[9] === 0x76 && bytes[10] === 0x69 && (bytes[11] === 0x66 || bytes[11] === 0x73) ) { return 'image/avif'; } return 'application/octet-stream'; } /** * Writes the display copy (`.webp`), downscaled to fit DISPLAY_EDGE with EXIF and * GPS stripped, and `.orig` with the exact bytes when `preserveOriginal` is set. * The media type comes from the bytes, never from a caller or a stored row, because * that type is later served to a browser. Each write stages to a `.tmp` file and * renames it into place, so a reader never sees half a file. */ export async function storeImage( bytes: Uint8Array, options: { preserveOriginal: boolean } ): Promise { if (bytes.length > MAX_IMAGE_BYTES) { const mb = Math.round(bytes.length / 1024 / 1024); throw new ImageRefusedError(`That image is ${mb} MB. The limit is 10 MB.`); } let meta: Metadata; try { meta = await sharp(bytes, { limitInputPixels: MAX_IMAGE_PIXELS }).metadata(); } catch { throw new ImageRefusedError( 'That file is not a supported image. Supported formats are PNG, JPEG, WebP, AVIF, and GIF.' ); } const format = meta.format?.toLowerCase(); if (!format || !SUPPORTED_FORMATS.has(format)) { throw new ImageRefusedError( 'That file is not a supported image format. Supported formats are PNG, JPEG, WebP, AVIF, and GIF.' ); } const mime = `image/${format === 'jpeg' ? 'jpeg' : format}`; // `rotate()` applies the EXIF orientation, then the WebP re-encode drops the // remaining metadata. let displayBuffer: Buffer; let width: number; let height: number; try { const rendered = await sharp(bytes, { limitInputPixels: MAX_IMAGE_PIXELS }) .rotate() .resize({ width: DISPLAY_EDGE, height: DISPLAY_EDGE, fit: 'inside', withoutEnlargement: true }) .webp({ quality: 82 }) .toBuffer({ resolveWithObject: true }); displayBuffer = rendered.data; width = rendered.info.width; height = rendered.info.height; } catch { throw new ImageRefusedError('That image could not be processed.'); } const imageId = crypto.randomUUID(); const dir = uploadsDir(); mkdirSync(dir, { recursive: true }); const displayTmp = join(dir, `.${imageId}.webp.tmp`); const displayTarget = join(dir, `${imageId}.webp`); const origTmp = join(dir, `.${imageId}.orig.tmp`); const origTarget = join(dir, `${imageId}.orig`); try { writeFileSync(displayTmp, displayBuffer); if (options.preserveOriginal) { writeFileSync(origTmp, bytes); } renameSync(displayTmp, displayTarget); if (options.preserveOriginal) { renameSync(origTmp, origTarget); } } catch (error) { for (const path of [displayTmp, origTmp, displayTarget, origTarget]) { try { if (existsSync(path)) unlinkSync(path); } catch { // A file that is already gone needs no cleanup. The original // error is the one to report. } } throw error; } return { imageId, mime, bytes: options.preserveOriginal ? bytes.byteLength : 0, width, height }; } /** * The bytes of a regular file, or null for anything else. The app writes only * regular files into `uploads/`, so a link there came from outside it, and * `readFileSync` on a name would follow that link and hand out whatever it points * at. `O_NOFOLLOW` makes the check hold through the read, not only before it. */ function readRegularFile(path: string): Buffer | null { let fd: number; try { fd = openSync(path, fsConstants.O_RDONLY | NO_FOLLOW); } catch { return null; } try { if (!fstatSync(fd).isFile()) return null; return readFileSync(fd); } catch { return null; } finally { closeSync(fd); } } /** * The id is checked against a strict UUID regex, because it becomes a filename: a * loose check allows path traversal. */ export function readImage( imageId: string, variant: 'display' | 'original' ): { bytes: Uint8Array; mime: string } | null { if (!UUID_REGEX.test(imageId)) return null; const dir = uploadsDir(); const filename = variant === 'display' ? `${imageId}.webp` : `${imageId}.orig`; const bytes = readRegularFile(join(dir, filename)); if (!bytes) return null; const mime = variant === 'display' ? 'image/webp' : detectMime(bytes); return { bytes, mime }; } export function removeImage(imageId: string): void { if (!UUID_REGEX.test(imageId)) return; const dir = uploadsDir(); for (const filename of [ `${imageId}.webp`, `${imageId}.orig`, `.${imageId}.webp.tmp`, `.${imageId}.orig.tmp` ]) { const filePath = join(dir, filename); try { if (existsSync(filePath)) unlinkSync(filePath); } catch { // A file that is already gone is the outcome this wants. } } } /** The name a kept import source wears: its id, then the extension it had. */ const SOURCE_FILE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.(zip|json|docx|md|markdown|txt)$/; const SOURCE_EXTENSIONS = new Set(['zip', 'json', 'docx', 'md', 'markdown', 'txt']); function importsDir(): string { return join(uploadsDir(), IMPORTS_DIRECTORY); } /** * Keeps the file a writer imported, so the report can hand it back later. The id is * returned, not taken, because the id and the extension together name the file. The * write stages to a `.tmp` file and renames it into place. */ export function storeImportSource(bytes: Uint8Array, extension: string): string { if (!SOURCE_EXTENSIONS.has(extension)) { throw new Error(`Unsupported import file type: ${extension}.`); } const sourceId = `${crypto.randomUUID()}.${extension}`; const dir = importsDir(); mkdirSync(dir, { recursive: true }); const tmp = join(dir, `.${sourceId}.tmp`); const target = join(dir, sourceId); try { writeFileSync(tmp, bytes); renameSync(tmp, target); } catch (error) { for (const path of [tmp, target]) { try { if (existsSync(path)) unlinkSync(path); } catch { // A file that is already gone needs no cleanup. The original error // is the one to report. } } throw error; } return sourceId; } /** * The stored file's path and size, for a caller that streams it. The id is checked * against the same pattern that lets it become a filename; a loose check allows * traversal out of the folder. `lstat`, not `stat`: a link to another file is not * a file this app stored, and the upload routes only ever write regular files. */ export function openImportSource(sourceId: string): { path: string; size: number } | null { if (!SOURCE_FILE.test(sourceId)) return null; const filePath = join(importsDir(), sourceId); try { const stats = lstatSync(filePath); return stats.isFile() ? { path: filePath, size: stats.size } : null; } catch { return null; } } export function removeImportSource(sourceId: string): void { if (!SOURCE_FILE.test(sourceId)) return; for (const path of [join(importsDir(), sourceId), join(importsDir(), `.${sourceId}.tmp`)]) { try { if (existsSync(path)) unlinkSync(path); } catch { // A file that is already gone is the outcome this wants. } } }