Skip to Content
Senri

Upload limit

Check the size, number and type of files with uploadLimit(). safeFileName()

import { , ,  } from "@senrijs/senri";
declare function (: string, : ): <void>;

const  = ();

.("/photos", ({
    : 20 * 1024 * 1024,
    : 5 * 1024 * 1024,
    : 3,
    : ["image/png", "image/jpeg"],
}));

.("/photos", async (, ) => {
    const {  } = await .<{ :  }>();
    await ((.), );
    return { : . };
});

It checks the files of a multipart/form-data body before passing it to the handler.

  • It checks the size (maxTotalSize is required, maxFileSize), the number (maxFiles) and the type (types, extensions)
  • Too large is 413, and the wrong type is 415
  • For PNG, JPEG, GIF, WebP and PDF, the first bytes of the content are checked too
  • Read the result with c.body(). A route's schemas run after the check
  • maxTotalSize applies instead of the app's default limit (1 MiB)

Memory

c.body() reads everything into memory, so it uses up to maxTotalSize of memory. To stream large files, use multipart().

File names

The file name that was sent (file.name) might be something like ../../etc/passwd. Do not use it as a storage path as is: choose the name yourself with crypto.randomUUID() and so on, or pass it through safeFileName().

Options

Prop

Type

On this page