1 import * as express from 'express'
2 import { extname, join } from 'path'
3 import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared'
4 import { renamePromise } from '../../../helpers/core-utils'
5 import { retryTransactionWrapper } from '../../../helpers/database-utils'
6 import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils'
7 import { logger } from '../../../helpers/logger'
8 import { createReqFiles, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils'
10 CONFIG, sequelizeTypescript, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT,
12 } from '../../../initializers'
13 import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub'
14 import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
15 import { JobQueue } from '../../../lib/job-queue'
17 asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setDefaultPagination, videosAddValidator, videosGetValidator,
18 videosRemoveValidator, videosSearchValidator, videosSortValidator, videosUpdateValidator
19 } from '../../../middlewares'
20 import { TagModel } from '../../../models/video/tag'
21 import { VideoModel } from '../../../models/video/video'
22 import { VideoFileModel } from '../../../models/video/video-file'
23 import { abuseVideoRouter } from './abuse'
24 import { blacklistRouter } from './blacklist'
25 import { videoChannelRouter } from './channel'
26 import { videoCommentRouter } from './comment'
27 import { rateVideoRouter } from './rate'
29 const videosRouter = express.Router()
31 const reqVideoFile = createReqFiles('videofile', CONFIG.STORAGE.VIDEOS_DIR, VIDEO_MIMETYPE_EXT)
33 videosRouter.use('/', abuseVideoRouter)
34 videosRouter.use('/', blacklistRouter)
35 videosRouter.use('/', rateVideoRouter)
36 videosRouter.use('/', videoChannelRouter)
37 videosRouter.use('/', videoCommentRouter)
39 videosRouter.get('/categories', listVideoCategories)
40 videosRouter.get('/licences', listVideoLicences)
41 videosRouter.get('/languages', listVideoLanguages)
42 videosRouter.get('/privacies', listVideoPrivacies)
49 asyncMiddleware(listVideos)
51 videosRouter.get('/search',
52 videosSearchValidator,
57 asyncMiddleware(searchVideos)
59 videosRouter.put('/:id',
61 asyncMiddleware(videosUpdateValidator),
62 asyncMiddleware(updateVideoRetryWrapper)
64 videosRouter.post('/upload',
67 asyncMiddleware(videosAddValidator),
68 asyncMiddleware(addVideoRetryWrapper)
71 videosRouter.get('/:id/description',
72 asyncMiddleware(videosGetValidator),
73 asyncMiddleware(getVideoDescription)
75 videosRouter.get('/:id',
76 asyncMiddleware(videosGetValidator),
79 videosRouter.post('/:id/views',
80 asyncMiddleware(videosGetValidator),
81 asyncMiddleware(viewVideo)
84 videosRouter.delete('/:id',
86 asyncMiddleware(videosRemoveValidator),
87 asyncMiddleware(removeVideoRetryWrapper)
90 // ---------------------------------------------------------------------------
96 // ---------------------------------------------------------------------------
98 function listVideoCategories (req: express.Request, res: express.Response) {
99 res.json(VIDEO_CATEGORIES)
102 function listVideoLicences (req: express.Request, res: express.Response) {
103 res.json(VIDEO_LICENCES)
106 function listVideoLanguages (req: express.Request, res: express.Response) {
107 res.json(VIDEO_LANGUAGES)
110 function listVideoPrivacies (req: express.Request, res: express.Response) {
111 res.json(VIDEO_PRIVACIES)
114 // Wrapper to video add that retry the function if there is a database error
115 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
116 async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
118 arguments: [ req, res, req.files['videofile'][0] ],
119 errorMessage: 'Cannot insert the video with many retries.'
122 const video = await retryTransactionWrapper(addVideo, options)
132 async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
133 const videoInfo: VideoCreate = req.body
135 // Prepare data so we don't block the transaction
137 name: videoInfo.name,
139 extname: extname(videoPhysicalFile.filename),
140 category: videoInfo.category,
141 licence: videoInfo.licence,
142 language: videoInfo.language,
143 commentsEnabled: videoInfo.commentsEnabled,
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 const videoCreated = await sequelizeTypescript.transaction(async t => {
180 const sequelizeOptions = { transaction: t }
182 const videoCreated = await video.save(sequelizeOptions)
183 // Do not forget to add video channel information to the created video
184 videoCreated.VideoChannel = res.locals.videoChannel
186 videoFile.videoId = video.id
187 await videoFile.save(sequelizeOptions)
189 video.VideoFiles = [ videoFile ]
191 if (videoInfo.tags) {
192 const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t)
194 await video.$set('Tags', tagInstances, sequelizeOptions)
195 video.Tags = tagInstances
198 // Let transcoding job send the video to friends because the video file extension might change
199 if (CONFIG.TRANSCODING.ENABLED === true) return videoCreated
200 // Don't send video to remote servers, it is private
201 if (video.privacy === VideoPrivacy.PRIVATE) return videoCreated
203 await sendCreateVideo(video, t)
204 await shareVideoByServerAndChannel(video, t)
206 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
211 if (CONFIG.TRANSCODING.ENABLED === true) {
212 // Put uuid because we don't have id auto incremented for now
214 videoUUID: videoCreated.uuid
217 await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
223 async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
225 arguments: [ req, res ],
226 errorMessage: 'Cannot update the video with many retries.'
229 await retryTransactionWrapper(updateVideo, options)
231 return res.type('json').status(204).end()
234 async function updateVideo (req: express.Request, res: express.Response) {
235 const videoInstance: VideoModel = res.locals.video
236 const videoFieldsSave = videoInstance.toJSON()
237 const videoInfoToUpdate: VideoUpdate = req.body
238 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
241 await sequelizeTypescript.transaction(async t => {
242 const sequelizeOptions = {
246 if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
247 if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
248 if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
249 if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
250 if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
251 if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', parseInt(videoInfoToUpdate.privacy.toString(), 10))
252 if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
253 if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled)
255 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
257 if (videoInfoToUpdate.tags) {
258 const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
260 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
261 videoInstance.Tags = tagInstances
264 // Now we'll update the video's meta data to our friends
265 if (wasPrivateVideo === false) {
266 await sendUpdateVideo(videoInstanceUpdated, t)
269 // Video is not private anymore, send a create action to remote servers
270 if (wasPrivateVideo === true && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE) {
271 await sendCreateVideo(videoInstanceUpdated, t)
272 await shareVideoByServerAndChannel(videoInstanceUpdated, t)
276 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
278 // Force fields we want to update
279 // If the transaction is retried, sequelize will think the object has not changed
280 // So it will skip the SQL request, even if the last one was ROLLBACKed!
281 resetSequelizeInstance(videoInstance, videoFieldsSave)
287 function getVideo (req: express.Request, res: express.Response) {
288 const videoInstance = res.locals.video
290 return res.json(videoInstance.toFormattedDetailsJSON())
293 async function viewVideo (req: express.Request, res: express.Response) {
294 const videoInstance = res.locals.video
296 await videoInstance.increment('views')
297 const serverAccount = await getServerActor()
299 if (videoInstance.isOwned()) {
300 await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
302 await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
305 return res.status(204).end()
308 async function getVideoDescription (req: express.Request, res: express.Response) {
309 const videoInstance = res.locals.video
312 if (videoInstance.isOwned()) {
313 description = videoInstance.description
315 description = await fetchRemoteVideoDescription(videoInstance)
318 return res.json({ description })
321 async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
322 const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort)
324 return res.json(getFormattedObjects(resultList.data, resultList.total))
327 async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
329 arguments: [ req, res ],
330 errorMessage: 'Cannot remove the video with many retries.'
333 await retryTransactionWrapper(removeVideo, options)
335 return res.type('json').status(204).end()
338 async function removeVideo (req: express.Request, res: express.Response) {
339 const videoInstance: VideoModel = res.locals.video
341 await sequelizeTypescript.transaction(async t => {
342 await videoInstance.destroy({ transaction: t })
345 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
348 async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
349 const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags(
356 return res.json(getFormattedObjects(resultList.data, resultList.total))