Skip to main content
Sonamu makes file uploads easy to handle with the @upload decorator and UploadedFile class.

File Upload Flow

@upload Decorator

Used when creating file upload APIs.

Single File Upload

Client request:

Multiple File Upload

Client request:

Accessing Files

Access uploaded files through the files property of Sonamu.getContext().
Single file access: Use the first element of the array
Multiple file access: Iterate through the array

Usage Example

UploadedFile Class

A class that wraps uploaded files.

Properties

Original filename

Methods

buffer

Accesses the file data as a Buffer.

md5()

Calculates the MD5 hash of the file.
Use cases:
  • File deduplication check
  • File integrity verification
  • Unique filename generation

saveToDisk(diskName, key)

Saves the file to disk.
Return value: URL of the saved file (unsigned)

Practical Examples

1. Profile Photo Upload

2. Multiple Image Upload

3. File Type Validation

4. Deduplication Check with MD5 Hash

5. Disk Selection

File Retrieval

Use Storage Manager to retrieve saved files.

Reading Files

File Download API

Getting URL

Signed URL (S3)

File Deletion

Cautions

Cautions when uploading files:
  1. File validation is required: Validate type, size, and extension
  2. Use unique filenames: Prevent overwrites
  3. Path validation: Prevent directory traversal
  4. File size limit: Prevent memory overflow
  5. MIME type validation: Don’t trust only the extension

Next Steps

Storage Manager

Managing multiple disks

Storage Drivers

fs, S3 driver configuration

URL Builder

Generating file URLs