Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Repository files navigation

tusboy

Build Status

Express middleware for tus resumable upload protocol. Uses abstract-tus-store. Name inspired by busboy.

tus-store-compatible

Install

npm install --save tusboy

Requires Node v6+

Usage

See ./test directory for usage examples.

Boilerplate example usage with s3-tus-store:

importtusboyfrom'tusboy'imports3storefrom's3-tus-store'importawsfrom'aws-sdk'import{Passthrough}from'stream'importexpressfrom'express'constbucket='my-bucket'constclient=newaws.S3({accessKeyId: process.env.S3_ACCESS_KEY,secretAccessKey: process.env.S3_SECRET_KEY})conststore=s3store({ client, bucket })constapp=express()app// .use(authenticate).use('/:username/avatar',newexpress.Router({mergeParams: true}).get('/',(req,res,next)=>{constkey=`users/${req.params.username}/avatar-resized`constrs=store.createReadStream(key,({ contentLength, metadata })=>{res.set('Content-Type',metadata.contentType)res.set('Content-Length',contentLength)rs.pipe(res)}).on('error',next)}).use('/upload',tusboy(store,{getKey: (req)=>{// Always return same key... last successful completed upload// wins. Can throw here if authz error.constkey=`users/${req.params.username}/avatar`returnkey},beforeComplete: async(req,upload,uploadId)=>{},afterComplete: async(req,upload,completedUploadId)=>{constkey=`users/${req.params.username}/avatar`console.log(`Completed upload ${completedUploadId}`)// If you return a promise, the last patch request will// block until promise is resolved.// e.g you could resize avatar and write it to .../avatar-resizedconstrs=store.createReadStream(key)// .pipe(resizeImage) actually resize image...constresizedKey=`users/${req.params.username}/avatar-resized`const{ uploadId }=awaitstore.create(resizedKey,{metadata: upload.metadata,uploadLength: upload.uploadLength,})awaitstore.append(uploadId,rs)},})))app.listen(3000)

About

Express middleware for the Tus resumable upload protocol

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages