]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/index.ts
Correctly send Flag/Dislike/View activities
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
1 import * as express from 'express'
2 import { extname, join } from 'path'
3 import { VideoCreate, VideoPrivacy, VideoState, VideoUpdate } from '../../../../shared'
4 import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
5 import { processImage } from '../../../helpers/image-utils'
6 import { logger } from '../../../helpers/logger'
7 import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
8 import { getFormattedObjects, getServerActor } from '../../../helpers/utils'
9 import {
10 CONFIG,
11 MIMETYPES,
12 PREVIEWS_SIZE,
13 sequelizeTypescript,
14 THUMBNAILS_SIZE,
15 VIDEO_CATEGORIES,
16 VIDEO_LANGUAGES,
17 VIDEO_LICENCES,
18 VIDEO_PRIVACIES
19 } from '../../../initializers'
20 import {
21 changeVideoChannelShare,
22 federateVideoIfNeeded,
23 fetchRemoteVideoDescription,
24 getVideoActivityPubUrl
25 } from '../../../lib/activitypub'
26 import { JobQueue } from '../../../lib/job-queue'
27 import { Redis } from '../../../lib/redis'
28 import {
29 asyncMiddleware,
30 asyncRetryTransactionMiddleware,
31 authenticate,
32 checkVideoFollowConstraints,
33 commonVideosFiltersValidator,
34 optionalAuthenticate,
35 paginationValidator,
36 setDefaultPagination,
37 setDefaultSort,
38 videosAddValidator,
39 videosGetValidator,
40 videosRemoveValidator,
41 videosSortValidator,
42 videosUpdateValidator
43 } from '../../../middlewares'
44 import { TagModel } from '../../../models/video/tag'
45 import { VideoModel } from '../../../models/video/video'
46 import { VideoFileModel } from '../../../models/video/video-file'
47 import { abuseVideoRouter } from './abuse'
48 import { blacklistRouter } from './blacklist'
49 import { videoCommentRouter } from './comment'
50 import { rateVideoRouter } from './rate'
51 import { ownershipVideoRouter } from './ownership'
52 import { VideoFilter } from '../../../../shared/models/videos/video-query.type'
53 import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils'
54 import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update'
55 import { videoCaptionsRouter } from './captions'
56 import { videoImportsRouter } from './import'
57 import { resetSequelizeInstance } from '../../../helpers/database-utils'
58 import { move } from 'fs-extra'
59 import { watchingRouter } from './watching'
60 import { Notifier } from '../../../lib/notifier'
61 import { sendView } from '../../../lib/activitypub/send/send-view'
62
63 const auditLogger = auditLoggerFactory('videos')
64 const videosRouter = express.Router()
65
66 const reqVideoFileAdd = createReqFiles(
67 [ 'videofile', 'thumbnailfile', 'previewfile' ],
68 Object.assign({}, MIMETYPES.VIDEO.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT),
69 {
70 videofile: CONFIG.STORAGE.TMP_DIR,
71 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
72 previewfile: CONFIG.STORAGE.TMP_DIR
73 }
74 )
75 const reqVideoFileUpdate = createReqFiles(
76 [ 'thumbnailfile', 'previewfile' ],
77 MIMETYPES.IMAGE.MIMETYPE_EXT,
78 {
79 thumbnailfile: CONFIG.STORAGE.TMP_DIR,
80 previewfile: CONFIG.STORAGE.TMP_DIR
81 }
82 )
83
84 videosRouter.use('/', abuseVideoRouter)
85 videosRouter.use('/', blacklistRouter)
86 videosRouter.use('/', rateVideoRouter)
87 videosRouter.use('/', videoCommentRouter)
88 videosRouter.use('/', videoCaptionsRouter)
89 videosRouter.use('/', videoImportsRouter)
90 videosRouter.use('/', ownershipVideoRouter)
91 videosRouter.use('/', watchingRouter)
92
93 videosRouter.get('/categories', listVideoCategories)
94 videosRouter.get('/licences', listVideoLicences)
95 videosRouter.get('/languages', listVideoLanguages)
96 videosRouter.get('/privacies', listVideoPrivacies)
97
98 videosRouter.get('/',
99 paginationValidator,
100 videosSortValidator,
101 setDefaultSort,
102 setDefaultPagination,
103 optionalAuthenticate,
104 commonVideosFiltersValidator,
105 asyncMiddleware(listVideos)
106 )
107 videosRouter.put('/:id',
108 authenticate,
109 reqVideoFileUpdate,
110 asyncMiddleware(videosUpdateValidator),
111 asyncRetryTransactionMiddleware(updateVideo)
112 )
113 videosRouter.post('/upload',
114 authenticate,
115 reqVideoFileAdd,
116 asyncMiddleware(videosAddValidator),
117 asyncRetryTransactionMiddleware(addVideo)
118 )
119
120 videosRouter.get('/:id/description',
121 asyncMiddleware(videosGetValidator),
122 asyncMiddleware(getVideoDescription)
123 )
124 videosRouter.get('/:id',
125 optionalAuthenticate,
126 asyncMiddleware(videosGetValidator),
127 asyncMiddleware(checkVideoFollowConstraints),
128 getVideo
129 )
130 videosRouter.post('/:id/views',
131 asyncMiddleware(videosGetValidator),
132 asyncMiddleware(viewVideo)
133 )
134
135 videosRouter.delete('/:id',
136 authenticate,
137 asyncMiddleware(videosRemoveValidator),
138 asyncRetryTransactionMiddleware(removeVideo)
139 )
140
141 // ---------------------------------------------------------------------------
142
143 export {
144 videosRouter
145 }
146
147 // ---------------------------------------------------------------------------
148
149 function listVideoCategories (req: express.Request, res: express.Response) {
150 res.json(VIDEO_CATEGORIES)
151 }
152
153 function listVideoLicences (req: express.Request, res: express.Response) {
154 res.json(VIDEO_LICENCES)
155 }
156
157 function listVideoLanguages (req: express.Request, res: express.Response) {
158 res.json(VIDEO_LANGUAGES)
159 }
160
161 function listVideoPrivacies (req: express.Request, res: express.Response) {
162 res.json(VIDEO_PRIVACIES)
163 }
164
165 async function addVideo (req: express.Request, res: express.Response) {
166 // Processing the video could be long
167 // Set timeout to 10 minutes
168 req.setTimeout(1000 * 60 * 10, () => {
169 logger.error('Upload video has timed out.')
170 return res.sendStatus(408)
171 })
172
173 const videoPhysicalFile = req.files['videofile'][0]
174 const videoInfo: VideoCreate = req.body
175
176 // Prepare data so we don't block the transaction
177 const videoData = {
178 name: videoInfo.name,
179 remote: false,
180 category: videoInfo.category,
181 licence: videoInfo.licence,
182 language: videoInfo.language,
183 commentsEnabled: videoInfo.commentsEnabled || false,
184 waitTranscoding: videoInfo.waitTranscoding || false,
185 state: CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED,
186 nsfw: videoInfo.nsfw || false,
187 description: videoInfo.description,
188 support: videoInfo.support,
189 privacy: videoInfo.privacy,
190 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
191 channelId: res.locals.videoChannel.id
192 }
193 const video = new VideoModel(videoData)
194 video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
195
196 // Build the file object
197 const { videoFileResolution } = await getVideoFileResolution(videoPhysicalFile.path)
198 const fps = await getVideoFileFPS(videoPhysicalFile.path)
199
200 const videoFileData = {
201 extname: extname(videoPhysicalFile.filename),
202 resolution: videoFileResolution,
203 size: videoPhysicalFile.size,
204 fps
205 }
206 const videoFile = new VideoFileModel(videoFileData)
207
208 // Move physical file
209 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
210 const destination = join(videoDir, video.getVideoFilename(videoFile))
211 await move(videoPhysicalFile.path, destination)
212 // This is important in case if there is another attempt in the retry process
213 videoPhysicalFile.filename = video.getVideoFilename(videoFile)
214 videoPhysicalFile.path = destination
215
216 // Process thumbnail or create it from the video
217 const thumbnailField = req.files['thumbnailfile']
218 if (thumbnailField) {
219 const thumbnailPhysicalFile = thumbnailField[0]
220 await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE)
221 } else {
222 await video.createThumbnail(videoFile)
223 }
224
225 // Process preview or create it from the video
226 const previewField = req.files['previewfile']
227 if (previewField) {
228 const previewPhysicalFile = previewField[0]
229 await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE)
230 } else {
231 await video.createPreview(videoFile)
232 }
233
234 // Create the torrent file
235 await video.createTorrentAndSetInfoHash(videoFile)
236
237 const videoCreated = await sequelizeTypescript.transaction(async t => {
238 const sequelizeOptions = { transaction: t }
239
240 const videoCreated = await video.save(sequelizeOptions)
241 // Do not forget to add video channel information to the created video
242 videoCreated.VideoChannel = res.locals.videoChannel
243
244 videoFile.videoId = video.id
245 await videoFile.save(sequelizeOptions)
246
247 video.VideoFiles = [ videoFile ]
248
249 // Create tags
250 if (videoInfo.tags !== undefined) {
251 const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t)
252
253 await video.$set('Tags', tagInstances, sequelizeOptions)
254 video.Tags = tagInstances
255 }
256
257 // Schedule an update in the future?
258 if (videoInfo.scheduleUpdate) {
259 await ScheduleVideoUpdateModel.create({
260 videoId: video.id,
261 updateAt: videoInfo.scheduleUpdate.updateAt,
262 privacy: videoInfo.scheduleUpdate.privacy || null
263 }, { transaction: t })
264 }
265
266 await federateVideoIfNeeded(video, true, t)
267
268 auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON()))
269 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
270
271 return videoCreated
272 })
273
274 Notifier.Instance.notifyOnNewVideo(videoCreated)
275
276 if (video.state === VideoState.TO_TRANSCODE) {
277 // Put uuid because we don't have id auto incremented for now
278 const dataInput = {
279 videoUUID: videoCreated.uuid,
280 isNewVideo: true
281 }
282
283 await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
284 }
285
286 return res.json({
287 video: {
288 id: videoCreated.id,
289 uuid: videoCreated.uuid
290 }
291 }).end()
292 }
293
294 async function updateVideo (req: express.Request, res: express.Response) {
295 const videoInstance: VideoModel = res.locals.video
296 const videoFieldsSave = videoInstance.toJSON()
297 const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON())
298 const videoInfoToUpdate: VideoUpdate = req.body
299 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
300 const wasUnlistedVideo = videoInstance.privacy === VideoPrivacy.UNLISTED
301
302 // Process thumbnail or create it from the video
303 if (req.files && req.files['thumbnailfile']) {
304 const thumbnailPhysicalFile = req.files['thumbnailfile'][0]
305 await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, videoInstance.getThumbnailName()), THUMBNAILS_SIZE)
306 }
307
308 // Process preview or create it from the video
309 if (req.files && req.files['previewfile']) {
310 const previewPhysicalFile = req.files['previewfile'][0]
311 await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, videoInstance.getPreviewName()), PREVIEWS_SIZE)
312 }
313
314 try {
315 const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => {
316 const sequelizeOptions = { transaction: t }
317 const oldVideoChannel = videoInstance.VideoChannel
318
319 if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
320 if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
321 if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
322 if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
323 if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
324 if (videoInfoToUpdate.waitTranscoding !== undefined) videoInstance.set('waitTranscoding', videoInfoToUpdate.waitTranscoding)
325 if (videoInfoToUpdate.support !== undefined) videoInstance.set('support', videoInfoToUpdate.support)
326 if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
327 if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled)
328 if (videoInfoToUpdate.privacy !== undefined) {
329 const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10)
330 videoInstance.set('privacy', newPrivacy)
331
332 if (wasPrivateVideo === true && newPrivacy !== VideoPrivacy.PRIVATE) {
333 videoInstance.set('publishedAt', new Date())
334 }
335 }
336
337 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
338
339 // Video tags update?
340 if (videoInfoToUpdate.tags !== undefined) {
341 const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
342
343 await videoInstanceUpdated.$set('Tags', tagInstances, sequelizeOptions)
344 videoInstanceUpdated.Tags = tagInstances
345 }
346
347 // Video channel update?
348 if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) {
349 await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t })
350 videoInstanceUpdated.VideoChannel = res.locals.videoChannel
351
352 if (wasPrivateVideo === false) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t)
353 }
354
355 // Schedule an update in the future?
356 if (videoInfoToUpdate.scheduleUpdate) {
357 await ScheduleVideoUpdateModel.upsert({
358 videoId: videoInstanceUpdated.id,
359 updateAt: videoInfoToUpdate.scheduleUpdate.updateAt,
360 privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
361 }, { transaction: t })
362 } else if (videoInfoToUpdate.scheduleUpdate === null) {
363 await ScheduleVideoUpdateModel.deleteByVideoId(videoInstanceUpdated.id, t)
364 }
365
366 const isNewVideo = wasPrivateVideo && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE
367
368 // Don't send update if the video was unfederated
369 if (!videoInstanceUpdated.VideoBlacklist || videoInstanceUpdated.VideoBlacklist.unfederated === false) {
370 await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
371 }
372
373 auditLogger.update(
374 getAuditIdFromRes(res),
375 new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()),
376 oldVideoAuditView
377 )
378 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
379
380 return videoInstanceUpdated
381 })
382
383 if (wasUnlistedVideo || wasPrivateVideo) {
384 Notifier.Instance.notifyOnNewVideo(videoInstanceUpdated)
385 }
386 } catch (err) {
387 // Force fields we want to update
388 // If the transaction is retried, sequelize will think the object has not changed
389 // So it will skip the SQL request, even if the last one was ROLLBACKed!
390 resetSequelizeInstance(videoInstance, videoFieldsSave)
391
392 throw err
393 }
394
395 return res.type('json').status(204).end()
396 }
397
398 function getVideo (req: express.Request, res: express.Response) {
399 const videoInstance = res.locals.video
400
401 if (videoInstance.isOutdated()) {
402 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: videoInstance.url } })
403 .catch(err => logger.error('Cannot create AP refresher job for video %s.', videoInstance.url, { err }))
404 }
405
406 return res.json(videoInstance.toFormattedDetailsJSON())
407 }
408
409 async function viewVideo (req: express.Request, res: express.Response) {
410 const videoInstance = res.locals.video
411
412 const ip = req.ip
413 const exists = await Redis.Instance.isVideoIPViewExists(ip, videoInstance.uuid)
414 if (exists) {
415 logger.debug('View for ip %s and video %s already exists.', ip, videoInstance.uuid)
416 return res.status(204).end()
417 }
418
419 await Promise.all([
420 Redis.Instance.addVideoView(videoInstance.id),
421 Redis.Instance.setIPVideoView(ip, videoInstance.uuid)
422 ])
423
424 const serverActor = await getServerActor()
425 await sendView(serverActor, videoInstance, undefined)
426
427 return res.status(204).end()
428 }
429
430 async function getVideoDescription (req: express.Request, res: express.Response) {
431 const videoInstance = res.locals.video
432 let description = ''
433
434 if (videoInstance.isOwned()) {
435 description = videoInstance.description
436 } else {
437 description = await fetchRemoteVideoDescription(videoInstance)
438 }
439
440 return res.json({ description })
441 }
442
443 async function listVideos (req: express.Request, res: express.Response) {
444 const resultList = await VideoModel.listForApi({
445 start: req.query.start,
446 count: req.query.count,
447 sort: req.query.sort,
448 includeLocalVideos: true,
449 categoryOneOf: req.query.categoryOneOf,
450 licenceOneOf: req.query.licenceOneOf,
451 languageOneOf: req.query.languageOneOf,
452 tagsOneOf: req.query.tagsOneOf,
453 tagsAllOf: req.query.tagsAllOf,
454 nsfw: buildNSFWFilter(res, req.query.nsfw),
455 filter: req.query.filter as VideoFilter,
456 withFiles: false,
457 user: res.locals.oauth ? res.locals.oauth.token.User : undefined
458 })
459
460 return res.json(getFormattedObjects(resultList.data, resultList.total))
461 }
462
463 async function removeVideo (req: express.Request, res: express.Response) {
464 const videoInstance: VideoModel = res.locals.video
465
466 await sequelizeTypescript.transaction(async t => {
467 await videoInstance.destroy({ transaction: t })
468 })
469
470 auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON()))
471 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
472
473 return res.type('json').status(204).end()
474 }