Server
Fastify
Using Fastify package adds routes for direct uploads.
npm i @uplo/fastifyimport fastify from 'fastify'
import { uploFastify } from '@uplo/fastify'
const app = fastify()
const uplo = {
// Your uplo config
}
app.register(uploFastify, { uplo })It adds POST /uploads/create-direct-upload (change prefix with mountPath).
It returns 201 with the Signed ID and upload data, or 422 with
{ success: false, error: { message } } for invalid requests, unknown
attachments, failed attachment validation or disabled direct uploads.
Authentication
The route has no authentication, so anyone who can reach it can upload files to your bucket. Register the plugin in a scope with your own auth hook:
app.register(async (scope) => {
scope.addHook('onRequest', async (request, reply) => {
const user = await getCurrentUser(request) // your auth
if (!user) {
return reply.status(401).send({ success: false, error: { message: 'Unauthorized' } })
}
})
scope.register(uploFastify, { uplo })
})Uploading alone does not attach a file to a record. Check that the user can
change the record before calling attachSignedFile.