]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/index.ts
Fix concurrency error when deleting a video
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
1 import * as express from 'express'
2 import * as Promise from 'bluebird'
3 import * as multer from 'multer'
4 import { extname, join } from 'path'
5
6 import { database as db } from '../../../initializers/database'
7 import {
8 CONFIG,
9 REQUEST_VIDEO_QADU_TYPES,
10 REQUEST_VIDEO_EVENT_TYPES,
11 VIDEO_CATEGORIES,
12 VIDEO_LICENCES,
13 VIDEO_LANGUAGES
14 } from '../../../initializers'
15 import {
16 addEventToRemoteVideo,
17 quickAndDirtyUpdateVideoToFriends,
18 addVideoToFriends,
19 updateVideoToFriends,
20 JobScheduler
21 } from '../../../lib'
22 import {
23 authenticate,
24 paginationValidator,
25 videosSortValidator,
26 setVideosSort,
27 setPagination,
28 setVideosSearch,
29 videosUpdateValidator,
30 videosSearchValidator,
31 videosAddValidator,
32 videosGetValidator,
33 videosRemoveValidator
34 } from '../../../middlewares'
35 import {
36 logger,
37 retryTransactionWrapper,
38 generateRandomString,
39 getFormattedObjects,
40 renamePromise
41 } from '../../../helpers'
42 import { TagInstance } from '../../../models'
43 import { VideoCreate, VideoUpdate } from '../../../../shared'
44
45 import { abuseVideoRouter } from './abuse'
46 import { blacklistRouter } from './blacklist'
47 import { rateVideoRouter } from './rate'
48 import { VideoInstance } from '../../../models/video/video-interface'
49
50 const videosRouter = express.Router()
51
52 // multer configuration
53 const storage = multer.diskStorage({
54 destination: (req, file, cb) => {
55 cb(null, CONFIG.STORAGE.VIDEOS_DIR)
56 },
57
58 filename: (req, file, cb) => {
59 let extension = ''
60 if (file.mimetype === 'video/webm') extension = 'webm'
61 else if (file.mimetype === 'video/mp4') extension = 'mp4'
62 else if (file.mimetype === 'video/ogg') extension = 'ogv'
63 generateRandomString(16)
64 .then(randomString => {
65 cb(null, randomString + '.' + extension)
66 })
67 .catch(err => {
68 logger.error('Cannot generate random string for file name.', err)
69 throw err
70 })
71 }
72 })
73
74 const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
75
76 videosRouter.use('/', abuseVideoRouter)
77 videosRouter.use('/', blacklistRouter)
78 videosRouter.use('/', rateVideoRouter)
79
80 videosRouter.get('/categories', listVideoCategories)
81 videosRouter.get('/licences', listVideoLicences)
82 videosRouter.get('/languages', listVideoLanguages)
83
84 videosRouter.get('/',
85 paginationValidator,
86 videosSortValidator,
87 setVideosSort,
88 setPagination,
89 listVideos
90 )
91 videosRouter.put('/:id',
92 authenticate,
93 videosUpdateValidator,
94 updateVideoRetryWrapper
95 )
96 videosRouter.post('/upload',
97 authenticate,
98 reqFiles,
99 videosAddValidator,
100 addVideoRetryWrapper
101 )
102 videosRouter.get('/:id',
103 videosGetValidator,
104 getVideo
105 )
106
107 videosRouter.delete('/:id',
108 authenticate,
109 videosRemoveValidator,
110 removeVideoRetryWrapper
111 )
112
113 videosRouter.get('/search/:value',
114 videosSearchValidator,
115 paginationValidator,
116 videosSortValidator,
117 setVideosSort,
118 setPagination,
119 setVideosSearch,
120 searchVideos
121 )
122
123 // ---------------------------------------------------------------------------
124
125 export {
126 videosRouter
127 }
128
129 // ---------------------------------------------------------------------------
130
131 function listVideoCategories (req: express.Request, res: express.Response) {
132 res.json(VIDEO_CATEGORIES)
133 }
134
135 function listVideoLicences (req: express.Request, res: express.Response) {
136 res.json(VIDEO_LICENCES)
137 }
138
139 function listVideoLanguages (req: express.Request, res: express.Response) {
140 res.json(VIDEO_LANGUAGES)
141 }
142
143 // Wrapper to video add that retry the function if there is a database error
144 // We need this because we run the transaction in SERIALIZABLE isolation that can fail
145 function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
146 const options = {
147 arguments: [ req, res, req.files['videofile'][0] ],
148 errorMessage: 'Cannot insert the video with many retries.'
149 }
150
151 retryTransactionWrapper(addVideo, options)
152 .then(() => {
153 // TODO : include Location of the new video -> 201
154 res.type('json').status(204).end()
155 })
156 .catch(err => next(err))
157 }
158
159 function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
160 const videoInfo: VideoCreate = req.body
161 let videoUUID = ''
162
163 return db.sequelize.transaction(t => {
164 const user = res.locals.oauth.token.User
165
166 const name = user.username
167 // null because it is OUR pod
168 const podId = null
169 const userId = user.id
170
171 return db.Author.findOrCreateAuthor(name, podId, userId, t)
172 .then(author => {
173 const tags = videoInfo.tags
174 if (!tags) return { author, tagInstances: undefined }
175
176 return db.Tag.findOrCreateTags(tags, t).then(tagInstances => ({ author, tagInstances }))
177 })
178 .then(({ author, tagInstances }) => {
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 duration: videoPhysicalFile['duration'], // duration was added by a previous middleware
189 authorId: author.id
190 }
191
192 const video = db.Video.build(videoData)
193 return { author, tagInstances, video }
194 })
195 .then(({ author, tagInstances, video }) => {
196 const videoFileData = {
197 extname: extname(videoPhysicalFile.filename),
198 resolution: 0, // TODO: improve readability,
199 size: videoPhysicalFile.size
200 }
201
202 const videoFile = db.VideoFile.build(videoFileData)
203 return { author, tagInstances, video, videoFile }
204 })
205 .then(({ author, tagInstances, video, videoFile }) => {
206 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
207 const source = join(videoDir, videoPhysicalFile.filename)
208 const destination = join(videoDir, video.getVideoFilename(videoFile))
209
210 return renamePromise(source, destination)
211 .then(() => {
212 // This is important in case if there is another attempt in the retry process
213 videoPhysicalFile.filename = video.getVideoFilename(videoFile)
214 return { author, tagInstances, video, videoFile }
215 })
216 })
217 .then(({ author, tagInstances, video, videoFile }) => {
218 const tasks = []
219
220 tasks.push(
221 video.createTorrentAndSetInfoHash(videoFile),
222 video.createThumbnail(videoFile),
223 video.createPreview(videoFile)
224 )
225
226 if (CONFIG.TRANSCODING.ENABLED === true) {
227 // Put uuid because we don't have id auto incremented for now
228 const dataInput = {
229 videoUUID: video.uuid
230 }
231
232 tasks.push(
233 JobScheduler.Instance.createJob(t, 'videoTranscoder', dataInput)
234 )
235 }
236
237 return Promise.all(tasks).then(() => ({ author, tagInstances, video, videoFile }))
238 })
239 .then(({ author, tagInstances, video, videoFile }) => {
240 const options = { transaction: t }
241
242 return video.save(options)
243 .then(videoCreated => {
244 // Do not forget to add Author information to the created video
245 videoCreated.Author = author
246 videoUUID = videoCreated.uuid
247
248 return { tagInstances, video: videoCreated, videoFile }
249 })
250 })
251 .then(({ tagInstances, video, videoFile }) => {
252 const options = { transaction: t }
253 videoFile.videoId = video.id
254
255 return videoFile.save(options)
256 .then(() => video.VideoFiles = [ videoFile ])
257 .then(() => ({ tagInstances, video }))
258 })
259 .then(({ tagInstances, video }) => {
260 if (!tagInstances) return video
261
262 const options = { transaction: t }
263 return video.setTags(tagInstances, options)
264 .then(() => {
265 video.Tags = tagInstances
266 return video
267 })
268 })
269 .then(video => {
270 // Let transcoding job send the video to friends because the video file extension might change
271 if (CONFIG.TRANSCODING.ENABLED === true) return undefined
272
273 return video.toAddRemoteJSON()
274 .then(remoteVideo => {
275 // Now we'll add the video's meta data to our friends
276 return addVideoToFriends(remoteVideo, t)
277 })
278 })
279 })
280 .then(() => logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoUUID))
281 .catch((err: Error) => {
282 logger.debug('Cannot insert the video.', err)
283 throw err
284 })
285 }
286
287 function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
288 const options = {
289 arguments: [ req, res ],
290 errorMessage: 'Cannot update the video with many retries.'
291 }
292
293 retryTransactionWrapper(updateVideo, options)
294 .then(() => {
295 return res.type('json').status(204).end()
296 })
297 .catch(err => next(err))
298 }
299
300 function updateVideo (req: express.Request, res: express.Response) {
301 const videoInstance = res.locals.video
302 const videoFieldsSave = videoInstance.toJSON()
303 const videoInfoToUpdate: VideoUpdate = req.body
304
305 return db.sequelize.transaction(t => {
306 let tagsPromise: Promise<TagInstance[]>
307 if (!videoInfoToUpdate.tags) {
308 tagsPromise = Promise.resolve(null)
309 } else {
310 tagsPromise = db.Tag.findOrCreateTags(videoInfoToUpdate.tags, t)
311 }
312
313 return tagsPromise
314 .then(tagInstances => {
315 const options = {
316 transaction: t
317 }
318
319 if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
320 if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
321 if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
322 if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
323 if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
324 if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
325
326 return videoInstance.save(options).then(() => tagInstances)
327 })
328 .then(tagInstances => {
329 if (!tagInstances) return
330
331 const options = { transaction: t }
332 return videoInstance.setTags(tagInstances, options)
333 .then(() => {
334 videoInstance.Tags = tagInstances
335
336 return
337 })
338 })
339 .then(() => {
340 const json = videoInstance.toUpdateRemoteJSON()
341
342 // Now we'll update the video's meta data to our friends
343 return updateVideoToFriends(json, t)
344 })
345 })
346 .then(() => {
347 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
348 })
349 .catch(err => {
350 logger.debug('Cannot update the video.', err)
351
352 // Force fields we want to update
353 // If the transaction is retried, sequelize will think the object has not changed
354 // So it will skip the SQL request, even if the last one was ROLLBACKed!
355 Object.keys(videoFieldsSave).forEach(key => {
356 const value = videoFieldsSave[key]
357 videoInstance.set(key, value)
358 })
359
360 throw err
361 })
362 }
363
364 function getVideo (req: express.Request, res: express.Response) {
365 const videoInstance = res.locals.video
366
367 if (videoInstance.isOwned()) {
368 // The increment is done directly in the database, not using the instance value
369 videoInstance.increment('views')
370 .then(() => {
371 // FIXME: make a real view system
372 // For example, only add a view when a user watch a video during 30s etc
373 const qaduParams = {
374 videoId: videoInstance.id,
375 type: REQUEST_VIDEO_QADU_TYPES.VIEWS
376 }
377 return quickAndDirtyUpdateVideoToFriends(qaduParams)
378 })
379 .catch(err => logger.error('Cannot add view to video %d.', videoInstance.id, err))
380 } else {
381 // Just send the event to our friends
382 const eventParams = {
383 videoId: videoInstance.id,
384 type: REQUEST_VIDEO_EVENT_TYPES.VIEWS
385 }
386 addEventToRemoteVideo(eventParams)
387 }
388
389 // Do not wait the view system
390 res.json(videoInstance.toFormattedJSON())
391 }
392
393 function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
394 db.Video.listForApi(req.query.start, req.query.count, req.query.sort)
395 .then(result => res.json(getFormattedObjects(result.data, result.total)))
396 .catch(err => next(err))
397 }
398
399 function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
400 const options = {
401 arguments: [ req, res ],
402 errorMessage: 'Cannot remove the video with many retries.'
403 }
404
405 retryTransactionWrapper(removeVideo, options)
406 .then(() => {
407 return res.type('json').status(204).end()
408 })
409 .catch(err => next(err))
410 }
411
412 function removeVideo (req: express.Request, res: express.Response) {
413 const videoInstance: VideoInstance = res.locals.video
414
415 return db.sequelize.transaction(t => {
416 return videoInstance.destroy({ transaction: t })
417 })
418 .then(() => {
419 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
420 })
421 .catch(err => {
422 logger.error('Errors when removed the video.', err)
423 throw err
424 })
425 }
426
427 function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
428 db.Video.searchAndPopulateAuthorAndPodAndTags(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort)
429 .then(result => res.json(getFormattedObjects(result.data, result.total)))
430 .catch(err => next(err))
431 }