From b329abc2f00628f8c6814d304df6faad301d216c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Mar 2021 09:58:08 +0100 Subject: Fix request body limit --- server/helpers/requests.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index 2c9da213c..aee8f6673 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts @@ -30,13 +30,25 @@ const peertubeGot = got.extend({ handlers: [ (options, next) => { const promiseOrStream = next(options) as CancelableRequest - const bodyKBLimit = options.context?.bodyKBLimit + const bodyKBLimit = options.context?.bodyKBLimit as number if (!bodyKBLimit) throw new Error('No KB limit for this request') + const bodyLimit = bodyKBLimit * 1000 + /* eslint-disable @typescript-eslint/no-floating-promises */ promiseOrStream.on('downloadProgress', progress => { - if (progress.transferred * 1000 > bodyKBLimit && progress.percent !== 1) { - promiseOrStream.cancel(`Exceeded the download limit of ${bodyKBLimit} bytes`) + if (progress.transferred > bodyLimit && progress.percent !== 1) { + const message = `Exceeded the download limit of ${bodyLimit} B` + logger.warn(message) + + // CancelableRequest + if (promiseOrStream.cancel) { + promiseOrStream.cancel() + return + } + + // Stream + (promiseOrStream as any).destroy() } }) @@ -177,5 +189,5 @@ function buildRequestError (error: any) { error.responseBody = error.response.body } - return newError + return error } -- cgit v1.2.3