From f42fcb4b58f146c2e5ace236548a99d361ade55f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 21 Jul 2023 17:46:37 +0200 Subject: Implement video file replacement in client --- shared/server-commands/videos/videos-command.ts | 81 +++++++++++++------------ 1 file changed, 42 insertions(+), 39 deletions(-) (limited to 'shared/server-commands/videos') diff --git a/shared/server-commands/videos/videos-command.ts b/shared/server-commands/videos/videos-command.ts index 6c38fa7ef..3fdbc348a 100644 --- a/shared/server-commands/videos/videos-command.ts +++ b/shared/server-commands/videos/videos-command.ts @@ -462,7 +462,7 @@ export class VideosCommand extends AbstractCommand { path: string attributes: { fixture?: string } & { [id: string]: any } }): Promise { - const { path, attributes, expectedStatus } = options + const { path, attributes, expectedStatus = HttpStatusCode.OK_200 } = options let size = 0 let videoFilePath: string @@ -597,43 +597,47 @@ export class VideosCommand extends AbstractCommand { const readable = createReadStream(videoFilePath, { highWaterMark: 8 * 1024 }) return new Promise>((resolve, reject) => { readable.on('data', async function onData (chunk) { - readable.pause() - - const headers = { - 'Authorization': 'Bearer ' + token, - 'Content-Type': 'application/octet-stream', - 'Content-Range': contentRangeBuilder - ? contentRangeBuilder(start, chunk) - : `bytes ${start}-${start + chunk.length - 1}/${size}`, - 'Content-Length': contentLength ? contentLength + '' : chunk.length + '' + try { + readable.pause() + + const headers = { + 'Authorization': 'Bearer ' + token, + 'Content-Type': 'application/octet-stream', + 'Content-Range': contentRangeBuilder + ? contentRangeBuilder(start, chunk) + : `bytes ${start}-${start + chunk.length - 1}/${size}`, + 'Content-Length': contentLength ? contentLength + '' : chunk.length + '' + } + + if (digestBuilder) { + Object.assign(headers, { digest: digestBuilder(chunk) }) + } + + const res = await got<{ video: VideoCreateResult }>({ + url, + method: 'put', + headers, + path: path + '?' + pathUploadId, + body: chunk, + responseType: 'json', + throwHttpErrors: false + }) + + start += chunk.length + + if (res.statusCode === expectedStatus) { + return resolve(res) + } + + if (res.statusCode !== HttpStatusCode.PERMANENT_REDIRECT_308) { + readable.off('data', onData) + return reject(new Error('Incorrect transient behaviour sending intermediary chunks')) + } + + readable.resume() + } catch (err) { + reject(err) } - - if (digestBuilder) { - Object.assign(headers, { digest: digestBuilder(chunk) }) - } - - const res = await got<{ video: VideoCreateResult }>({ - url, - method: 'put', - headers, - path: path + '?' + pathUploadId, - body: chunk, - responseType: 'json', - throwHttpErrors: false - }) - - start += chunk.length - - if (res.statusCode === expectedStatus) { - return resolve(res) - } - - if (res.statusCode !== HttpStatusCode.PERMANENT_REDIRECT_308) { - readable.off('data', onData) - return reject(new Error('Incorrect transient behaviour sending intermediary chunks')) - } - - readable.resume() }) }) } @@ -695,8 +699,7 @@ export class VideosCommand extends AbstractCommand { ...options, path: '/api/v1/videos/' + options.videoId + '/source/replace-resumable', - attributes: { fixture: options.fixture }, - expectedStatus: HttpStatusCode.NO_CONTENT_204 + attributes: { fixture: options.fixture } }) } -- cgit v1.2.3