Something went wrong. Try again.
A self-hosted web platform for applications & services with design based on Google's Material 3 Expressive. (Work In Progress)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186import * as nodePath from "node:path";import sharp from "sharp";import type { Instance } from "../index.ts";import System from "../system.ts";
export default class ImageSystem extends System { _internalImages: Map< string, { [resolution: string]: { userId: number; path: string; validUntil: number; public?: boolean; resize?: { dimensions: { width: number; height: number }; changeFormatTo?: "avif" | "jpeg" | "png" | "webp"; position?: "top" | "right top" | "right" | "right bottom" | "bottom" | "left bottom" | "left" | "left top" | "centre"; fit?: "cover" | "contain" | "fill"; background?: string; }; }; } >; _internalImagePaths: Map<string, string>;
constructor(instance: Instance) { super("image", instance);
this._internalImages = new Map(); this._internalImagePaths = new Map(); }
// returns an endpoint where the image located at the provided path can be loaded from on the client // defaults to 3hrs validity
async serveImage( userId: number, path: string, options?: { isPublic?: boolean; evadeCache?: boolean; validMs?: number; } & ( | { crop?: { x: number; y: number; width: number; height: number; }; } | { resize?: { dimensions: | { width: number; height: number } | ((originalSize: { width: number; height: number }) => { width: number; height: number; }); changeFormatTo?: "avif" | "jpeg" | "png" | "webp"; position?: "top" | "right top" | "right" | "right bottom" | "bottom" | "left bottom" | "left" | "left top" | "centre"; fit?: "cover" | "contain" | "fill"; background?: string; }; } ), ): Promise<string> { const opts = { isPublic: options?.isPublic ?? false, evadeCache: options?.evadeCache ?? false, validMs: options?.validMs ?? 21600000, crop: options && "crop" in options ? options.crop : undefined, resize: options && "resize" in options ? options.resize : undefined, };
if (opts.resize?.dimensions) { if (typeof opts.resize.dimensions === "function") { const sharpInstance = sharp(path); const metadata = await sharpInstance.metadata(); opts.resize.dimensions = opts.resize.dimensions({ width: metadata.width ?? 0, height: metadata.height ?? 0, }); } }
let cropKey = ""; if (opts.crop) { cropKey = `crop_${opts.crop.x}_${opts.crop.y}_${opts.crop.width}_${opts.crop.height}`; }
let requestResolution = "raw"; if (opts.resize?.dimensions && typeof opts.resize.dimensions !== "function") { requestResolution = `${opts.resize.dimensions.width}x${opts.resize.dimensions.height}`; } if (cropKey) { requestResolution += `_${cropKey}`; }
if (!opts.evadeCache) { const existingImageId = this._internalImagePaths.get(path); if (existingImageId) { const existingImage = this._internalImages.get(existingImageId); if (existingImage) { const existingImageOfResolution = existingImage[requestResolution]; if (existingImageOfResolution) { existingImageOfResolution.validUntil = Date.now() + opts.validMs; return `${this.instance.sys.configuration.proxy.secure ? "https://" : "http://"}${this.instance.sys.configuration.proxy.hostname}/api/asset/image/${existingImageId}/${requestResolution}`; } } } }
const imageId = crypto.randomUUID();
this._internalImages.set(imageId, { [requestResolution]: { path: path, userId: userId, validUntil: Date.now() + opts.validMs, public: opts.isPublic, resize: { dimensions: opts.resize?.dimensions as { width: number; height: number; }, changeFormatTo: opts.resize?.changeFormatTo ?? "jpeg", position: opts.resize?.position ?? "top", fit: opts.resize?.fit ?? "cover", background: opts.resize?.background ?? "white", }, }, });
this._internalImagePaths.set(path, imageId);
this.log.info(`Serving image at '${nodePath.relative(this.instance.sys.filesystem.FS_ROOT, path)}' as '${imageId}'`);
return `${this.instance.sys.configuration.proxy.secure ? "https://" : "http://"}${this.instance.sys.configuration.proxy.hostname}/api/asset/image/${imageId}/${requestResolution}`; }
async resizeImage( inputPath: string, outputPath: string, dimensions: | { width: number; height: number } | ((originalSize: { width: number; height: number }) => { width: number; height: number; }), options?: { changeFormatTo?: "avif" | "jpeg" | "png" | "webp"; position?: "top" | "right top" | "right" | "right bottom" | "bottom" | "left bottom" | "left" | "left top" | "centre"; fit?: "cover" | "contain" | "fill"; background?: string; }, ): Promise<boolean> { const sharpInstance = sharp(inputPath);
const originalDimensions = await sharpInstance.metadata();
if (typeof dimensions === "function") { dimensions = dimensions(originalDimensions); }
sharpInstance.resize(dimensions.width, dimensions.height, { withoutEnlargement: true, position: options?.position, fit: options?.fit, background: options?.background, kernel: originalDimensions.width / 2 > dimensions.width || originalDimensions.height / 2 > dimensions.height ? "cubic" : "mks2021", });
if (options) { if (options.changeFormatTo) { sharpInstance.toFormat(options.changeFormatTo, { progressive: true }); } }
await sharpInstance.toFile(outputPath);
return true; }}