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