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