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