diff options
author | Chocobozzz <me@florianbigard.com> | 2021-11-10 09:42:37 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-11-10 09:43:33 +0100 |
commit | 020d3d3d79338148873cfd78ba59856f63260f2f (patch) | |
tree | 343218cafb3573d7285c4c4616d629f398009913 /server/controllers/api | |
parent | 1868ff3db93a8f2e4ceb91aa3600b633ca00b4f7 (diff) | |
download | PeerTube-020d3d3d79338148873cfd78ba59856f63260f2f.tar.gz PeerTube-020d3d3d79338148873cfd78ba59856f63260f2f.tar.zst PeerTube-020d3d3d79338148873cfd78ba59856f63260f2f.zip |
Remove resumable cache after upload success
Diffstat (limited to 'server/controllers/api')
-rw-r--r-- | server/controllers/api/videos/upload.ts | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/server/controllers/api/videos/upload.ts b/server/controllers/api/videos/upload.ts index 02aadd426..3e9979330 100644 --- a/server/controllers/api/videos/upload.ts +++ b/server/controllers/api/videos/upload.ts | |||
@@ -19,7 +19,7 @@ import { VideoPathManager } from '@server/lib/video-path-manager' | |||
19 | import { buildNextVideoState } from '@server/lib/video-state' | 19 | import { buildNextVideoState } from '@server/lib/video-state' |
20 | import { openapiOperationDoc } from '@server/middlewares/doc' | 20 | import { openapiOperationDoc } from '@server/middlewares/doc' |
21 | import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' | 21 | import { MVideo, MVideoFile, MVideoFullLight } from '@server/types/models' |
22 | import { uploadx } from '@uploadx/core' | 22 | import { Uploadx } from '@uploadx/core' |
23 | import { VideoCreate, VideoState } from '../../../../shared' | 23 | import { VideoCreate, VideoState } from '../../../../shared' |
24 | import { HttpStatusCode } from '../../../../shared/models' | 24 | import { HttpStatusCode } from '../../../../shared/models' |
25 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | 25 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
@@ -41,6 +41,7 @@ import { | |||
41 | authenticate, | 41 | authenticate, |
42 | videosAddLegacyValidator, | 42 | videosAddLegacyValidator, |
43 | videosAddResumableInitValidator, | 43 | videosAddResumableInitValidator, |
44 | videosResumableUploadIdValidator, | ||
44 | videosAddResumableValidator | 45 | videosAddResumableValidator |
45 | } from '../../../middlewares' | 46 | } from '../../../middlewares' |
46 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' | 47 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' |
@@ -50,7 +51,9 @@ import { VideoFileModel } from '../../../models/video/video-file' | |||
50 | const lTags = loggerTagsFactory('api', 'video') | 51 | const lTags = loggerTagsFactory('api', 'video') |
51 | const auditLogger = auditLoggerFactory('videos') | 52 | const auditLogger = auditLoggerFactory('videos') |
52 | const uploadRouter = express.Router() | 53 | const uploadRouter = express.Router() |
53 | const uploadxMiddleware = uploadx.upload({ directory: getResumableUploadPath() }) | 54 | |
55 | const uploadx = new Uploadx({ directory: getResumableUploadPath() }) | ||
56 | uploadx.getUserId = (_, res: express.Response) => res.locals.oauth?.token.user.id | ||
54 | 57 | ||
55 | const reqVideoFileAdd = createReqFiles( | 58 | const reqVideoFileAdd = createReqFiles( |
56 | [ 'videofile', 'thumbnailfile', 'previewfile' ], | 59 | [ 'videofile', 'thumbnailfile', 'previewfile' ], |
@@ -84,18 +87,21 @@ uploadRouter.post('/upload-resumable', | |||
84 | authenticate, | 87 | authenticate, |
85 | reqVideoFileAddResumable, | 88 | reqVideoFileAddResumable, |
86 | asyncMiddleware(videosAddResumableInitValidator), | 89 | asyncMiddleware(videosAddResumableInitValidator), |
87 | uploadxMiddleware | 90 | uploadx.upload |
88 | ) | 91 | ) |
89 | 92 | ||
90 | uploadRouter.delete('/upload-resumable', | 93 | uploadRouter.delete('/upload-resumable', |
91 | authenticate, | 94 | authenticate, |
92 | uploadxMiddleware | 95 | videosResumableUploadIdValidator, |
96 | asyncMiddleware(deleteUploadResumableCache), | ||
97 | uploadx.upload | ||
93 | ) | 98 | ) |
94 | 99 | ||
95 | uploadRouter.put('/upload-resumable', | 100 | uploadRouter.put('/upload-resumable', |
96 | openapiOperationDoc({ operationId: 'uploadResumable' }), | 101 | openapiOperationDoc({ operationId: 'uploadResumable' }), |
97 | authenticate, | 102 | authenticate, |
98 | uploadxMiddleware, // uploadx doesn't next() before the file upload completes | 103 | videosResumableUploadIdValidator, |
104 | uploadx.upload, // uploadx doesn't next() before the file upload completes | ||
99 | asyncMiddleware(videosAddResumableValidator), | 105 | asyncMiddleware(videosAddResumableValidator), |
100 | asyncMiddleware(addVideoResumable) | 106 | asyncMiddleware(addVideoResumable) |
101 | ) | 107 | ) |
@@ -108,7 +114,7 @@ export { | |||
108 | 114 | ||
109 | // --------------------------------------------------------------------------- | 115 | // --------------------------------------------------------------------------- |
110 | 116 | ||
111 | export async function addVideoLegacy (req: express.Request, res: express.Response) { | 117 | async function addVideoLegacy (req: express.Request, res: express.Response) { |
112 | // Uploading the video could be long | 118 | // Uploading the video could be long |
113 | // Set timeout to 10 minutes, as Express's default is 2 minutes | 119 | // Set timeout to 10 minutes, as Express's default is 2 minutes |
114 | req.setTimeout(1000 * 60 * 10, () => { | 120 | req.setTimeout(1000 * 60 * 10, () => { |
@@ -128,7 +134,7 @@ export async function addVideoLegacy (req: express.Request, res: express.Respons | |||
128 | return res.json(response) | 134 | return res.json(response) |
129 | } | 135 | } |
130 | 136 | ||
131 | export async function addVideoResumable (req: express.Request, res: express.Response) { | 137 | async function addVideoResumable (req: express.Request, res: express.Response) { |
132 | const videoPhysicalFile = res.locals.videoFileResumable | 138 | const videoPhysicalFile = res.locals.videoFileResumable |
133 | const videoInfo = videoPhysicalFile.metadata | 139 | const videoInfo = videoPhysicalFile.metadata |
134 | const files = { previewfile: videoInfo.previewfile } | 140 | const files = { previewfile: videoInfo.previewfile } |
@@ -291,3 +297,9 @@ function createTorrentFederate (video: MVideoFullLight, videoFile: MVideoFile) { | |||
291 | }) | 297 | }) |
292 | .catch(err => logger.error('Cannot federate or notify video creation %s', video.url, { err, ...lTags(video.uuid) })) | 298 | .catch(err => logger.error('Cannot federate or notify video creation %s', video.url, { err, ...lTags(video.uuid) })) |
293 | } | 299 | } |
300 | |||
301 | async function deleteUploadResumableCache (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
302 | await Redis.Instance.deleteUploadSession(req.query.upload_id) | ||
303 | |||
304 | return next() | ||
305 | } | ||