Checks the images users upload and serves them back, scaled down to the width a request names. The avatar, playlist cover, dump thumbnail and attachment routes call it.
Returns the MIME type of a JPEG, PNG, GIF or WebP image from its magic bytes, or null for anything else. These four are the only image types accepted anywhere. The type the browser declared is never trusted, since the file is later served with the type found here.
Writes data to filePath, replacing whatever is there only once the whole
file is written, so a failed write leaves the old file whole. The upload
routes call it before recording the new file, so that clients told of it
never fetch it early.
Returns how to scale the image whose first IMAGE_HEAD_BYTES are
head, or null when it is to be served as it is: an animated WebP, which
ffmpeg can't read, or anything but a JPEG, PNG, GIF, WebP or BMP.
Sends the image at path, of type mime, with cacheControl: a WebP copy
at most as wide as the request's ?w= names (shared/image.ts), or as
defaultWidth without one, and the image as it is when the request names
no width or the copy can't be made. With ?f=compat, the copy is a JPEG,
or a PNG for an image that may be transparent, which the digest's emails
ask for, since not every mail client reads WebP. Every route that answers
one of the site's own images ends here, so each takes both. It resolves
false, having sent nothing, when the file is missing.
Serves an uploaded image inline as mime, or scaled down as
serveImage does, cached for cacheMaxAge seconds, and only by the
browser when isPrivate. It answers 404 and resolves false when the file
is missing. The caller checks first that the viewer may see it.
Checks an uploaded image's size and content, and returns its MIME type, which the route stores to serve the image with.
Returned by scalableImage, for scaleImage.
-
compat: "jpg" | "png"
The format of its copy for a reader without WebP: a JPEG, or a PNG for an image that may be transparent, which a JPEG would fill with black.
Usage
import * as mod from "api/lib/upload.ts";