diff --git a/packages/server/README.md b/packages/server/README.md index 298b6bf0..2b9ae20f 100644 --- a/packages/server/README.md +++ b/packages/server/README.md @@ -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 diff --git a/packages/server/src/handlers/PostHandler.ts b/packages/server/src/handlers/PostHandler.ts index efea6265..7d51e25e 100644 --- a/packages/server/src/handlers/PostHandler.ts +++ b/packages/server/src/handlers/PostHandler.ts @@ -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) @@ -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) } diff --git a/packages/server/src/types.ts b/packages/server/src/types.ts index 3ca79c4c..652629a8 100644 --- a/packages/server/src/types.ts +++ b/packages/server/src/types.ts @@ -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 /** * The Lock interface defines methods for implementing a locking mechanism. diff --git a/packages/server/test/PostHandler.test.ts b/packages/server/test/PostHandler.test.ts index e7ce3559..0838a51c 100644 --- a/packages/server/test/PostHandler.test.ts +++ b/packages/server/test/PostHandler.test.ts @@ -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 () => {