1 import * as express from 'express'
2 import * as multer from 'multer'
3 import { extname, join } from 'path'
5 import { database as db } from '../../../initializers/database'
8 REQUEST_VIDEO_QADU_TYPES,
9 REQUEST_VIDEO_EVENT_TYPES,
15 } from '../../../initializers'
17 addEventToRemoteVideo,
18 quickAndDirtyUpdateVideoToFriends,
22 fetchRemoteDescription
31 videosUpdateValidator,
32 videosSearchValidator,
35 videosRemoveValidator,
37 } from '../../../middlewares'
40 retryTransactionWrapper,
45 resetSequelizeInstance
46 } from '../../../helpers'
47 import { VideoInstance } from '../../../models'
48 import { VideoCreate, VideoUpdate, VideoPrivacy } from '../../../../shared'
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'
56 const videosRouter = express.Router()
58 // multer configuration
59 const storage = multer.diskStorage({
60 destination: (req, file, cb) => {
61 cb(null, CONFIG.STORAGE.VIDEOS_DIR)
64 filename: async (req, file, cb) => {
65 const extension = VIDEO_MIMETYPE_EXT[file.mimetype]
69 randomString = await generateRandomString(16)
71 logger.error('Cannot generate random string for file name.', err)
72 randomString = 'fake-random-string'
75 cb(null, randomString + '.' + extension)
79 const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
81 videosRouter.use('/', abuseVideoRouter)
82 videosRouter.use('/', blacklistRouter)
83 videosRouter.use('/', rateVideoRouter)
84 videosRouter.use('/', videoChannelRouter)
86 videosRouter.get('/categories', listVideoCategories)
87 videosRouter.get('/licences', listVideoLicences)
88 videosRouter.get('/languages', listVideoLanguages)
89 videosRouter.get('/privacies', listVideoPrivacies)
96 asyncMiddleware(listVideos)
98 videosRouter.put('/:id',
100 videosUpdateValidator,
101 asyncMiddleware(updateVideoRetryWrapper)
103 videosRouter.post('/upload',
107 asyncMiddleware(addVideoRetryWrapper)
110 videosRouter.get('/:id/description',
112 asyncMiddleware(getVideoDescription)
114 videosRouter.get('/:id',
119 videosRouter.delete('/:id',
121 videosRemoveValidator,
122 asyncMiddleware(removeVideoRetryWrapper)
125 videosRouter.get('/search/:value',
126 videosSearchValidator,
132 asyncMiddleware(searchVideos)
135 // ---------------------------------------------------------------------------
141 // ---------------------------------------------------------------------------
143 function listVideoCategories (req: express.Request, res: express.Response) {
144 res.json(VIDEO_CATEGORIES)
147 function listVideoLicences (req: express.Request, res: express.Response) {
148 res.json(VIDEO_LICENCES)
151 function listVideoLanguages (req: express.Request, res: express.Response) {
152 res.json(VIDEO_LANGUAGES)
155 function listVideoPrivacies (req: express.Request, res: express.Response) {
156 res.json(VIDEO_PRIVACIES)
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) {
163 arguments: [ req, res, req.files['videofile'][0] ],
164 errorMessage: 'Cannot insert the video with many retries.'
167 await retryTransactionWrapper(addVideo, options)
169 // TODO : include Location of the new video -> 201
170 res.type('json').status(204).end()
173 async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
174 const videoInfo: VideoCreate = req.body
177 await db.sequelize.transaction(async t => {
178 const sequelizeOptions = { transaction: t }
181 name: videoInfo.name,
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
193 const video = db.Video.build(videoData)
194 video.url = getActivityPubUrl('video', video.uuid)
196 const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename)
197 const videoFileHeight = await getVideoFileHeight(videoFilePath)
199 const videoFileData = {
200 extname: extname(videoPhysicalFile.filename),
201 resolution: videoFileHeight,
202 size: videoPhysicalFile.size
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))
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)
216 video.createTorrentAndSetInfoHash(videoFile),
217 video.createThumbnail(videoFile),
218 video.createPreview(videoFile)
221 if (CONFIG.TRANSCODING.ENABLED === true) {
222 // Put uuid because we don't have id auto incremented for now
224 videoUUID: video.uuid
228 JobScheduler.Instance.createJob(t, 'videoFileOptimizer', dataInput)
231 await Promise.all(tasks)
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
238 videoFile.videoId = video.id
240 await videoFile.save(sequelizeOptions)
241 video.VideoFiles = [videoFile]
243 if (videoInfo.tags) {
244 const tagInstances = await db.Tag.findOrCreateTags(videoInfo.tags, t)
246 await video.setTags(tagInstances, sequelizeOptions)
247 video.Tags = tagInstances
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
255 const remoteVideo = await video.toAddRemoteJSON()
256 // Now we'll add the video's meta data to our friends
257 return addVideoToFriends(remoteVideo, t)
260 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoUUID)
263 async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
265 arguments: [ req, res ],
266 errorMessage: 'Cannot update the video with many retries.'
269 await retryTransactionWrapper(updateVideo, options)
271 return res.type('json').status(204).end()
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
281 await db.sequelize.transaction(async t => {
282 const sequelizeOptions = {
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)
294 await videoInstance.save(sequelizeOptions)
296 if (videoInfoToUpdate.tags) {
297 const tagInstances = await db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t)
299 await videoInstance.setTags(tagInstances, sequelizeOptions)
300 videoInstance.Tags = tagInstances
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)
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)
316 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
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)
327 function getVideo (req: express.Request, res: express.Response) {
328 const videoInstance = res.locals.video
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')
337 videoId: videoInstance.id,
338 type: REQUEST_VIDEO_QADU_TYPES.VIEWS
340 return quickAndDirtyUpdateVideoToFriends(qaduParams)
342 .catch(err => logger.error('Cannot add view to video %s.', videoInstance.uuid, err))
344 // Just send the event to our friends
345 const eventParams = {
346 videoId: videoInstance.id,
347 type: REQUEST_VIDEO_EVENT_TYPES.VIEWS
349 addEventToRemoteVideo(eventParams)
350 .catch(err => logger.error('Cannot add event to remote video %s.', videoInstance.uuid, err))
353 // Do not wait the view system
354 return res.json(videoInstance.toFormattedDetailsJSON())
357 async function getVideoDescription (req: express.Request, res: express.Response) {
358 const videoInstance = res.locals.video
361 if (videoInstance.isOwned()) {
362 description = videoInstance.description
364 description = await fetchRemoteDescription(videoInstance)
367 return res.json({ description })
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)
373 return res.json(getFormattedObjects(resultList.data, resultList.total))
376 async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
378 arguments: [ req, res ],
379 errorMessage: 'Cannot remove the video with many retries.'
382 await retryTransactionWrapper(removeVideo, options)
384 return res.type('json').status(204).end()
387 async function removeVideo (req: express.Request, res: express.Response) {
388 const videoInstance: VideoInstance = res.locals.video
390 await db.sequelize.transaction(async t => {
391 await videoInstance.destroy({ transaction: t })
394 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
397 async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
398 const resultList = await db.Video.searchAndPopulateAccountAndPodAndTags(
406 return res.json(getFormattedObjects(resultList.data, resultList.total))