You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To begin with, you must choose a Key-Value Stores.
You can see which store you need here
For this cookbook, we will use @tus/file-store
Install
npm install @tus/server @tus/file-store
TUS server
Create a file to manage the TUS server
// tus-server.tsimport{FileStore}from'@tus/file-store';import{Server}from'@tus/server';import{tmpdir}from'node:os';import{join}from'node:path';exportconstDATA_STORE=newFileStore({directory: join(tmpdir(),'upload'),// Where the files will be storedexpirationPeriodInMilliseconds: 60*60*1000,// 60 minutes});exportconstTUS_UPLOAD_PATH='/public/v1/upload';exportconstTUS_UPLOAD_PATHS=[TUS_VOD_PATH,`${TUS_VOD_PATH}/:uploadId`];exportconstTUS_UPLOAD_SERVER=newServer({path: TUS_VOD_PATH,allowedOrigins: ['*'],// Authorize everything, we'll let Adonis CORS do the workdatastore: DATA_STORE,respectForwardedHeaders: true,// Only if you have a reverse proxyasynconIncomingRequest(){awaitDATA_STORE.deleteExpired();},onUploadFinish: async(req,upload)=>{// Do something here when the file download is complete.},});
Controller
We can create our UploadController.
// controllers/upload_controller.tsimporttype{HttpContext}from'@adonisjs/core/http';import{TUS_UPLOAD_SERVER}from'#app/tus-server.js'exportdefaultclassUploadController{asynchandle({ request, response}: HttpContext){// You can perform any bouncer checks here before submitting the request to TUS.returnTUS_UPLOAD_SERVER.handle(request.request,response.response);}}
Router
Define the upload routes
// start/routes.tsconstUploadController=()=>import('#controllers/upload_controller.js')router.group(()=>{router.any('',[UploadController,'handle']);router.any(':uploadId',[UploadController,'handle']);}).prefix('/publiv/v1/upload');// ⚠️ Must be the same path defined in the TUS_UPLOAD_PATH constant.
Bodyparser
Adonis will try to read the body, and we don't want that, otherwise TUS won't be able to do its job.
// config/bodyparser.tsimport{TUS_UPLOAD_PATHS}from'#app/tus-server.js'//... rest of the configmultipart: {autoProcess: true,convertEmptyStringsToNull: true,processManually: [// let @tus/server process the body
...TUS_UPLOAD_PATHS,],types: ['multipart/form-data'],},
CORS
To enable resumable downloads, we need to update the CORS configuration and expose certain headers.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
To begin with, you must choose a Key-Value Stores.
You can see which store you need here
For this cookbook, we will use
@tus/file-storeInstall
TUS server
Create a file to manage the TUS server
Controller
We can create our UploadController.
Router
Define the upload routes
Bodyparser
Adonis will try to read the body, and we don't want that, otherwise TUS won't be able to do its job.
CORS
To enable resumable downloads, we need to update the CORS configuration and expose certain headers.
🎉 Happy file uploading!
Beta Was this translation helpful? Give feedback.
All reactions