Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Latest commit

 

History

History
72 lines (48 loc) · 2.45 KB

file-uploads.md

File metadata and controls

72 lines (48 loc) · 2.45 KB

File uploads

Step 1: Use body parser to parse the multipart using fileParser (⚠: files are parsed and stored temporarily in memory)

Step 2: Request now contains a files property - an array of multer parsed files.

Step 3: Handle the files, presumably save the file contents using one of the following uploaders.

File uploaders

File uploader is an express middleware, that looks for request's files property. If files is not an empty array, it uploads all of the files and transforms this property into a list of generated string ids.

⇓ an HTTP "multipart/form-data; boundary=BOUNDARY" request.

BOUNDARY
Content-Disposition: form-data; name="file1"; filename="file1.png"
fb c4 5c 07 d2 86 8b 86 72 db ...
BOUNDARY
Content-Disposition: form-data; name="file2"; filename="file1.png"
c6 96 23 59 44 42 4f 93 c2 8e  ...
BOUNDARY

⇓ a fileParser middleware

request: { files: MulterFile[], ...}

⇓ any fileUploader middleware

request: { files: ['2018068-4wqrvjr', ... ], ...}

⇓ `req.files` is now an array of the file identifiers to the uploader's storage

Filesystem uploader

This Uploader "uploads" (=saves) the file to the local filesystem under a given folder.

Filesystem file name is a pseudo-random pattern of YYYYMMDD-*******.

Initial options (string)

A directory name where to upload the files. Default: current directory

Example

import { uploadFilesystem, fileParser } from 'unicore';

const server = createServer();
server.use('/files', fileParser());
server.use(uploadFileSystem(/* dirname where to upload the files (string) */));
server.use((req, res, next) => {
    res.json(req.files); // ['2018068-4wqrvjr', ... ]
});

GCP Storage Bucket

This Uploader saves the file to the specified Google Cloud Storage.

The file id is a pseudo-random pattern of {prefix}/YYYYMMDD-*******.

Use https://storage.googleapis.com/{bucket}/{fileId} to retrieve the file.

Initial options (object)

Key Type Description
prefix string A path-like prefix to prefix all the uploaded filenames with. Default: none
bucket string The destination bucket name. Required
credentials object Google Cloud Storage lib. credentials object. Default: none
projectId string Your Google Cloud Platform project ID. Default: none
public boolean Whether to upload files with public access allowed. Default: true