From 790c2837ddcb443c0f1ea6adcdcb101dfe159d01 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 9 Sep 2021 09:31:50 +0200 Subject: Fix silent 500 after resumable upload --- server/controllers/api/videos/upload.ts | 5 +---- server/helpers/upload.ts | 9 +-------- server/tests/api/videos/resumable-upload.ts | 1 + shared/extra-utils/requests/requests.ts | 8 +++++++- shared/extra-utils/shared/abstract-command.ts | 16 ++++++++++++++-- shared/extra-utils/videos/videos-command.ts | 15 +++++++++++++++ 6 files changed, 39 insertions(+), 15 deletions(-) diff --git a/server/controllers/api/videos/upload.ts b/server/controllers/api/videos/upload.ts index 7e87df8b1..55cb9cf20 100644 --- a/server/controllers/api/videos/upload.ts +++ b/server/controllers/api/videos/upload.ts @@ -2,7 +2,7 @@ import express from 'express' import { move } from 'fs-extra' import { basename } from 'path' import { getLowercaseExtension } from '@server/helpers/core-utils' -import { deleteResumableUploadMetaFile, getResumableUploadPath } from '@server/helpers/upload' +import { getResumableUploadPath } from '@server/helpers/upload' import { uuidToShort } from '@server/helpers/uuid' import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' @@ -130,9 +130,6 @@ export async function addVideoResumable (_req: express.Request, res: express.Res const videoInfo = videoPhysicalFile.metadata const files = { previewfile: videoInfo.previewfile } - // Don't need the meta file anymore - await deleteResumableUploadMetaFile(videoPhysicalFile.path) - return addVideo({ res, videoPhysicalFile, videoInfo, files }) } diff --git a/server/helpers/upload.ts b/server/helpers/upload.ts index 030a6b7d5..3cb17edd0 100644 --- a/server/helpers/upload.ts +++ b/server/helpers/upload.ts @@ -1,5 +1,3 @@ -import { METAFILE_EXTNAME } from '@uploadx/core' -import { remove } from 'fs-extra' import { join } from 'path' import { RESUMABLE_UPLOAD_DIRECTORY } from '../initializers/constants' @@ -9,13 +7,8 @@ function getResumableUploadPath (filename?: string) { return RESUMABLE_UPLOAD_DIRECTORY } -function deleteResumableUploadMetaFile (filepath: string) { - return remove(filepath + METAFILE_EXTNAME) -} - // --------------------------------------------------------------------------- export { - getResumableUploadPath, - deleteResumableUploadMetaFile + getResumableUploadPath } diff --git a/server/tests/api/videos/resumable-upload.ts b/server/tests/api/videos/resumable-upload.ts index 857859fd3..59970aa94 100644 --- a/server/tests/api/videos/resumable-upload.ts +++ b/server/tests/api/videos/resumable-upload.ts @@ -113,6 +113,7 @@ describe('Test resumable upload', function () { it('Should correctly delete files after an upload', async function () { const uploadId = await prepareUpload() await sendChunks({ pathUploadId: uploadId }) + await server.videos.endResumableUpload({ pathUploadId: uploadId }) expect(await countResumableUploads()).to.equal(0) }) diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 5bbf7f3bf..501e0b374 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -57,9 +57,15 @@ function makeActivityPubGetRequest (url: string, path: string, expectedStatus = }) } -function makeDeleteRequest (options: CommonRequestParams) { +function makeDeleteRequest (options: CommonRequestParams & { + query?: any + rawQuery?: string +}) { const req = request(options.url).delete(options.path) + if (options.query) req.query(options.query) + if (options.rawQuery) req.query(options.rawQuery) + return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) } diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index 021045e49..a57c857fc 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts @@ -40,6 +40,11 @@ interface InternalGetCommandOptions extends InternalCommonCommandOptions { query?: { [ id: string ]: any } } +interface InternalDeleteCommandOptions extends InternalCommonCommandOptions { + query?: { [ id: string ]: any } + rawQuery?: string +} + abstract class AbstractCommand { constructor ( @@ -82,8 +87,15 @@ abstract class AbstractCommand { }) } - protected deleteRequest (options: InternalCommonCommandOptions) { - return makeDeleteRequest(this.buildCommonRequestOptions(options)) + protected deleteRequest (options: InternalDeleteCommandOptions) { + const { query, rawQuery } = options + + return makeDeleteRequest({ + ...this.buildCommonRequestOptions(options), + + query, + rawQuery + }) } protected putBodyRequest (options: InternalCommonCommandOptions & { diff --git a/shared/extra-utils/videos/videos-command.ts b/shared/extra-utils/videos/videos-command.ts index d35339c8d..63980c147 100644 --- a/shared/extra-utils/videos/videos-command.ts +++ b/shared/extra-utils/videos/videos-command.ts @@ -449,6 +449,8 @@ export class VideosCommand extends AbstractCommand { const result = await this.sendResumableChunks({ ...options, pathUploadId, videoFilePath, size }) + await this.endResumableUpload({ ...options, pathUploadId }) + return result.body?.video || result.body as any } @@ -542,6 +544,19 @@ export class VideosCommand extends AbstractCommand { }) } + endResumableUpload (options: OverrideCommandOptions & { + pathUploadId: string + }) { + return this.deleteRequest({ + ...options, + + path: '/api/v1/videos/upload-resumable', + rawQuery: options.pathUploadId, + implicitToken: true, + defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 + }) + } + quickUpload (options: OverrideCommandOptions & { name: string nsfw?: boolean -- cgit v1.2.3