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 { renamePromise } from '../../../helpers/core-utils'
6 import { retryTransactionWrapper } from '../../../helpers/database-utils'
7 import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils'
8 import { logger } from '../../../helpers/logger'
9 import { createReqFiles, generateRandomString, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils'
11 CONFIG, sequelizeTypescript, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT,
13 } from '../../../initializers'
14 import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub'
15 import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
16 import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler'
18 asyncMiddleware, authenticate, paginationValidator, setPagination, setVideosSort, videosAddValidator, videosGetValidator,
19 videosRemoveValidator, videosSearchValidator, videosSortValidator, videosUpdateValidator
20 } from '../../../middlewares'
21 import { TagModel } from '../../../models/video/tag'
22 import { VideoModel } from '../../../models/video/video'
23 import { VideoFileModel } from '../../../models/video/video-file'
24 import { abuseVideoRouter } from './abuse'
25 import { blacklistRouter } from './blacklist'
26 import { videoChannelRouter } from './channel'
27 import { videoCommentRouter } from './comment'
28 import { rateVideoRouter } from './rate'
30 const videosRouter = express.Router()
32 const reqVideoFile = createReqFiles('videofile', CONFIG.STORAGE.VIDEOS_DIR, VIDEO_MIMETYPE_EXT)
34 videosRouter.use('/', abuseVideoRouter)
35 videosRouter.use('/', blacklistRouter)
36 videosRouter.use('/', rateVideoRouter)
37 videosRouter.use('/', videoChannelRouter)
38 videosRouter.use('/', videoCommentRouter)
40 videosRouter.get('/categories', listVideoCategories)
41 videosRouter.get('/licences', listVideoLicences)
42 videosRouter.get('/languages', listVideoLanguages)
43 videosRouter.get('/privacies', listVideoPrivacies)
50 asyncMiddleware(listVideos)
52 videosRouter.get('/search',
53 videosSearchValidator,
58 asyncMiddleware(searchVideos)
60 videosRouter.put('/:id',
62 asyncMiddleware(videosUpdateValidator),
63 asyncMiddleware(updateVideoRetryWrapper)
65 videosRouter.post('/upload',
68 asyncMiddleware(videosAddValidator),
69 asyncMiddleware(addVideoRetryWrapper)
72 videosRouter.get('/:id/description',
73 asyncMiddleware(videosGetValidator),
74 asyncMiddleware(getVideoDescription)
76 videosRouter.get('/:id',
77 asyncMiddleware(videosGetValidator),
80 videosRouter.post('/:id/views',
81 asyncMiddleware(videosGetValidator),
82 asyncMiddleware(viewVideo)
85 videosRouter.delete('/:id',
87 asyncMiddleware(videosRemoveValidator),
88 asyncMiddleware(removeVideoRetryWrapper)
91 // ---------------------------------------------------------------------------
97 // ---------------------------------------------------------------------------
99 function listVideoCategories (req: express.Request, res: express.Response) {
100 res.json(VIDEO_CATEGORIES)
103 function listVideoLicences (req: express.Request, res: express.Response) {
104 res.json(VIDEO_LICENCES)
107 function listVideoLanguages (req: express.Request, res: express.Response) {
108 res.json(VIDEO_LANGUAGES)
111 function listVideoPrivacies (req: express.Request, res: express.Response) {
112 res.json(VIDEO_PRIVACIES)
115 // Wrapper to video add that retry the function if there is a database error
116 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
117 async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
119 arguments: [ req, res, req.files['videofile'][0] ],
120 errorMessage: 'Cannot insert the video with many retries.'
123 const video = await retryTransactionWrapper(addVideo, options)
133 async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
134 const videoInfo: VideoCreate = req.body
136 // Prepare data so we don't block the transaction
138 name: videoInfo.name,
140 extname: extname(videoPhysicalFile.filename),
141 category: videoInfo.category,
142 licence: videoInfo.licence,
143 language: videoInfo.language,
144 nsfw: videoInfo.nsfw,
145 description: videoInfo.description,
146 privacy: videoInfo.privacy,
147 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
148 channelId: res.locals.videoChannel.id
150 const video = new VideoModel(videoData)
151 video.url = getVideoActivityPubUrl(video)
153 const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename)
154 const videoFileHeight = await getVideoFileHeight(videoFilePath)
156 const videoFileData = {
157 extname: extname(videoPhysicalFile.filename),
158 resolution: videoFileHeight,
159 size: videoPhysicalFile.size
161 const videoFile = new VideoFileModel(videoFileData)
162 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
163 const source = join(videoDir, videoPhysicalFile.filename)
164 const destination = join(videoDir, video.getVideoFilename(videoFile))
166 await renamePromise(source, destination)
167 // This is important in case if there is another attempt in the retry process
168 videoPhysicalFile.filename = video.getVideoFilename(videoFile)
173 video.createTorrentAndSetInfoHash(videoFile),
174 video.createThumbnail(videoFile),
175 video.createPreview(videoFile)
177 await Promise.all(tasks)
179 return sequelizeTypescript.transaction(async t => {
180 const sequelizeOptions = { transaction: t }
182 if (CONFIG.TRANSCODING.ENABLED === true) {
183 // Put uuid because we don't have id auto incremented for now
185 videoUUID: video.uuid
188 await transcodingJobScheduler.createJob(t, 'videoFileOptimizer', dataInput)
191 const videoCreated = await video.save(sequelizeOptions)
192 // Do not forget to add video channel information to the created video
193 videoCreated.VideoChannel = res.locals.videoChannel
195 videoFile.videoId = video.id
196 await videoFile.save(sequelizeOptions)
198 video.VideoFiles = [ videoFile ]
200 if (videoInfo.tags) {
201 const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t)
203 await video.$set('Tags', tagInstances, sequelizeOptions)
204 video.Tags = tagInstances
207 // Let transcoding job send the video to friends because the video file extension might change
208 if (CONFIG.TRANSCODING.ENABLED === true) return videoCreated
209 // Don't send video to remote servers, it is private
210 if (video.privacy === VideoPrivacy.PRIVATE) return videoCreated
212 await sendCreateVideo(video, t)
213 await shareVideoByServerAndChannel(video, t)
215 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
221 async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
223 arguments: [ req, res ],
224 errorMessage: 'Cannot update the video with many retries.'
227 await retryTransactionWrapper(updateVideo, options)
229 return res.type('json').status(204).end()
232 async function updateVideo (req: express.Request, res: express.Response) {
233 const videoInstance: VideoModel = res.locals.video
234 const videoFieldsSave = videoInstance.toJSON()
235 const videoInfoToUpdate: VideoUpdate = req.body
236 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
239 await sequelizeTypescript.transaction(async t => {
240 const sequelizeOptions = {
244 if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
245 if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
246 if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
247 if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
248 if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
249 if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', parseInt(videoInfoToUpdate.privacy.toString(), 10))
250 if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
252 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
254 if (videoInfoToUpdate.tags) {
255 const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
257 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
258 videoInstance.Tags = tagInstances
261 // Now we'll update the video's meta data to our friends
262 if (wasPrivateVideo === false) {
263 await sendUpdateVideo(videoInstanceUpdated, t)
266 // Video is not private anymore, send a create action to remote servers
267 if (wasPrivateVideo === true && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE) {
268 await sendCreateVideo(videoInstanceUpdated, t)
269 await shareVideoByServerAndChannel(videoInstanceUpdated, t)
273 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
275 // Force fields we want to update
276 // If the transaction is retried, sequelize will think the object has not changed
277 // So it will skip the SQL request, even if the last one was ROLLBACKed!
278 resetSequelizeInstance(videoInstance, videoFieldsSave)
284 function getVideo (req: express.Request, res: express.Response) {
285 const videoInstance = res.locals.video
287 return res.json(videoInstance.toFormattedDetailsJSON())
290 async function viewVideo (req: express.Request, res: express.Response) {
291 const videoInstance = res.locals.video
293 await videoInstance.increment('views')
294 const serverAccount = await getServerActor()
296 if (videoInstance.isOwned()) {
297 await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
299 await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
302 return res.status(204).end()
305 async function getVideoDescription (req: express.Request, res: express.Response) {
306 const videoInstance = res.locals.video
309 if (videoInstance.isOwned()) {
310 description = videoInstance.description
312 description = await fetchRemoteVideoDescription(videoInstance)
315 return res.json({ description })
318 async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
319 const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort)
321 return res.json(getFormattedObjects(resultList.data, resultList.total))
324 async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
326 arguments: [ req, res ],
327 errorMessage: 'Cannot remove the video with many retries.'
330 await retryTransactionWrapper(removeVideo, options)
332 return res.type('json').status(204).end()
335 async function removeVideo (req: express.Request, res: express.Response) {
336 const videoInstance: VideoModel = res.locals.video
338 await sequelizeTypescript.transaction(async t => {
339 await videoInstance.destroy({ transaction: t })
342 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
345 async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
346 const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags(
353 return res.json(getFormattedObjects(resultList.data, resultList.total))