]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/index.ts
9ad84609f212986c94735b1d8217c79a4d227c7d
[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
5 import { database as db } from '../../../initializers/database'
6 import {
7 CONFIG,
8 REQUEST_VIDEO_QADU_TYPES,
9 REQUEST_VIDEO_EVENT_TYPES,
10 VIDEO_CATEGORIES,
11 VIDEO_LICENCES,
12 VIDEO_LANGUAGES,
13 VIDEO_PRIVACIES,
14 VIDEO_MIMETYPE_EXT
15 } from '../../../initializers'
16 import {
17 addEventToRemoteVideo,
18 quickAndDirtyUpdateVideoToFriends,
19 addVideoToFriends,
20 updateVideoToFriends,
21 JobScheduler,
22 fetchRemoteDescription
23 } from '../../../lib'
24 import {
25 authenticate,
26 paginationValidator,
27 videosSortValidator,
28 setVideosSort,
29 setPagination,
30 setVideosSearch,
31 videosUpdateValidator,
32 videosSearchValidator,
33 videosAddValidator,
34 videosGetValidator,
35 videosRemoveValidator,
36 asyncMiddleware
37 } from '../../../middlewares'
38 import {
39 logger,
40 retryTransactionWrapper,
41 generateRandomString,
42 getFormattedObjects,
43 renamePromise,
44 getVideoFileHeight,
45 resetSequelizeInstance
46 } from '../../../helpers'
47 import { VideoInstance } from '../../../models'
48 import { VideoCreate, VideoUpdate, VideoPrivacy } from '../../../../shared'
49
50 import { abuseVideoRouter } from './abuse'
51 import { blacklistRouter } from './blacklist'
52 import { rateVideoRouter } from './rate'
53 import { videoChannelRouter } from './channel'
54 import { getActivityPubUrl } from '../../../helpers/activitypub'
55
56 const videosRouter = express.Router()
57
58 // multer configuration
59 const storage = multer.diskStorage({
60 destination: (req, file, cb) => {
61 cb(null, CONFIG.STORAGE.VIDEOS_DIR)
62 },
63
64 filename: async (req, file, cb) => {
65 const extension = VIDEO_MIMETYPE_EXT[file.mimetype]
66 let randomString = ''
67
68 try {
69 randomString = await generateRandomString(16)
70 } catch (err) {
71 logger.error('Cannot generate random string for file name.', err)
72 randomString = 'fake-random-string'
73 }
74
75 cb(null, randomString + '.' + extension)
76 }
77 })
78
79 const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
80
81 videosRouter.use('/', abuseVideoRouter)
82 videosRouter.use('/', blacklistRouter)
83 videosRouter.use('/', rateVideoRouter)
84 videosRouter.use('/', videoChannelRouter)
85
86 videosRouter.get('/categories', listVideoCategories)
87 videosRouter.get('/licences', listVideoLicences)
88 videosRouter.get('/languages', listVideoLanguages)
89 videosRouter.get('/privacies', listVideoPrivacies)
90
91 videosRouter.get('/',
92 paginationValidator,
93 videosSortValidator,
94 setVideosSort,
95 setPagination,
96 asyncMiddleware(listVideos)
97 )
98 videosRouter.put('/:id',
99 authenticate,
100 videosUpdateValidator,
101 asyncMiddleware(updateVideoRetryWrapper)
102 )
103 videosRouter.post('/upload',
104 authenticate,
105 reqFiles,
106 videosAddValidator,
107 asyncMiddleware(addVideoRetryWrapper)
108 )
109
110 videosRouter.get('/:id/description',
111 videosGetValidator,
112 asyncMiddleware(getVideoDescription)
113 )
114 videosRouter.get('/:id',
115 videosGetValidator,
116 getVideo
117 )
118
119 videosRouter.delete('/:id',
120 authenticate,
121 videosRemoveValidator,
122 asyncMiddleware(removeVideoRetryWrapper)
123 )
124
125 videosRouter.get('/search/:value',
126 videosSearchValidator,
127 paginationValidator,
128 videosSortValidator,
129 setVideosSort,
130 setPagination,
131 setVideosSearch,
132 asyncMiddleware(searchVideos)
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 await retryTransactionWrapper(addVideo, options)
168
169 // TODO : include Location of the new video -> 201
170 res.type('json').status(204).end()
171 }
172
173 async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
174 const videoInfo: VideoCreate = req.body
175 let videoUUID = ''
176
177 await db.sequelize.transaction(async t => {
178 const sequelizeOptions = { transaction: t }
179
180 const videoData = {
181 name: videoInfo.name,
182 remote: false,
183 extname: extname(videoPhysicalFile.filename),
184 category: videoInfo.category,
185 licence: videoInfo.licence,
186 language: videoInfo.language,
187 nsfw: videoInfo.nsfw,
188 description: videoInfo.description,
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 = db.Video.build(videoData)
194 video.url = getActivityPubUrl('video', video.uuid)
195
196 const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename)
197 const videoFileHeight = await getVideoFileHeight(videoFilePath)
198
199 const videoFileData = {
200 extname: extname(videoPhysicalFile.filename),
201 resolution: videoFileHeight,
202 size: videoPhysicalFile.size
203 }
204 const videoFile = db.VideoFile.build(videoFileData)
205 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
206 const source = join(videoDir, videoPhysicalFile.filename)
207 const destination = join(videoDir, video.getVideoFilename(videoFile))
208
209 await renamePromise(source, destination)
210 // This is important in case if there is another attempt in the retry process
211 videoPhysicalFile.filename = video.getVideoFilename(videoFile)
212
213 const tasks = []
214
215 tasks.push(
216 video.createTorrentAndSetInfoHash(videoFile),
217 video.createThumbnail(videoFile),
218 video.createPreview(videoFile)
219 )
220
221 if (CONFIG.TRANSCODING.ENABLED === true) {
222 // Put uuid because we don't have id auto incremented for now
223 const dataInput = {
224 videoUUID: video.uuid
225 }
226
227 tasks.push(
228 JobScheduler.Instance.createJob(t, 'videoFileOptimizer', dataInput)
229 )
230 }
231 await Promise.all(tasks)
232
233 const videoCreated = await video.save(sequelizeOptions)
234 // Do not forget to add video channel information to the created video
235 videoCreated.VideoChannel = res.locals.videoChannel
236 videoUUID = videoCreated.uuid
237
238 videoFile.videoId = video.id
239
240 await videoFile.save(sequelizeOptions)
241 video.VideoFiles = [videoFile]
242
243 if (videoInfo.tags) {
244 const tagInstances = await db.Tag.findOrCreateTags(videoInfo.tags, t)
245
246 await video.setTags(tagInstances, sequelizeOptions)
247 video.Tags = tagInstances
248 }
249
250 // Let transcoding job send the video to friends because the video file extension might change
251 if (CONFIG.TRANSCODING.ENABLED === true) return undefined
252 // Don't send video to remote pods, it is private
253 if (video.privacy === VideoPrivacy.PRIVATE) return undefined
254
255 const remoteVideo = await video.toAddRemoteJSON()
256 // Now we'll add the video's meta data to our friends
257 return addVideoToFriends(remoteVideo, t)
258 })
259
260 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoUUID)
261 }
262
263 async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
264 const options = {
265 arguments: [ req, res ],
266 errorMessage: 'Cannot update the video with many retries.'
267 }
268
269 await retryTransactionWrapper(updateVideo, options)
270
271 return res.type('json').status(204).end()
272 }
273
274 async function updateVideo (req: express.Request, res: express.Response) {
275 const videoInstance = res.locals.video
276 const videoFieldsSave = videoInstance.toJSON()
277 const videoInfoToUpdate: VideoUpdate = req.body
278 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
279
280 try {
281 await db.sequelize.transaction(async t => {
282 const sequelizeOptions = {
283 transaction: t
284 }
285
286 if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
287 if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
288 if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
289 if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
290 if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
291 if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', videoInfoToUpdate.privacy)
292 if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
293
294 await videoInstance.save(sequelizeOptions)
295
296 if (videoInfoToUpdate.tags) {
297 const tagInstances = await db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t)
298
299 await videoInstance.setTags(tagInstances, sequelizeOptions)
300 videoInstance.Tags = tagInstances
301 }
302
303 // Now we'll update the video's meta data to our friends
304 if (wasPrivateVideo === false) {
305 const json = videoInstance.toUpdateRemoteJSON()
306 return updateVideoToFriends(json, t)
307 }
308
309 // Video is not private anymore, send a create action to remote pods
310 if (wasPrivateVideo === true && videoInstance.privacy !== VideoPrivacy.PRIVATE) {
311 const remoteVideo = await videoInstance.toAddRemoteJSON()
312 return addVideoToFriends(remoteVideo, t)
313 }
314 })
315
316 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
317 } catch (err) {
318 // Force fields we want to update
319 // If the transaction is retried, sequelize will think the object has not changed
320 // So it will skip the SQL request, even if the last one was ROLLBACKed!
321 resetSequelizeInstance(videoInstance, videoFieldsSave)
322
323 throw err
324 }
325 }
326
327 function getVideo (req: express.Request, res: express.Response) {
328 const videoInstance = res.locals.video
329
330 if (videoInstance.isOwned()) {
331 // The increment is done directly in the database, not using the instance value
332 // FIXME: make a real view system
333 // For example, only add a view when a user watch a video during 30s etc
334 videoInstance.increment('views')
335 .then(() => {
336 const qaduParams = {
337 videoId: videoInstance.id,
338 type: REQUEST_VIDEO_QADU_TYPES.VIEWS
339 }
340 return quickAndDirtyUpdateVideoToFriends(qaduParams)
341 })
342 .catch(err => logger.error('Cannot add view to video %s.', videoInstance.uuid, err))
343 } else {
344 // Just send the event to our friends
345 const eventParams = {
346 videoId: videoInstance.id,
347 type: REQUEST_VIDEO_EVENT_TYPES.VIEWS
348 }
349 addEventToRemoteVideo(eventParams)
350 .catch(err => logger.error('Cannot add event to remote video %s.', videoInstance.uuid, err))
351 }
352
353 // Do not wait the view system
354 return res.json(videoInstance.toFormattedDetailsJSON())
355 }
356
357 async function getVideoDescription (req: express.Request, res: express.Response) {
358 const videoInstance = res.locals.video
359 let description = ''
360
361 if (videoInstance.isOwned()) {
362 description = videoInstance.description
363 } else {
364 description = await fetchRemoteDescription(videoInstance)
365 }
366
367 return res.json({ description })
368 }
369
370 async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
371 const resultList = await db.Video.listForApi(req.query.start, req.query.count, req.query.sort)
372
373 return res.json(getFormattedObjects(resultList.data, resultList.total))
374 }
375
376 async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
377 const options = {
378 arguments: [ req, res ],
379 errorMessage: 'Cannot remove the video with many retries.'
380 }
381
382 await retryTransactionWrapper(removeVideo, options)
383
384 return res.type('json').status(204).end()
385 }
386
387 async function removeVideo (req: express.Request, res: express.Response) {
388 const videoInstance: VideoInstance = res.locals.video
389
390 await db.sequelize.transaction(async t => {
391 await videoInstance.destroy({ transaction: t })
392 })
393
394 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
395 }
396
397 async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
398 const resultList = await db.Video.searchAndPopulateAccountAndPodAndTags(
399 req.params.value,
400 req.query.field,
401 req.query.start,
402 req.query.count,
403 req.query.sort
404 )
405
406 return res.json(getFormattedObjects(resultList.data, resultList.total))
407 }