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