]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/index.ts
Use RsaSignature2017
[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 import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared'
5 import {
6 generateRandomString,
7 getFormattedObjects,
8 getVideoFileHeight,
9 logger,
10 renamePromise,
11 resetSequelizeInstance,
12 retryTransactionWrapper
13 } from '../../../helpers'
14 import { getServerActor } from '../../../helpers/utils'
15 import {
16 CONFIG,
17 sequelizeTypescript,
18 VIDEO_CATEGORIES,
19 VIDEO_LANGUAGES,
20 VIDEO_LICENCES,
21 VIDEO_MIMETYPE_EXT,
22 VIDEO_PRIVACIES
23 } from '../../../initializers'
24 import {
25 fetchRemoteVideoDescription,
26 getVideoActivityPubUrl,
27 shareVideoByServerAndChannel
28 } from '../../../lib/activitypub'
29 import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
30 import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler'
31 import {
32 asyncMiddleware,
33 authenticate,
34 paginationValidator,
35 setPagination,
36 setVideosSort,
37 videosAddValidator,
38 videosGetValidator,
39 videosRemoveValidator,
40 videosSearchValidator,
41 videosSortValidator,
42 videosUpdateValidator
43 } from '../../../middlewares'
44 import { TagModel } from '../../../models/video/tag'
45 import { VideoModel } from '../../../models/video/video'
46 import { VideoFileModel } from '../../../models/video/video-file'
47 import { abuseVideoRouter } from './abuse'
48 import { blacklistRouter } from './blacklist'
49 import { videoChannelRouter } from './channel'
50 import { rateVideoRouter } from './rate'
51
52 const videosRouter = express.Router()
53
54 // multer configuration
55 const storage = multer.diskStorage({
56 destination: (req, file, cb) => {
57 cb(null, CONFIG.STORAGE.VIDEOS_DIR)
58 },
59
60 filename: async (req, file, cb) => {
61 const extension = VIDEO_MIMETYPE_EXT[file.mimetype]
62 let randomString = ''
63
64 try {
65 randomString = await generateRandomString(16)
66 } catch (err) {
67 logger.error('Cannot generate random string for file name.', err)
68 randomString = 'fake-random-string'
69 }
70
71 cb(null, randomString + extension)
72 }
73 })
74
75 const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
76
77 videosRouter.use('/', abuseVideoRouter)
78 videosRouter.use('/', blacklistRouter)
79 videosRouter.use('/', rateVideoRouter)
80 videosRouter.use('/', videoChannelRouter)
81
82 videosRouter.get('/categories', listVideoCategories)
83 videosRouter.get('/licences', listVideoLicences)
84 videosRouter.get('/languages', listVideoLanguages)
85 videosRouter.get('/privacies', listVideoPrivacies)
86
87 videosRouter.get('/',
88 paginationValidator,
89 videosSortValidator,
90 setVideosSort,
91 setPagination,
92 asyncMiddleware(listVideos)
93 )
94 videosRouter.get('/search',
95 videosSearchValidator,
96 paginationValidator,
97 videosSortValidator,
98 setVideosSort,
99 setPagination,
100 asyncMiddleware(searchVideos)
101 )
102 videosRouter.put('/:id',
103 authenticate,
104 asyncMiddleware(videosUpdateValidator),
105 asyncMiddleware(updateVideoRetryWrapper)
106 )
107 videosRouter.post('/upload',
108 authenticate,
109 reqFiles,
110 asyncMiddleware(videosAddValidator),
111 asyncMiddleware(addVideoRetryWrapper)
112 )
113
114 videosRouter.get('/:id/description',
115 asyncMiddleware(videosGetValidator),
116 asyncMiddleware(getVideoDescription)
117 )
118 videosRouter.get('/:id',
119 asyncMiddleware(videosGetValidator),
120 getVideo
121 )
122 videosRouter.post('/:id/views',
123 asyncMiddleware(videosGetValidator),
124 asyncMiddleware(viewVideo)
125 )
126
127 videosRouter.delete('/:id',
128 authenticate,
129 asyncMiddleware(videosRemoveValidator),
130 asyncMiddleware(removeVideoRetryWrapper)
131 )
132
133 // ---------------------------------------------------------------------------
134
135 export {
136 videosRouter
137 }
138
139 // ---------------------------------------------------------------------------
140
141 function listVideoCategories (req: express.Request, res: express.Response) {
142 res.json(VIDEO_CATEGORIES)
143 }
144
145 function listVideoLicences (req: express.Request, res: express.Response) {
146 res.json(VIDEO_LICENCES)
147 }
148
149 function listVideoLanguages (req: express.Request, res: express.Response) {
150 res.json(VIDEO_LANGUAGES)
151 }
152
153 function listVideoPrivacies (req: express.Request, res: express.Response) {
154 res.json(VIDEO_PRIVACIES)
155 }
156
157 // Wrapper to video add that retry the function if there is a database error
158 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
159 async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
160 const options = {
161 arguments: [ req, res, req.files['videofile'][0] ],
162 errorMessage: 'Cannot insert the video with many retries.'
163 }
164
165 const video = await retryTransactionWrapper(addVideo, options)
166
167 res.json({
168 video: {
169 id: video.id,
170 uuid: video.uuid
171 }
172 }).end()
173 }
174
175 async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
176 const videoInfo: VideoCreate = req.body
177
178 // Prepare data so we don't block the transaction
179 const videoData = {
180 name: videoInfo.name,
181 remote: false,
182 extname: extname(videoPhysicalFile.filename),
183 category: videoInfo.category,
184 licence: videoInfo.licence,
185 language: videoInfo.language,
186 nsfw: videoInfo.nsfw,
187 description: videoInfo.description,
188 privacy: videoInfo.privacy,
189 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
190 channelId: res.locals.videoChannel.id
191 }
192 const video = new VideoModel(videoData)
193 video.url = getVideoActivityPubUrl(video)
194
195 const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename)
196 const videoFileHeight = await getVideoFileHeight(videoFilePath)
197
198 const videoFileData = {
199 extname: extname(videoPhysicalFile.filename),
200 resolution: videoFileHeight,
201 size: videoPhysicalFile.size
202 }
203 const videoFile = new VideoFileModel(videoFileData)
204 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
205 const source = join(videoDir, videoPhysicalFile.filename)
206 const destination = join(videoDir, video.getVideoFilename(videoFile))
207
208 await renamePromise(source, destination)
209 // This is important in case if there is another attempt in the retry process
210 videoPhysicalFile.filename = video.getVideoFilename(videoFile)
211
212 const tasks = []
213
214 tasks.push(
215 video.createTorrentAndSetInfoHash(videoFile),
216 video.createThumbnail(videoFile),
217 video.createPreview(videoFile)
218 )
219 await Promise.all(tasks)
220
221 return sequelizeTypescript.transaction(async t => {
222 const sequelizeOptions = { transaction: t }
223
224 if (CONFIG.TRANSCODING.ENABLED === true) {
225 // Put uuid because we don't have id auto incremented for now
226 const dataInput = {
227 videoUUID: video.uuid
228 }
229
230 await transcodingJobScheduler.createJob(t, 'videoFileOptimizer', dataInput)
231 }
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
237 videoFile.videoId = video.id
238 await videoFile.save(sequelizeOptions)
239
240 video.VideoFiles = [ videoFile ]
241
242 if (videoInfo.tags) {
243 const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t)
244
245 await video.$set('Tags', tagInstances, sequelizeOptions)
246 video.Tags = tagInstances
247 }
248
249 // Let transcoding job send the video to friends because the video file extension might change
250 if (CONFIG.TRANSCODING.ENABLED === true) return videoCreated
251 // Don't send video to remote servers, it is private
252 if (video.privacy === VideoPrivacy.PRIVATE) return videoCreated
253
254 await sendCreateVideo(video, t)
255 await shareVideoByServerAndChannel(video, t)
256
257 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
258
259 return videoCreated
260 })
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: VideoModel = 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 sequelizeTypescript.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', parseInt(videoInfoToUpdate.privacy.toString(), 10))
292 if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
293
294 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
295
296 if (videoInfoToUpdate.tags) {
297 const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
298
299 await videoInstance.$set('Tags', 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 await sendUpdateVideo(videoInstanceUpdated, t)
306 }
307
308 // Video is not private anymore, send a create action to remote servers
309 if (wasPrivateVideo === true && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE) {
310 await sendCreateVideo(videoInstanceUpdated, t)
311 await shareVideoByServerAndChannel(videoInstanceUpdated, t)
312 }
313 })
314
315 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
316 } catch (err) {
317 // Force fields we want to update
318 // If the transaction is retried, sequelize will think the object has not changed
319 // So it will skip the SQL request, even if the last one was ROLLBACKed!
320 resetSequelizeInstance(videoInstance, videoFieldsSave)
321
322 throw err
323 }
324 }
325
326 function getVideo (req: express.Request, res: express.Response) {
327 const videoInstance = res.locals.video
328
329 return res.json(videoInstance.toFormattedDetailsJSON())
330 }
331
332 async function viewVideo (req: express.Request, res: express.Response) {
333 const videoInstance = res.locals.video
334
335 await videoInstance.increment('views')
336 const serverAccount = await getServerActor()
337
338 if (videoInstance.isOwned()) {
339 await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
340 } else {
341 await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
342 }
343
344 return res.status(204).end()
345 }
346
347 async function getVideoDescription (req: express.Request, res: express.Response) {
348 const videoInstance = res.locals.video
349 let description = ''
350
351 if (videoInstance.isOwned()) {
352 description = videoInstance.description
353 } else {
354 description = await fetchRemoteVideoDescription(videoInstance)
355 }
356
357 return res.json({ description })
358 }
359
360 async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
361 const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort)
362
363 return res.json(getFormattedObjects(resultList.data, resultList.total))
364 }
365
366 async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
367 const options = {
368 arguments: [ req, res ],
369 errorMessage: 'Cannot remove the video with many retries.'
370 }
371
372 await retryTransactionWrapper(removeVideo, options)
373
374 return res.type('json').status(204).end()
375 }
376
377 async function removeVideo (req: express.Request, res: express.Response) {
378 const videoInstance: VideoModel = res.locals.video
379
380 await sequelizeTypescript.transaction(async t => {
381 await videoInstance.destroy({ transaction: t })
382 })
383
384 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
385 }
386
387 async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
388 const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags(
389 req.query.search,
390 req.query.start,
391 req.query.count,
392 req.query.sort
393 )
394
395 return res.json(getFormattedObjects(resultList.data, resultList.total))
396 }