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