Skip to content

feat(storage): support custom upload content-type detection - #13

Merged
kamilmysliwiec merged 5 commits into
nestjs:masterfrom
rafaself:feat/custom-upload-content-type-detection
Oct 8, 2026
Merged

kamilmysliwiec merged 5 commits into
nestjs:masterfrom
rafaself:feat/custom-upload-content-type-detection

Conversation

@rafaself

@rafaself rafaself commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

  • The commit message follows the contribution guidelines.
  • Tests for the changes have been added.
  • Docs have been added / updated.

PR Type

  • Feature

What is the current behavior?

When uploadToDisk() has a contentTypes allow-list, files without a recognized magic signature
are rejected. This prevents applications from allowing formats such as CSV.

Issue Number: #12

Scope

Addresses #12 by adding an optional async detectContentType(bytes, file) fallback for unknown
signatures. The detector receives a configurable bounded prefix (4 KiB by default, up to 64 KiB).
Its result is checked against contentTypes before any bytes are written and supplies the stored
content type. CSV detection also gives default upload keys a .csv extension.

The API docs state that prefix detection does not validate the full file and that client-provided
MIME types and filenames are untrusted.

What is the new behavior?

Built-in signature detection remains the default and takes precedence. Applications can opt in to
classifying formats without signatures; an undefined result continues to be rejected by an
allow-list.

Does this PR introduce a breaking change?

  • No

Validation

  • npm run test:e2e: 887 passed, 17 skipped because MinIO is unavailable.
  • npm run build
  • npm run lint

Closes #12

Let applications classify bounded prefixes for formats without magic signatures,
preserving built-in signature detection as the default behavior.

Closes nestjs#12
Allow custom detectors to use samples shorter than the built-in signature sniff length.

Refs nestjs#12
Reject known signatures outside the allow-list before fallback detection.

Refs nestjs#12
@rafaself rafaself changed the title feat(storage): support custom upload content types feat(storage): support custom upload content-type detection Oct 6, 2026
@rafaself
rafaself marked this pull request as ready for review October 6, 2026 21:47
rafaself and others added 2 commits October 6, 2026 19:21
Document the accepted one-byte lower bound for upload content samples.

Refs nestjs#12
Match `contentTypes` and derive the key extension from the type's
essence, so a detector can return `text/csv; charset=utf-8`. Extensions
for detector types now come from the existing extension table instead
of a CSV-only entry. Reject empty or non-string detector results with
a clear error, read and copy the sample once, and document that the
detector must settle promptly and that the exported
`detectContentType()` is a no-op as the option.
@kamilmysliwiec
kamilmysliwiec merged commit 2a17f3b into nestjs:master Oct 8, 2026
@kamilmysliwiec kamilmysliwiec added the enhancement New feature or request label Oct 8, 2026
@rafaself
rafaself deleted the feat/custom-upload-content-type-detection branch October 8, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support custom content-type detection for uploads without magic signatures

2 participants