Skip to content

Commit

Permalink
@tus/server: Metadata on namingFunction + propagate error (#558)
Browse files Browse the repository at this point in the history
* fix: propagate error

* feat: add metadata to namingFunction
  • Loading branch information
fenos authored Jan 17, 2024
1 parent 367fdec commit a49bc4b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ By default, it expects everything in the path after the last `/` to be the uploa
#### `options.namingFunction`

Control how you want to name files (`(req) => string`)
Control how you want to name files (`(req, metadata) => string`)

In `@tus/server`, the upload ID in the URL is the same as the file name.
This means using a custom `namingFunction` will return a different `Location` header for uploading
Expand Down
22 changes: 11 additions & 11 deletions packages/server/src/handlers/PostHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,21 @@ export class PostHandler extends BaseHandler {
throw ERRORS.INVALID_LENGTH
}

let metadata
if ('upload-metadata' in req.headers) {
try {
metadata = Metadata.parse(upload_metadata)
} catch {
throw ERRORS.INVALID_METADATA
}
}

let id
try {
id = this.options.namingFunction(req)
id = this.options.namingFunction(req, metadata)
} catch (error) {
log('create: check your `namingFunction`. Error', error)
throw ERRORS.FILE_WRITE_ERROR
throw error
}

const maxFileSize = await this.getConfiguredMaxSize(req, id)
Expand All @@ -72,15 +81,6 @@ export class PostHandler extends BaseHandler {
throw ERRORS.ERR_MAX_SIZE_EXCEEDED
}

let metadata
if ('upload-metadata' in req.headers) {
try {
metadata = Metadata.parse(upload_metadata)
} catch {
throw ERRORS.INVALID_METADATA
}
}

if (this.options.onIncomingRequest) {
await this.options.onIncomingRequest(req, res, id)
}
Expand Down
5 changes: 4 additions & 1 deletion packages/server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export type ServerOptions = {
* Default uses `crypto.randomBytes(16).toString('hex')`.
* @param req - The incoming HTTP request.
*/
namingFunction?: (req: http.IncomingMessage) => string
namingFunction?: (
req: http.IncomingMessage,
metadata?: Record<string, string | null>
) => string

/**
* The Lock interface defines methods for implementing a locking mechanism.
Expand Down
6 changes: 4 additions & 2 deletions packages/server/test/PostHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ describe('PostHandler', () => {
const handler = new PostHandler(fake_store, {
path: '/test',
locker: new MemoryLocker(),
namingFunction: sinon.stub().throws(),
namingFunction: () => {
throw {status_code: 400}
},
})

req.headers = {'upload-length': '1000'}
return assert.rejects(() => handler.send(req, res, context), {status_code: 500})
return assert.rejects(() => handler.send(req, res, context), {status_code: 400})
})

it('should call custom namingFunction', async () => {
Expand Down

0 comments on commit a49bc4b

Please sign in to comment.