]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
93e1258c | 2 | import { extname, join } from 'path' |
571389d4 | 3 | import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared' |
da854ddd C |
4 | import { renamePromise } from '../../../helpers/core-utils' |
5 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
6 | import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils' | |
ac81d1a0 | 7 | import { processImage } from '../../../helpers/image-utils' |
da854ddd | 8 | import { logger } from '../../../helpers/logger' |
47564bbe | 9 | import { createReqFiles, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils' |
65fcc311 | 10 | import { |
ac81d1a0 C |
11 | CONFIG, |
12 | IMAGE_MIMETYPE_EXT, | |
13 | PREVIEWS_SIZE, | |
14 | sequelizeTypescript, | |
15 | THUMBNAILS_SIZE, | |
16 | VIDEO_CATEGORIES, | |
17 | VIDEO_LANGUAGES, | |
18 | VIDEO_LICENCES, | |
19 | VIDEO_MIMETYPE_EXT, | |
3fd3ab2d C |
20 | VIDEO_PRIVACIES |
21 | } from '../../../initializers' | |
da854ddd | 22 | import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub' |
50d6de9c | 23 | import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send' |
94a5ff8a | 24 | import { JobQueue } from '../../../lib/job-queue' |
65fcc311 | 25 | import { |
ac81d1a0 C |
26 | asyncMiddleware, |
27 | authenticate, | |
28 | paginationValidator, | |
29 | setDefaultPagination, | |
30 | setDefaultSort, | |
31 | videosAddValidator, | |
32 | videosGetValidator, | |
33 | videosRemoveValidator, | |
34 | videosSearchValidator, | |
35 | videosSortValidator, | |
36 | videosUpdateValidator | |
65fcc311 | 37 | } from '../../../middlewares' |
3fd3ab2d C |
38 | import { TagModel } from '../../../models/video/tag' |
39 | import { VideoModel } from '../../../models/video/video' | |
40 | import { VideoFileModel } from '../../../models/video/video-file' | |
65fcc311 C |
41 | import { abuseVideoRouter } from './abuse' |
42 | import { blacklistRouter } from './blacklist' | |
72c7248b | 43 | import { videoChannelRouter } from './channel' |
bf1f6508 | 44 | import { videoCommentRouter } from './comment' |
571389d4 | 45 | import { rateVideoRouter } from './rate' |
65fcc311 C |
46 | |
47 | const videosRouter = express.Router() | |
9f10b292 | 48 | |
ac81d1a0 C |
49 | const reqVideoFileAdd = createReqFiles( |
50 | [ 'videofile', 'thumbnailfile', 'previewfile' ], | |
51 | Object.assign({}, VIDEO_MIMETYPE_EXT, IMAGE_MIMETYPE_EXT), | |
52 | { | |
53 | videofile: CONFIG.STORAGE.VIDEOS_DIR, | |
54 | thumbnailfile: CONFIG.STORAGE.THUMBNAILS_DIR, | |
55 | previewfile: CONFIG.STORAGE.PREVIEWS_DIR | |
56 | } | |
57 | ) | |
58 | const reqVideoFileUpdate = createReqFiles( | |
59 | [ 'thumbnailfile', 'previewfile' ], | |
60 | IMAGE_MIMETYPE_EXT, | |
61 | { | |
62 | thumbnailfile: CONFIG.STORAGE.THUMBNAILS_DIR, | |
63 | previewfile: CONFIG.STORAGE.PREVIEWS_DIR | |
64 | } | |
65 | ) | |
8c308c2b | 66 | |
65fcc311 C |
67 | videosRouter.use('/', abuseVideoRouter) |
68 | videosRouter.use('/', blacklistRouter) | |
69 | videosRouter.use('/', rateVideoRouter) | |
72c7248b | 70 | videosRouter.use('/', videoChannelRouter) |
bf1f6508 | 71 | videosRouter.use('/', videoCommentRouter) |
d33242b0 | 72 | |
65fcc311 C |
73 | videosRouter.get('/categories', listVideoCategories) |
74 | videosRouter.get('/licences', listVideoLicences) | |
75 | videosRouter.get('/languages', listVideoLanguages) | |
fd45e8f4 | 76 | videosRouter.get('/privacies', listVideoPrivacies) |
6e07c3de | 77 | |
65fcc311 C |
78 | videosRouter.get('/', |
79 | paginationValidator, | |
80 | videosSortValidator, | |
1174a847 | 81 | setDefaultSort, |
f05a1c30 | 82 | setDefaultPagination, |
eb080476 | 83 | asyncMiddleware(listVideos) |
fbf1134e | 84 | ) |
f3aaa9a9 C |
85 | videosRouter.get('/search', |
86 | videosSearchValidator, | |
87 | paginationValidator, | |
88 | videosSortValidator, | |
1174a847 | 89 | setDefaultSort, |
f05a1c30 | 90 | setDefaultPagination, |
f3aaa9a9 C |
91 | asyncMiddleware(searchVideos) |
92 | ) | |
65fcc311 C |
93 | videosRouter.put('/:id', |
94 | authenticate, | |
ac81d1a0 | 95 | reqVideoFileUpdate, |
a2431b7d | 96 | asyncMiddleware(videosUpdateValidator), |
eb080476 | 97 | asyncMiddleware(updateVideoRetryWrapper) |
7b1f49de | 98 | ) |
e95561cd | 99 | videosRouter.post('/upload', |
65fcc311 | 100 | authenticate, |
ac81d1a0 | 101 | reqVideoFileAdd, |
3fd3ab2d | 102 | asyncMiddleware(videosAddValidator), |
eb080476 | 103 | asyncMiddleware(addVideoRetryWrapper) |
fbf1134e | 104 | ) |
9567011b C |
105 | |
106 | videosRouter.get('/:id/description', | |
a2431b7d | 107 | asyncMiddleware(videosGetValidator), |
9567011b C |
108 | asyncMiddleware(getVideoDescription) |
109 | ) | |
65fcc311 | 110 | videosRouter.get('/:id', |
a2431b7d | 111 | asyncMiddleware(videosGetValidator), |
68ce3ae0 | 112 | getVideo |
fbf1134e | 113 | ) |
1f3e9fec C |
114 | videosRouter.post('/:id/views', |
115 | asyncMiddleware(videosGetValidator), | |
116 | asyncMiddleware(viewVideo) | |
117 | ) | |
198b205c | 118 | |
65fcc311 C |
119 | videosRouter.delete('/:id', |
120 | authenticate, | |
a2431b7d | 121 | asyncMiddleware(videosRemoveValidator), |
eb080476 | 122 | asyncMiddleware(removeVideoRetryWrapper) |
fbf1134e | 123 | ) |
198b205c | 124 | |
9f10b292 | 125 | // --------------------------------------------------------------------------- |
c45f7f84 | 126 | |
65fcc311 C |
127 | export { |
128 | videosRouter | |
129 | } | |
c45f7f84 | 130 | |
9f10b292 | 131 | // --------------------------------------------------------------------------- |
c45f7f84 | 132 | |
556ddc31 | 133 | function listVideoCategories (req: express.Request, res: express.Response) { |
65fcc311 | 134 | res.json(VIDEO_CATEGORIES) |
6e07c3de C |
135 | } |
136 | ||
556ddc31 | 137 | function listVideoLicences (req: express.Request, res: express.Response) { |
65fcc311 | 138 | res.json(VIDEO_LICENCES) |
6f0c39e2 C |
139 | } |
140 | ||
556ddc31 | 141 | function listVideoLanguages (req: express.Request, res: express.Response) { |
65fcc311 | 142 | res.json(VIDEO_LANGUAGES) |
3092476e C |
143 | } |
144 | ||
fd45e8f4 C |
145 | function listVideoPrivacies (req: express.Request, res: express.Response) { |
146 | res.json(VIDEO_PRIVACIES) | |
147 | } | |
148 | ||
ed04d94f C |
149 | // Wrapper to video add that retry the function if there is a database error |
150 | // We need this because we run the transaction in SERIALIZABLE isolation that can fail | |
eb080476 | 151 | async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
d6a5b018 | 152 | const options = { |
556ddc31 | 153 | arguments: [ req, res, req.files['videofile'][0] ], |
d6a5b018 C |
154 | errorMessage: 'Cannot insert the video with many retries.' |
155 | } | |
ed04d94f | 156 | |
cadb46d8 | 157 | const video = await retryTransactionWrapper(addVideo, options) |
eb080476 | 158 | |
cadb46d8 C |
159 | res.json({ |
160 | video: { | |
161 | id: video.id, | |
162 | uuid: video.uuid | |
163 | } | |
164 | }).end() | |
ed04d94f C |
165 | } |
166 | ||
e11f68a3 | 167 | async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) { |
556ddc31 | 168 | const videoInfo: VideoCreate = req.body |
9f10b292 | 169 | |
e11f68a3 C |
170 | // Prepare data so we don't block the transaction |
171 | const videoData = { | |
172 | name: videoInfo.name, | |
173 | remote: false, | |
174 | extname: extname(videoPhysicalFile.filename), | |
175 | category: videoInfo.category, | |
176 | licence: videoInfo.licence, | |
177 | language: videoInfo.language, | |
47564bbe | 178 | commentsEnabled: videoInfo.commentsEnabled, |
e11f68a3 C |
179 | nsfw: videoInfo.nsfw, |
180 | description: videoInfo.description, | |
181 | privacy: videoInfo.privacy, | |
182 | duration: videoPhysicalFile['duration'], // duration was added by a previous middleware | |
183 | channelId: res.locals.videoChannel.id | |
184 | } | |
3fd3ab2d | 185 | const video = new VideoModel(videoData) |
e11f68a3 | 186 | video.url = getVideoActivityPubUrl(video) |
eb080476 | 187 | |
ac81d1a0 | 188 | const videoFileHeight = await getVideoFileHeight(videoPhysicalFile.path) |
6fcd19ba | 189 | |
e11f68a3 C |
190 | const videoFileData = { |
191 | extname: extname(videoPhysicalFile.filename), | |
192 | resolution: videoFileHeight, | |
193 | size: videoPhysicalFile.size | |
194 | } | |
3fd3ab2d | 195 | const videoFile = new VideoFileModel(videoFileData) |
e11f68a3 | 196 | const videoDir = CONFIG.STORAGE.VIDEOS_DIR |
e11f68a3 | 197 | const destination = join(videoDir, video.getVideoFilename(videoFile)) |
e3a682a8 | 198 | |
ac81d1a0 | 199 | await renamePromise(videoPhysicalFile.path, destination) |
e3a682a8 C |
200 | // This is important in case if there is another attempt in the retry process |
201 | videoPhysicalFile.filename = video.getVideoFilename(videoFile) | |
ac81d1a0 C |
202 | |
203 | // Process thumbnail or create it from the video | |
204 | const thumbnailField = req.files['thumbnailfile'] | |
205 | if (thumbnailField) { | |
206 | const thumbnailPhysicalFile = thumbnailField[0] | |
207 | await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE) | |
208 | } else { | |
209 | await video.createThumbnail(videoFile) | |
210 | } | |
93e1258c | 211 | |
ac81d1a0 C |
212 | // Process preview or create it from the video |
213 | const previewField = req.files['previewfile'] | |
214 | if (previewField) { | |
215 | const previewPhysicalFile = previewField[0] | |
216 | await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE) | |
217 | } else { | |
218 | await video.createPreview(videoFile) | |
219 | } | |
eb080476 | 220 | |
ac81d1a0 | 221 | await video.createTorrentAndSetInfoHash(videoFile) |
eb080476 | 222 | |
94a5ff8a | 223 | const videoCreated = await sequelizeTypescript.transaction(async t => { |
e11f68a3 | 224 | const sequelizeOptions = { transaction: t } |
eb080476 | 225 | |
eb080476 C |
226 | const videoCreated = await video.save(sequelizeOptions) |
227 | // Do not forget to add video channel information to the created video | |
228 | videoCreated.VideoChannel = res.locals.videoChannel | |
7920c273 | 229 | |
eb080476 | 230 | videoFile.videoId = video.id |
eb080476 | 231 | await videoFile.save(sequelizeOptions) |
e11f68a3 C |
232 | |
233 | video.VideoFiles = [ videoFile ] | |
93e1258c | 234 | |
eb080476 | 235 | if (videoInfo.tags) { |
3fd3ab2d | 236 | const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t) |
eb080476 | 237 | |
3fd3ab2d | 238 | await video.$set('Tags', tagInstances, sequelizeOptions) |
eb080476 C |
239 | video.Tags = tagInstances |
240 | } | |
241 | ||
242 | // Let transcoding job send the video to friends because the video file extension might change | |
cadb46d8 | 243 | if (CONFIG.TRANSCODING.ENABLED === true) return videoCreated |
60862425 | 244 | // Don't send video to remote servers, it is private |
cadb46d8 | 245 | if (video.privacy === VideoPrivacy.PRIVATE) return videoCreated |
eb080476 | 246 | |
50d6de9c | 247 | await sendCreateVideo(video, t) |
a7d647c4 | 248 | await shareVideoByServerAndChannel(video, t) |
eb080476 | 249 | |
cadb46d8 C |
250 | logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) |
251 | ||
252 | return videoCreated | |
253 | }) | |
94a5ff8a C |
254 | |
255 | if (CONFIG.TRANSCODING.ENABLED === true) { | |
256 | // Put uuid because we don't have id auto incremented for now | |
257 | const dataInput = { | |
258 | videoUUID: videoCreated.uuid | |
259 | } | |
260 | ||
261 | await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) | |
262 | } | |
263 | ||
264 | return videoCreated | |
7b1f49de C |
265 | } |
266 | ||
eb080476 | 267 | async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
d6a5b018 C |
268 | const options = { |
269 | arguments: [ req, res ], | |
270 | errorMessage: 'Cannot update the video with many retries.' | |
271 | } | |
ed04d94f | 272 | |
eb080476 C |
273 | await retryTransactionWrapper(updateVideo, options) |
274 | ||
275 | return res.type('json').status(204).end() | |
ed04d94f C |
276 | } |
277 | ||
eb080476 | 278 | async function updateVideo (req: express.Request, res: express.Response) { |
3fd3ab2d | 279 | const videoInstance: VideoModel = res.locals.video |
7f4e7c36 | 280 | const videoFieldsSave = videoInstance.toJSON() |
556ddc31 | 281 | const videoInfoToUpdate: VideoUpdate = req.body |
fd45e8f4 | 282 | const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE |
7b1f49de | 283 | |
ac81d1a0 C |
284 | // Process thumbnail or create it from the video |
285 | if (req.files && req.files['thumbnailfile']) { | |
286 | const thumbnailPhysicalFile = req.files['thumbnailfile'][0] | |
287 | await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, videoInstance.getThumbnailName()), THUMBNAILS_SIZE) | |
288 | } | |
289 | ||
290 | // Process preview or create it from the video | |
291 | if (req.files && req.files['previewfile']) { | |
292 | const previewPhysicalFile = req.files['previewfile'][0] | |
293 | await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, videoInstance.getPreviewName()), PREVIEWS_SIZE) | |
294 | } | |
295 | ||
eb080476 | 296 | try { |
3fd3ab2d | 297 | await sequelizeTypescript.transaction(async t => { |
eb080476 C |
298 | const sequelizeOptions = { |
299 | transaction: t | |
300 | } | |
7b1f49de | 301 | |
eb080476 C |
302 | if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name) |
303 | if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category) | |
304 | if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence) | |
305 | if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language) | |
306 | if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw) | |
f595d394 | 307 | if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', parseInt(videoInfoToUpdate.privacy.toString(), 10)) |
eb080476 | 308 | if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description) |
47564bbe | 309 | if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled) |
7b1f49de | 310 | |
54141398 | 311 | const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) |
7b1f49de | 312 | |
eb080476 | 313 | if (videoInfoToUpdate.tags) { |
3fd3ab2d | 314 | const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t) |
7b1f49de | 315 | |
3fd3ab2d | 316 | await videoInstance.$set('Tags', tagInstances, sequelizeOptions) |
eb080476 C |
317 | videoInstance.Tags = tagInstances |
318 | } | |
7920c273 | 319 | |
eb080476 | 320 | // Now we'll update the video's meta data to our friends |
fd45e8f4 | 321 | if (wasPrivateVideo === false) { |
54141398 | 322 | await sendUpdateVideo(videoInstanceUpdated, t) |
fd45e8f4 C |
323 | } |
324 | ||
60862425 | 325 | // Video is not private anymore, send a create action to remote servers |
f595d394 | 326 | if (wasPrivateVideo === true && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE) { |
50d6de9c | 327 | await sendCreateVideo(videoInstanceUpdated, t) |
a7d647c4 | 328 | await shareVideoByServerAndChannel(videoInstanceUpdated, t) |
fd45e8f4 | 329 | } |
eb080476 | 330 | }) |
6fcd19ba | 331 | |
eb080476 C |
332 | logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid) |
333 | } catch (err) { | |
6fcd19ba C |
334 | // Force fields we want to update |
335 | // If the transaction is retried, sequelize will think the object has not changed | |
336 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
eb080476 | 337 | resetSequelizeInstance(videoInstance, videoFieldsSave) |
6fcd19ba C |
338 | |
339 | throw err | |
eb080476 | 340 | } |
9f10b292 | 341 | } |
8c308c2b | 342 | |
1f3e9fec C |
343 | function getVideo (req: express.Request, res: express.Response) { |
344 | const videoInstance = res.locals.video | |
345 | ||
346 | return res.json(videoInstance.toFormattedDetailsJSON()) | |
347 | } | |
348 | ||
349 | async function viewVideo (req: express.Request, res: express.Response) { | |
818f7987 | 350 | const videoInstance = res.locals.video |
9e167724 | 351 | |
1f3e9fec | 352 | await videoInstance.increment('views') |
50d6de9c | 353 | const serverAccount = await getServerActor() |
40ff5707 | 354 | |
9e167724 | 355 | if (videoInstance.isOwned()) { |
1f3e9fec | 356 | await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined) |
e4c87ec2 | 357 | } else { |
1f3e9fec | 358 | await sendCreateViewToOrigin(serverAccount, videoInstance, undefined) |
9e167724 C |
359 | } |
360 | ||
1f3e9fec | 361 | return res.status(204).end() |
9f10b292 | 362 | } |
8c308c2b | 363 | |
9567011b C |
364 | async function getVideoDescription (req: express.Request, res: express.Response) { |
365 | const videoInstance = res.locals.video | |
366 | let description = '' | |
367 | ||
368 | if (videoInstance.isOwned()) { | |
369 | description = videoInstance.description | |
370 | } else { | |
571389d4 | 371 | description = await fetchRemoteVideoDescription(videoInstance) |
9567011b C |
372 | } |
373 | ||
374 | return res.json({ description }) | |
375 | } | |
376 | ||
eb080476 | 377 | async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 378 | const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 C |
379 | |
380 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 381 | } |
c45f7f84 | 382 | |
eb080476 | 383 | async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
91f6f169 C |
384 | const options = { |
385 | arguments: [ req, res ], | |
386 | errorMessage: 'Cannot remove the video with many retries.' | |
387 | } | |
8c308c2b | 388 | |
eb080476 C |
389 | await retryTransactionWrapper(removeVideo, options) |
390 | ||
391 | return res.type('json').status(204).end() | |
91f6f169 C |
392 | } |
393 | ||
eb080476 | 394 | async function removeVideo (req: express.Request, res: express.Response) { |
3fd3ab2d | 395 | const videoInstance: VideoModel = res.locals.video |
91f6f169 | 396 | |
3fd3ab2d | 397 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 398 | await videoInstance.destroy({ transaction: t }) |
91f6f169 | 399 | }) |
eb080476 C |
400 | |
401 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) | |
9f10b292 | 402 | } |
8c308c2b | 403 | |
eb080476 | 404 | async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 405 | const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags( |
f3aaa9a9 | 406 | req.query.search, |
eb080476 C |
407 | req.query.start, |
408 | req.query.count, | |
409 | req.query.sort | |
410 | ) | |
411 | ||
412 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 413 | } |