]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/index.ts
Add comments controller
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
1 import * as express from 'express'
2 import * as multer from 'multer'
3 import { extname, join } from 'path'
4 import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared'
5 import {
6 generateRandomString,
7 getFormattedObjects,
8 getVideoFileHeight,
9 logger,
10 renamePromise,
11 resetSequelizeInstance,
12 retryTransactionWrapper
13 } from '../../../helpers'
14 import { getServerActor } from '../../../helpers/utils'
15 import {
16 CONFIG,
17 sequelizeTypescript,
18 VIDEO_CATEGORIES,
19 VIDEO_LANGUAGES,
20 VIDEO_LICENCES,
21 VIDEO_MIMETYPE_EXT,
22 VIDEO_PRIVACIES
23 } from '../../../initializers'
24 import {
25 fetchRemoteVideoDescription,
26 getVideoActivityPubUrl,
27 shareVideoByServerAndChannel
28 } from '../../../lib/activitypub'
29 import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
30 import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler'
31 import {
32 asyncMiddleware,
33 authenticate,
34 paginationValidator,
35 setPagination,
36 setVideosSort,
37 videosAddValidator,
38 videosGetValidator,
39 videosRemoveValidator,
40 videosSearchValidator,
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 { videoChannelRouter } from './channel'
50 import { videoCommentRouter } from './comment'
51 import { rateVideoRouter } from './rate'
52
53 const videosRouter = express.Router()
54
55 // multer configuration
56 const storage = multer.diskStorage({
57 destination: (req, file, cb) => {
58 cb(null, CONFIG.STORAGE.VIDEOS_DIR)
59 },
60
61 filename: async (req, file, cb) => {
62 const extension = VIDEO_MIMETYPE_EXT[file.mimetype]
63 let randomString = ''
64
65 try {
66 randomString = await generateRandomString(16)
67 } catch (err) {
68 logger.error('Cannot generate random string for file name.', err)
69 randomString = 'fake-random-string'
70 }
71
72 cb(null, randomString + extension)
73 }
74 })
75
76 const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
77
78 videosRouter.use('/', abuseVideoRouter)
79 videosRouter.use('/', blacklistRouter)
80 videosRouter.use('/', rateVideoRouter)
81 videosRouter.use('/', videoChannelRouter)
82 videosRouter.use('/', videoCommentRouter)
83
84 videosRouter.get('/categories', listVideoCategories)
85 videosRouter.get('/licences', listVideoLicences)
86 videosRouter.get('/languages', listVideoLanguages)
87 videosRouter.get('/privacies', listVideoPrivacies)
88
89 videosRouter.get('/',
90 paginationValidator,
91 videosSortValidator,
92 setVideosSort,
93 setPagination,
94 asyncMiddleware(listVideos)
95 )
96 videosRouter.get('/search',
97 videosSearchValidator,
98 paginationValidator,
99 videosSortValidator,
100 setVideosSort,
101 setPagination,
102 asyncMiddleware(searchVideos)
103 )
104 videosRouter.put('/:id',
105 authenticate,
106 asyncMiddleware(videosUpdateValidator),
107 asyncMiddleware(updateVideoRetryWrapper)
108 )
109 videosRouter.post('/upload',
110 authenticate,
111 reqFiles,
112 asyncMiddleware(videosAddValidator),
113 asyncMiddleware(addVideoRetryWrapper)
114 )
115
116 videosRouter.get('/:id/description',
117 asyncMiddleware(videosGetValidator),
118 asyncMiddleware(getVideoDescription)
119 )
120 videosRouter.get('/:id',
121 asyncMiddleware(videosGetValidator),
122 getVideo
123 )
124 videosRouter.post('/:id/views',
125 asyncMiddleware(videosGetValidator),
126 asyncMiddleware(viewVideo)
127 )
128
129 videosRouter.delete('/:id',
130 authenticate,
131 asyncMiddleware(videosRemoveValidator),
132 asyncMiddleware(removeVideoRetryWrapper)
133 )
134
135 // ---------------------------------------------------------------------------
136
137 export {
138 videosRouter
139 }
140
141 // ---------------------------------------------------------------------------
142
143 function listVideoCategories (req: express.Request, res: express.Response) {
144 res.json(VIDEO_CATEGORIES)
145 }
146
147 function listVideoLicences (req: express.Request, res: express.Response) {
148 res.json(VIDEO_LICENCES)
149 }
150
151 function listVideoLanguages (req: express.Request, res: express.Response) {
152 res.json(VIDEO_LANGUAGES)
153 }
154
155 function listVideoPrivacies (req: express.Request, res: express.Response) {
156 res.json(VIDEO_PRIVACIES)
157 }
158
159 // Wrapper to video add that retry the function if there is a database error
160 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
161 async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
162 const options = {
163 arguments: [ req, res, req.files['videofile'][0] ],
164 errorMessage: 'Cannot insert the video with many retries.'
165 }
166
167 const video = await retryTransactionWrapper(addVideo, options)
168
169 res.json({
170 video: {
171 id: video.id,
172 uuid: video.uuid
173 }
174 }).end()
175 }
176
177 async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
178 const videoInfo: VideoCreate = req.body
179
180 // Prepare data so we don't block the transaction
181 const videoData = {
182 name: videoInfo.name,
183 remote: false,
184 extname: extname(videoPhysicalFile.filename),
185 category: videoInfo.category,
186 licence: videoInfo.licence,
187 language: videoInfo.language,
188 nsfw: videoInfo.nsfw,
189 description: videoInfo.description,
190 privacy: videoInfo.privacy,
191 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
192 channelId: res.locals.videoChannel.id
193 }
194 const video = new VideoModel(videoData)
195 video.url = getVideoActivityPubUrl(video)
196
197 const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename)
198 const videoFileHeight = await getVideoFileHeight(videoFilePath)
199
200 const videoFileData = {
201 extname: extname(videoPhysicalFile.filename),
202 resolution: videoFileHeight,
203 size: videoPhysicalFile.size
204 }
205 const videoFile = new VideoFileModel(videoFileData)
206 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
207 const source = join(videoDir, videoPhysicalFile.filename)
208 const destination = join(videoDir, video.getVideoFilename(videoFile))
209
210 await renamePromise(source, destination)
211 // This is important in case if there is another attempt in the retry process
212 videoPhysicalFile.filename = video.getVideoFilename(videoFile)
213
214 const tasks = []
215
216 tasks.push(
217 video.createTorrentAndSetInfoHash(videoFile),
218 video.createThumbnail(videoFile),
219 video.createPreview(videoFile)
220 )
221 await Promise.all(tasks)
222
223 return sequelizeTypescript.transaction(async t => {
224 const sequelizeOptions = { transaction: t }
225
226 if (CONFIG.TRANSCODING.ENABLED === true) {
227 // Put uuid because we don't have id auto incremented for now
228 const dataInput = {
229 videoUUID: video.uuid
230 }
231
232 await transcodingJobScheduler.createJob(t, 'videoFileOptimizer', dataInput)
233 }
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
265 async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
266 const options = {
267 arguments: [ req, res ],
268 errorMessage: 'Cannot update the video with many retries.'
269 }
270
271 await retryTransactionWrapper(updateVideo, options)
272
273 return res.type('json').status(204).end()
274 }
275
276 async function updateVideo (req: express.Request, res: express.Response) {
277 const videoInstance: VideoModel = res.locals.video
278 const videoFieldsSave = videoInstance.toJSON()
279 const videoInfoToUpdate: VideoUpdate = req.body
280 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
281
282 try {
283 await sequelizeTypescript.transaction(async t => {
284 const sequelizeOptions = {
285 transaction: t
286 }
287
288 if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
289 if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
290 if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
291 if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
292 if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
293 if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', parseInt(videoInfoToUpdate.privacy.toString(), 10))
294 if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
295
296 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
297
298 if (videoInfoToUpdate.tags) {
299 const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
300
301 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
302 videoInstance.Tags = tagInstances
303 }
304
305 // Now we'll update the video's meta data to our friends
306 if (wasPrivateVideo === false) {
307 await sendUpdateVideo(videoInstanceUpdated, t)
308 }
309
310 // Video is not private anymore, send a create action to remote servers
311 if (wasPrivateVideo === true && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE) {
312 await sendCreateVideo(videoInstanceUpdated, t)
313 await shareVideoByServerAndChannel(videoInstanceUpdated, t)
314 }
315 })
316
317 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
318 } catch (err) {
319 // Force fields we want to update
320 // If the transaction is retried, sequelize will think the object has not changed
321 // So it will skip the SQL request, even if the last one was ROLLBACKed!
322 resetSequelizeInstance(videoInstance, videoFieldsSave)
323
324 throw err
325 }
326 }
327
328 function getVideo (req: express.Request, res: express.Response) {
329 const videoInstance = res.locals.video
330
331 return res.json(videoInstance.toFormattedDetailsJSON())
332 }
333
334 async function viewVideo (req: express.Request, res: express.Response) {
335 const videoInstance = res.locals.video
336
337 await videoInstance.increment('views')
338 const serverAccount = await getServerActor()
339
340 if (videoInstance.isOwned()) {
341 await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
342 } else {
343 await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
344 }
345
346 return res.status(204).end()
347 }
348
349 async function getVideoDescription (req: express.Request, res: express.Response) {
350 const videoInstance = res.locals.video
351 let description = ''
352
353 if (videoInstance.isOwned()) {
354 description = videoInstance.description
355 } else {
356 description = await fetchRemoteVideoDescription(videoInstance)
357 }
358
359 return res.json({ description })
360 }
361
362 async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
363 const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort)
364
365 return res.json(getFormattedObjects(resultList.data, resultList.total))
366 }
367
368 async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
369 const options = {
370 arguments: [ req, res ],
371 errorMessage: 'Cannot remove the video with many retries.'
372 }
373
374 await retryTransactionWrapper(removeVideo, options)
375
376 return res.type('json').status(204).end()
377 }
378
379 async function removeVideo (req: express.Request, res: express.Response) {
380 const videoInstance: VideoModel = res.locals.video
381
382 await sequelizeTypescript.transaction(async t => {
383 await videoInstance.destroy({ transaction: t })
384 })
385
386 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
387 }
388
389 async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
390 const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags(
391 req.query.search,
392 req.query.start,
393 req.query.count,
394 req.query.sort
395 )
396
397 return res.json(getFormattedObjects(resultList.data, resultList.total))
398 }