diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2021-10-25 17:42:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-25 17:42:20 +0200 |
commit | 276250f0a36e00373166d91d539e5220d6f158c7 (patch) | |
tree | 394e4fd65912edbbe9266ccfbacfc14f433371e7 /server/middlewares/validators/videos | |
parent | b2ad0090c182c7f2a8cba1cced3987d408a4b159 (diff) | |
download | PeerTube-276250f0a36e00373166d91d539e5220d6f158c7.tar.gz PeerTube-276250f0a36e00373166d91d539e5220d6f158c7.tar.zst PeerTube-276250f0a36e00373166d91d539e5220d6f158c7.zip |
prevent multiple post-process triggering of upload-resumable (#4175)
* prevent multiple post-process triggering of upload-resumable
* switch from 409 to 503 for upload being processed
* Improve resumable upload check
Co-authored-by: Chocobozzz <me@florianbigard.com>
Diffstat (limited to 'server/middlewares/validators/videos')
-rw-r--r-- | server/middlewares/validators/videos/videos.ts | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts index 23ee9778a..e486887a7 100644 --- a/server/middlewares/validators/videos/videos.ts +++ b/server/middlewares/validators/videos/videos.ts | |||
@@ -1,6 +1,8 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { body, header, param, query, ValidationChain } from 'express-validator' | 2 | import { body, header, param, query, ValidationChain } from 'express-validator' |
3 | import { isTestInstance } from '@server/helpers/core-utils' | ||
3 | import { getResumableUploadPath } from '@server/helpers/upload' | 4 | import { getResumableUploadPath } from '@server/helpers/upload' |
5 | import { Redis } from '@server/lib/redis' | ||
4 | import { isAbleToUploadVideo } from '@server/lib/user' | 6 | import { isAbleToUploadVideo } from '@server/lib/user' |
5 | import { getServerActor } from '@server/models/application/application' | 7 | import { getServerActor } from '@server/models/application/application' |
6 | import { ExpressPromiseHandler } from '@server/types/express' | 8 | import { ExpressPromiseHandler } from '@server/types/express' |
@@ -105,12 +107,34 @@ const videosAddLegacyValidator = getCommonVideoEditAttributes().concat([ | |||
105 | const videosAddResumableValidator = [ | 107 | const videosAddResumableValidator = [ |
106 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | 108 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
107 | const user = res.locals.oauth.token.User | 109 | const user = res.locals.oauth.token.User |
108 | |||
109 | const body: express.CustomUploadXFile<express.UploadXFileMetadata> = req.body | 110 | const body: express.CustomUploadXFile<express.UploadXFileMetadata> = req.body |
110 | const file = { ...body, duration: undefined, path: getResumableUploadPath(body.id), filename: body.metadata.filename } | 111 | const file = { ...body, duration: undefined, path: getResumableUploadPath(body.id), filename: body.metadata.filename } |
111 | |||
112 | const cleanup = () => deleteFileAndCatch(file.path) | 112 | const cleanup = () => deleteFileAndCatch(file.path) |
113 | 113 | ||
114 | const uploadId = req.query.upload_id | ||
115 | const sessionExists = await Redis.Instance.doesUploadSessionExist(uploadId) | ||
116 | |||
117 | if (sessionExists) { | ||
118 | const sessionResponse = await Redis.Instance.getUploadSession(uploadId) | ||
119 | |||
120 | if (!sessionResponse) { | ||
121 | res.setHeader('Retry-After', 300) // ask to retry after 5 min, knowing the upload_id is kept for up to 15 min after completion | ||
122 | |||
123 | return res.fail({ | ||
124 | status: HttpStatusCode.SERVICE_UNAVAILABLE_503, | ||
125 | message: 'The upload is already being processed' | ||
126 | }) | ||
127 | } | ||
128 | |||
129 | if (isTestInstance()) { | ||
130 | res.setHeader('x-resumable-upload-cached', 'true') | ||
131 | } | ||
132 | |||
133 | return res.json(sessionResponse) | ||
134 | } | ||
135 | |||
136 | await Redis.Instance.setUploadSession(uploadId) | ||
137 | |||
114 | if (!await doesVideoChannelOfAccountExist(file.metadata.channelId, user, res)) return cleanup() | 138 | if (!await doesVideoChannelOfAccountExist(file.metadata.channelId, user, res)) return cleanup() |
115 | 139 | ||
116 | try { | 140 | try { |