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,
13 } from '../../../initializers'
15 addEventToRemoteVideo,
16 quickAndDirtyUpdateVideoToFriends,
28 videosUpdateValidator,
29 videosSearchValidator,
32 videosRemoveValidator,
34 } from '../../../middlewares'
37 retryTransactionWrapper,
42 resetSequelizeInstance
43 } from '../../../helpers'
44 import { TagInstance, VideoInstance } from '../../../models'
45 import { VideoCreate, VideoUpdate } from '../../../../shared'
47 import { abuseVideoRouter } from './abuse'
48 import { blacklistRouter } from './blacklist'
49 import { rateVideoRouter } from './rate'
50 import { videoChannelRouter } from './channel'
52 const videosRouter = express.Router()
54 // multer configuration
55 const storage = multer.diskStorage({
56 destination: (req, file, cb) => {
57 cb(null, CONFIG.STORAGE.VIDEOS_DIR)
60 filename: (req, file, cb) => {
62 if (file.mimetype === 'video/webm') extension = 'webm'
63 else if (file.mimetype === 'video/mp4') extension = 'mp4'
64 else if (file.mimetype === 'video/ogg') extension = 'ogv'
65 generateRandomString(16)
66 .then(randomString => {
67 cb(null, randomString + '.' + extension)
70 logger.error('Cannot generate random string for file name.', err)
76 const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
78 videosRouter.use('/', abuseVideoRouter)
79 videosRouter.use('/', blacklistRouter)
80 videosRouter.use('/', rateVideoRouter)
81 videosRouter.use('/', videoChannelRouter)
83 videosRouter.get('/categories', listVideoCategories)
84 videosRouter.get('/licences', listVideoLicences)
85 videosRouter.get('/languages', listVideoLanguages)
92 asyncMiddleware(listVideos)
94 videosRouter.put('/:id',
96 videosUpdateValidator,
97 asyncMiddleware(updateVideoRetryWrapper)
99 videosRouter.post('/upload',
103 asyncMiddleware(addVideoRetryWrapper)
105 videosRouter.get('/:id',
110 videosRouter.delete('/:id',
112 videosRemoveValidator,
113 asyncMiddleware(removeVideoRetryWrapper)
116 videosRouter.get('/search/:value',
117 videosSearchValidator,
123 asyncMiddleware(searchVideos)
126 // ---------------------------------------------------------------------------
132 // ---------------------------------------------------------------------------
134 function listVideoCategories (req: express.Request, res: express.Response) {
135 res.json(VIDEO_CATEGORIES)
138 function listVideoLicences (req: express.Request, res: express.Response) {
139 res.json(VIDEO_LICENCES)
142 function listVideoLanguages (req: express.Request, res: express.Response) {
143 res.json(VIDEO_LANGUAGES)
146 // Wrapper to video add that retry the function if there is a database error
147 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
148 async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
150 arguments: [ req, res, req.files['videofile'][0] ],
151 errorMessage: 'Cannot insert the video with many retries.'
154 await retryTransactionWrapper(addVideo, options)
156 // TODO : include Location of the new video -> 201
157 res.type('json').status(204).end()
160 async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
161 const videoInfo: VideoCreate = req.body
164 await db.sequelize.transaction(async t => {
165 const sequelizeOptions = { transaction: t }
168 name: videoInfo.name,
170 extname: extname(videoPhysicalFile.filename),
171 category: videoInfo.category,
172 licence: videoInfo.licence,
173 language: videoInfo.language,
174 nsfw: videoInfo.nsfw,
175 description: videoInfo.description,
176 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
177 channelId: res.locals.videoChannel.id
179 const video = db.Video.build(videoData)
181 const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename)
182 const videoFileHeight = await getVideoFileHeight(videoFilePath)
184 const videoFileData = {
185 extname: extname(videoPhysicalFile.filename),
186 resolution: videoFileHeight,
187 size: videoPhysicalFile.size
189 const videoFile = db.VideoFile.build(videoFileData)
190 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
191 const source = join(videoDir, videoPhysicalFile.filename)
192 const destination = join(videoDir, video.getVideoFilename(videoFile))
194 await renamePromise(source, destination)
195 // This is important in case if there is another attempt in the retry process
196 videoPhysicalFile.filename = video.getVideoFilename(videoFile)
201 video.createTorrentAndSetInfoHash(videoFile),
202 video.createThumbnail(videoFile),
203 video.createPreview(videoFile)
206 if (CONFIG.TRANSCODING.ENABLED === true) {
207 // Put uuid because we don't have id auto incremented for now
209 videoUUID: video.uuid
213 JobScheduler.Instance.createJob(t, 'videoFileOptimizer', dataInput)
216 await Promise.all(tasks)
218 const videoCreated = await video.save(sequelizeOptions)
219 // Do not forget to add video channel information to the created video
220 videoCreated.VideoChannel = res.locals.videoChannel
221 videoUUID = videoCreated.uuid
223 videoFile.videoId = video.id
225 await videoFile.save(sequelizeOptions)
226 video.VideoFiles = [videoFile]
228 if (videoInfo.tags) {
229 const tagInstances = await db.Tag.findOrCreateTags(videoInfo.tags, t)
231 await video.setTags(tagInstances, sequelizeOptions)
232 video.Tags = tagInstances
235 // Let transcoding job send the video to friends because the video file extension might change
236 if (CONFIG.TRANSCODING.ENABLED === true) return undefined
238 const remoteVideo = await video.toAddRemoteJSON()
239 // Now we'll add the video's meta data to our friends
240 return addVideoToFriends(remoteVideo, t)
243 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoUUID)
246 async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
248 arguments: [ req, res ],
249 errorMessage: 'Cannot update the video with many retries.'
252 await retryTransactionWrapper(updateVideo, options)
254 return res.type('json').status(204).end()
257 async function updateVideo (req: express.Request, res: express.Response) {
258 const videoInstance = res.locals.video
259 const videoFieldsSave = videoInstance.toJSON()
260 const videoInfoToUpdate: VideoUpdate = req.body
263 await db.sequelize.transaction(async t => {
264 const sequelizeOptions = {
268 if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
269 if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
270 if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
271 if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
272 if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
273 if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
275 await videoInstance.save(sequelizeOptions)
277 if (videoInfoToUpdate.tags) {
278 const tagInstances = await db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t)
280 await videoInstance.setTags(tagInstances, sequelizeOptions)
281 videoInstance.Tags = tagInstances
284 const json = videoInstance.toUpdateRemoteJSON()
286 // Now we'll update the video's meta data to our friends
287 return updateVideoToFriends(json, t)
290 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
292 // Force fields we want to update
293 // If the transaction is retried, sequelize will think the object has not changed
294 // So it will skip the SQL request, even if the last one was ROLLBACKed!
295 resetSequelizeInstance(videoInstance, videoFieldsSave)
301 function getVideo (req: express.Request, res: express.Response) {
302 const videoInstance = res.locals.video
304 if (videoInstance.isOwned()) {
305 // The increment is done directly in the database, not using the instance value
306 // FIXME: make a real view system
307 // For example, only add a view when a user watch a video during 30s etc
308 videoInstance.increment('views')
311 videoId: videoInstance.id,
312 type: REQUEST_VIDEO_QADU_TYPES.VIEWS
314 return quickAndDirtyUpdateVideoToFriends(qaduParams)
316 .catch(err => logger.error('Cannot add view to video %s.', videoInstance.uuid, err))
318 // Just send the event to our friends
319 const eventParams = {
320 videoId: videoInstance.id,
321 type: REQUEST_VIDEO_EVENT_TYPES.VIEWS
323 addEventToRemoteVideo(eventParams)
324 .catch(err => logger.error('Cannot add event to remote video %s.', videoInstance.uuid, err))
327 // Do not wait the view system
328 return res.json(videoInstance.toFormattedDetailsJSON())
331 async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
332 const resultList = await db.Video.listForApi(req.query.start, req.query.count, req.query.sort)
334 return res.json(getFormattedObjects(resultList.data, resultList.total))
337 async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
339 arguments: [ req, res ],
340 errorMessage: 'Cannot remove the video with many retries.'
343 await retryTransactionWrapper(removeVideo, options)
345 return res.type('json').status(204).end()
348 async function removeVideo (req: express.Request, res: express.Response) {
349 const videoInstance: VideoInstance = res.locals.video
351 await db.sequelize.transaction(async t => {
352 await videoInstance.destroy({ transaction: t })
355 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
358 async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
359 const resultList = await db.Video.searchAndPopulateAuthorAndPodAndTags(
367 return res.json(getFormattedObjects(resultList.data, resultList.total))