]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
6fcd19ba | 2 | import * as Promise from 'bluebird' |
4d4e5cd4 C |
3 | import * as multer from 'multer' |
4 | import * as path 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, | |
19 | updateVideoToFriends | |
20 | } from '../../../lib' | |
21 | import { | |
22 | authenticate, | |
23 | paginationValidator, | |
24 | videosSortValidator, | |
25 | setVideosSort, | |
26 | setPagination, | |
27 | setVideosSearch, | |
28 | videosUpdateValidator, | |
29 | videosSearchValidator, | |
30 | videosAddValidator, | |
31 | videosGetValidator, | |
32 | videosRemoveValidator | |
33 | } from '../../../middlewares' | |
34 | import { | |
35 | logger, | |
65fcc311 | 36 | retryTransactionWrapper, |
65fcc311 | 37 | generateRandomString, |
6fcd19ba C |
38 | getFormatedObjects, |
39 | renamePromise | |
65fcc311 | 40 | } from '../../../helpers' |
6fcd19ba | 41 | import { TagInstance } from '../../../models' |
4771e000 | 42 | import { VideoCreate, VideoUpdate } from '../../../../shared' |
65fcc311 C |
43 | |
44 | import { abuseVideoRouter } from './abuse' | |
45 | import { blacklistRouter } from './blacklist' | |
46 | import { rateVideoRouter } from './rate' | |
47 | ||
48 | const videosRouter = express.Router() | |
9f10b292 C |
49 | |
50 | // multer configuration | |
f0f5567b | 51 | const storage = multer.diskStorage({ |
075f16ca | 52 | destination: (req, file, cb) => { |
65fcc311 | 53 | cb(null, CONFIG.STORAGE.VIDEOS_DIR) |
9f10b292 C |
54 | }, |
55 | ||
075f16ca | 56 | filename: (req, file, cb) => { |
f0f5567b | 57 | let extension = '' |
9f10b292 C |
58 | if (file.mimetype === 'video/webm') extension = 'webm' |
59 | else if (file.mimetype === 'video/mp4') extension = 'mp4' | |
60 | else if (file.mimetype === 'video/ogg') extension = 'ogv' | |
6fcd19ba C |
61 | generateRandomString(16) |
62 | .then(randomString => { | |
63 | const filename = randomString | |
64 | cb(null, filename + '.' + extension) | |
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 | |
69818c93 | 130 | function listVideoCategories (req: express.Request, res: express.Response, next: express.NextFunction) { |
65fcc311 | 131 | res.json(VIDEO_CATEGORIES) |
6e07c3de C |
132 | } |
133 | ||
69818c93 | 134 | function listVideoLicences (req: express.Request, res: express.Response, next: express.NextFunction) { |
65fcc311 | 135 | res.json(VIDEO_LICENCES) |
6f0c39e2 C |
136 | } |
137 | ||
69818c93 | 138 | function listVideoLanguages (req: express.Request, res: express.Response, next: express.NextFunction) { |
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 C |
145 | const options = { |
146 | arguments: [ req, res, req.files.videofile[0] ], | |
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 | ||
6fcd19ba | 158 | function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File) { |
4771e000 | 159 | const videoInfos: 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 => { | |
171 | const tags = videoInfos.tags | |
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 = { | |
178 | name: videoInfos.name, | |
0a6658fd | 179 | remote: false, |
6fcd19ba C |
180 | extname: path.extname(videoFile.filename), |
181 | category: videoInfos.category, | |
182 | licence: videoInfos.licence, | |
183 | language: videoInfos.language, | |
184 | nsfw: videoInfos.nsfw, | |
185 | description: videoInfos.description, | |
186 | duration: videoFile['duration'], // duration was added by a previous middleware | |
187 | authorId: author.id | |
188 | } | |
189 | ||
190 | const video = db.Video.build(videoData) | |
191 | return { author, tagInstances, video } | |
feb4bdfd | 192 | }) |
6fcd19ba C |
193 | .then(({ author, tagInstances, video }) => { |
194 | const videoDir = CONFIG.STORAGE.VIDEOS_DIR | |
195 | const source = path.join(videoDir, videoFile.filename) | |
196 | const destination = path.join(videoDir, video.getVideoFilename()) | |
197 | ||
198 | return renamePromise(source, destination) | |
199 | .then(() => { | |
200 | // This is important in case if there is another attempt in the retry process | |
201 | videoFile.filename = video.getVideoFilename() | |
202 | return { author, tagInstances, video } | |
203 | }) | |
558d7c23 | 204 | }) |
6fcd19ba C |
205 | .then(({ author, tagInstances, video }) => { |
206 | const options = { transaction: t } | |
7920c273 | 207 | |
6fcd19ba C |
208 | return video.save(options) |
209 | .then(videoCreated => { | |
210 | // Do not forget to add Author informations to the created video | |
211 | videoCreated.Author = author | |
feb4bdfd | 212 | |
6fcd19ba C |
213 | return { tagInstances, video: videoCreated } |
214 | }) | |
3a8a8b51 | 215 | }) |
6fcd19ba C |
216 | .then(({ tagInstances, video }) => { |
217 | if (!tagInstances) return video | |
7920c273 | 218 | |
6fcd19ba C |
219 | const options = { transaction: t } |
220 | return video.setTags(tagInstances, options) | |
221 | .then(() => { | |
222 | video.Tags = tagInstances | |
223 | return video | |
224 | }) | |
7920c273 | 225 | }) |
6fcd19ba C |
226 | .then(video => { |
227 | // Let transcoding job send the video to friends because the videofile extension might change | |
228 | if (CONFIG.TRANSCODING.ENABLED === true) return undefined | |
229 | ||
230 | return video.toAddRemoteJSON() | |
231 | .then(remoteVideo => { | |
232 | // Now we'll add the video's meta data to our friends | |
233 | return addVideoToFriends(remoteVideo, t) | |
234 | }) | |
528a9efa | 235 | }) |
6fcd19ba C |
236 | }) |
237 | .then(() => logger.info('Video with name %s created.', videoInfos.name)) | |
238 | .catch((err: Error) => { | |
239 | logger.debug('Cannot insert the video.', { error: err.stack }) | |
240 | throw err | |
7b1f49de C |
241 | }) |
242 | } | |
243 | ||
69818c93 | 244 | function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
d6a5b018 C |
245 | const options = { |
246 | arguments: [ req, res ], | |
247 | errorMessage: 'Cannot update the video with many retries.' | |
248 | } | |
ed04d94f | 249 | |
6fcd19ba C |
250 | retryTransactionWrapper(updateVideo, options) |
251 | .then(() => { | |
252 | // TODO : include Location of the new video -> 201 | |
253 | return res.type('json').status(204).end() | |
254 | }) | |
255 | .catch(err => next(err)) | |
ed04d94f C |
256 | } |
257 | ||
6fcd19ba | 258 | function updateVideo (req: express.Request, res: express.Response) { |
818f7987 | 259 | const videoInstance = res.locals.video |
7f4e7c36 | 260 | const videoFieldsSave = videoInstance.toJSON() |
4771e000 | 261 | const videoInfosToUpdate: VideoUpdate = req.body |
7b1f49de | 262 | |
6fcd19ba C |
263 | return db.sequelize.transaction(t => { |
264 | let tagsPromise: Promise<TagInstance[]> | |
265 | if (!videoInfosToUpdate.tags) { | |
266 | tagsPromise = Promise.resolve(null) | |
267 | } else { | |
268 | tagsPromise = db.Tag.findOrCreateTags(videoInfosToUpdate.tags, t) | |
269 | } | |
7b1f49de | 270 | |
6fcd19ba C |
271 | return tagsPromise |
272 | .then(tagInstances => { | |
273 | const options = { | |
274 | transaction: t | |
275 | } | |
7b1f49de | 276 | |
6fcd19ba C |
277 | if (videoInfosToUpdate.name !== undefined) videoInstance.set('name', videoInfosToUpdate.name) |
278 | if (videoInfosToUpdate.category !== undefined) videoInstance.set('category', videoInfosToUpdate.category) | |
279 | if (videoInfosToUpdate.licence !== undefined) videoInstance.set('licence', videoInfosToUpdate.licence) | |
280 | if (videoInfosToUpdate.language !== undefined) videoInstance.set('language', videoInfosToUpdate.language) | |
281 | if (videoInfosToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfosToUpdate.nsfw) | |
282 | if (videoInfosToUpdate.description !== undefined) videoInstance.set('description', videoInfosToUpdate.description) | |
7b1f49de | 283 | |
6fcd19ba | 284 | return videoInstance.save(options).then(() => tagInstances) |
ed04d94f | 285 | }) |
6fcd19ba C |
286 | .then(tagInstances => { |
287 | if (!tagInstances) return | |
7b1f49de | 288 | |
6fcd19ba C |
289 | const options = { transaction: t } |
290 | return videoInstance.setTags(tagInstances, options) | |
291 | .then(() => { | |
292 | videoInstance.Tags = tagInstances | |
7920c273 | 293 | |
6fcd19ba C |
294 | return |
295 | }) | |
7f4e7c36 | 296 | }) |
6fcd19ba C |
297 | .then(() => { |
298 | const json = videoInstance.toUpdateRemoteJSON() | |
7f4e7c36 | 299 | |
6fcd19ba C |
300 | // Now we'll update the video's meta data to our friends |
301 | return updateVideoToFriends(json, t) | |
302 | }) | |
303 | }) | |
304 | .then(() => { | |
c3d19a49 | 305 | logger.info('Video with name %s updated.', videoInstance.name) |
6fcd19ba C |
306 | }) |
307 | .catch(err => { | |
ad0997ad | 308 | logger.debug('Cannot update the video.', err) |
6fcd19ba C |
309 | |
310 | // Force fields we want to update | |
311 | // If the transaction is retried, sequelize will think the object has not changed | |
312 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
075f16ca | 313 | Object.keys(videoFieldsSave).forEach(key => { |
6fcd19ba C |
314 | const value = videoFieldsSave[key] |
315 | videoInstance.set(key, value) | |
316 | }) | |
317 | ||
318 | throw err | |
9f10b292 C |
319 | }) |
320 | } | |
8c308c2b | 321 | |
69818c93 | 322 | function getVideo (req: express.Request, res: express.Response, next: express.NextFunction) { |
818f7987 | 323 | const videoInstance = res.locals.video |
9e167724 C |
324 | |
325 | if (videoInstance.isOwned()) { | |
326 | // The increment is done directly in the database, not using the instance value | |
6fcd19ba C |
327 | videoInstance.increment('views') |
328 | .then(() => { | |
329 | // FIXME: make a real view system | |
330 | // For example, only add a view when a user watch a video during 30s etc | |
331 | const qaduParams = { | |
332 | videoId: videoInstance.id, | |
333 | type: REQUEST_VIDEO_QADU_TYPES.VIEWS | |
334 | } | |
335 | return quickAndDirtyUpdateVideoToFriends(qaduParams) | |
336 | }) | |
ad0997ad | 337 | .catch(err => logger.error('Cannot add view to video %d.', videoInstance.id, err)) |
e4c87ec2 C |
338 | } else { |
339 | // Just send the event to our friends | |
d38b8281 C |
340 | const eventParams = { |
341 | videoId: videoInstance.id, | |
65fcc311 | 342 | type: REQUEST_VIDEO_EVENT_TYPES.VIEWS |
d38b8281 | 343 | } |
65fcc311 | 344 | addEventToRemoteVideo(eventParams) |
9e167724 C |
345 | } |
346 | ||
347 | // Do not wait the view system | |
818f7987 | 348 | res.json(videoInstance.toFormatedJSON()) |
9f10b292 | 349 | } |
8c308c2b | 350 | |
69818c93 | 351 | function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
6fcd19ba C |
352 | db.Video.listForApi(req.query.start, req.query.count, req.query.sort) |
353 | .then(result => res.json(getFormatedObjects(result.data, result.total))) | |
354 | .catch(err => next(err)) | |
9f10b292 | 355 | } |
c45f7f84 | 356 | |
69818c93 | 357 | function removeVideo (req: express.Request, res: express.Response, next: express.NextFunction) { |
818f7987 | 358 | const videoInstance = res.locals.video |
8c308c2b | 359 | |
6fcd19ba C |
360 | videoInstance.destroy() |
361 | .then(() => res.type('json').status(204).end()) | |
362 | .catch(err => { | |
ad0997ad | 363 | logger.error('Errors when removed the video.', err) |
807df9e6 | 364 | return next(err) |
6fcd19ba | 365 | }) |
9f10b292 | 366 | } |
8c308c2b | 367 | |
69818c93 | 368 | function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
6fcd19ba C |
369 | db.Video.searchAndPopulateAuthorAndPodAndTags(req.params.value, req.query.field, req.query.start, req.query.count, req.query.sort) |
370 | .then(result => res.json(getFormatedObjects(result.data, result.total))) | |
371 | .catch(err => next(err)) | |
9f10b292 | 372 | } |