]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import * as Promise from 'bluebird' | |
3 | import * as multer from 'multer' | |
4 | import * as path 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 | } 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, | |
36 | retryTransactionWrapper, | |
37 | generateRandomString, | |
38 | getFormatedObjects, | |
39 | renamePromise | |
40 | } from '../../../helpers' | |
41 | import { TagInstance } from '../../../models' | |
42 | ||
43 | import { abuseVideoRouter } from './abuse' | |
44 | import { blacklistRouter } from './blacklist' | |
45 | import { rateVideoRouter } from './rate' | |
46 | ||
47 | const videosRouter = express.Router() | |
48 | ||
49 | // multer configuration | |
50 | const storage = multer.diskStorage({ | |
51 | destination: function (req, file, cb) { | |
52 | cb(null, CONFIG.STORAGE.VIDEOS_DIR) | |
53 | }, | |
54 | ||
55 | filename: function (req, file, cb) { | |
56 | let extension = '' | |
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' | |
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 | }) | |
69 | } | |
70 | }) | |
71 | ||
72 | const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) | |
73 | ||
74 | videosRouter.use('/', abuseVideoRouter) | |
75 | videosRouter.use('/', blacklistRouter) | |
76 | videosRouter.use('/', rateVideoRouter) | |
77 | ||
78 | videosRouter.get('/categories', listVideoCategories) | |
79 | videosRouter.get('/licences', listVideoLicences) | |
80 | videosRouter.get('/languages', listVideoLanguages) | |
81 | ||
82 | videosRouter.get('/', | |
83 | paginationValidator, | |
84 | videosSortValidator, | |
85 | setVideosSort, | |
86 | setPagination, | |
87 | listVideos | |
88 | ) | |
89 | videosRouter.put('/:id', | |
90 | authenticate, | |
91 | videosUpdateValidator, | |
92 | updateVideoRetryWrapper | |
93 | ) | |
94 | videosRouter.post('/', | |
95 | authenticate, | |
96 | reqFiles, | |
97 | videosAddValidator, | |
98 | addVideoRetryWrapper | |
99 | ) | |
100 | videosRouter.get('/:id', | |
101 | videosGetValidator, | |
102 | getVideo | |
103 | ) | |
104 | ||
105 | videosRouter.delete('/:id', | |
106 | authenticate, | |
107 | videosRemoveValidator, | |
108 | removeVideo | |
109 | ) | |
110 | ||
111 | videosRouter.get('/search/:value', | |
112 | videosSearchValidator, | |
113 | paginationValidator, | |
114 | videosSortValidator, | |
115 | setVideosSort, | |
116 | setPagination, | |
117 | setVideosSearch, | |
118 | searchVideos | |
119 | ) | |
120 | ||
121 | // --------------------------------------------------------------------------- | |
122 | ||
123 | export { | |
124 | videosRouter | |
125 | } | |
126 | ||
127 | // --------------------------------------------------------------------------- | |
128 | ||
129 | function listVideoCategories (req: express.Request, res: express.Response, next: express.NextFunction) { | |
130 | res.json(VIDEO_CATEGORIES) | |
131 | } | |
132 | ||
133 | function listVideoLicences (req: express.Request, res: express.Response, next: express.NextFunction) { | |
134 | res.json(VIDEO_LICENCES) | |
135 | } | |
136 | ||
137 | function listVideoLanguages (req: express.Request, res: express.Response, next: express.NextFunction) { | |
138 | res.json(VIDEO_LANGUAGES) | |
139 | } | |
140 | ||
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 | |
143 | function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | |
144 | const options = { | |
145 | arguments: [ req, res, req.files.videofile[0] ], | |
146 | errorMessage: 'Cannot insert the video with many retries.' | |
147 | } | |
148 | ||
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)) | |
155 | } | |
156 | ||
157 | function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File) { | |
158 | const videoInfos = req.body | |
159 | ||
160 | return db.sequelize.transaction(t => { | |
161 | const user = res.locals.oauth.token.User | |
162 | ||
163 | const name = user.username | |
164 | // null because it is OUR pod | |
165 | const podId = null | |
166 | const userId = user.id | |
167 | ||
168 | return db.Author.findOrCreateAuthor(name, podId, userId, t) | |
169 | .then(author => { | |
170 | const tags = videoInfos.tags | |
171 | if (!tags) return { author, tagInstances: undefined } | |
172 | ||
173 | return db.Tag.findOrCreateTags(tags, t).then(tagInstances => ({ author, tagInstances })) | |
174 | }) | |
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 } | |
191 | }) | |
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 | }) | |
203 | }) | |
204 | .then(({ author, tagInstances, video }) => { | |
205 | const options = { transaction: t } | |
206 | ||
207 | return video.save(options) | |
208 | .then(videoCreated => { | |
209 | // Do not forget to add Author informations to the created video | |
210 | videoCreated.Author = author | |
211 | ||
212 | return { tagInstances, video: videoCreated } | |
213 | }) | |
214 | }) | |
215 | .then(({ tagInstances, video }) => { | |
216 | if (!tagInstances) return video | |
217 | ||
218 | const options = { transaction: t } | |
219 | return video.setTags(tagInstances, options) | |
220 | .then(() => { | |
221 | video.Tags = tagInstances | |
222 | return video | |
223 | }) | |
224 | }) | |
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 | }) | |
234 | }) | |
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 | |
240 | }) | |
241 | } | |
242 | ||
243 | function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { | |
244 | const options = { | |
245 | arguments: [ req, res ], | |
246 | errorMessage: 'Cannot update the video with many retries.' | |
247 | } | |
248 | ||
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)) | |
255 | } | |
256 | ||
257 | function updateVideo (req: express.Request, res: express.Response) { | |
258 | const videoInstance = res.locals.video | |
259 | const videoFieldsSave = videoInstance.toJSON() | |
260 | const videoInfosToUpdate = req.body | |
261 | ||
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 | } | |
269 | ||
270 | return tagsPromise | |
271 | .then(tagInstances => { | |
272 | const options = { | |
273 | transaction: t | |
274 | } | |
275 | ||
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) | |
282 | ||
283 | return videoInstance.save(options).then(() => tagInstances) | |
284 | }) | |
285 | .then(tagInstances => { | |
286 | if (!tagInstances) return | |
287 | ||
288 | const options = { transaction: t } | |
289 | return videoInstance.setTags(tagInstances, options) | |
290 | .then(() => { | |
291 | videoInstance.Tags = tagInstances | |
292 | ||
293 | return | |
294 | }) | |
295 | }) | |
296 | .then(() => { | |
297 | const json = videoInstance.toUpdateRemoteJSON() | |
298 | ||
299 | // Now we'll update the video's meta data to our friends | |
300 | return updateVideoToFriends(json, t) | |
301 | }) | |
302 | }) | |
303 | .then(() => { | |
304 | logger.info('Video with name %s updated.', videoInstance.name) | |
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 | |
318 | }) | |
319 | } | |
320 | ||
321 | function getVideo (req: express.Request, res: express.Response, next: express.NextFunction) { | |
322 | const videoInstance = res.locals.video | |
323 | ||
324 | if (videoInstance.isOwned()) { | |
325 | // The increment is done directly in the database, not using the instance value | |
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 })) | |
337 | } else { | |
338 | // Just send the event to our friends | |
339 | const eventParams = { | |
340 | videoId: videoInstance.id, | |
341 | type: REQUEST_VIDEO_EVENT_TYPES.VIEWS | |
342 | } | |
343 | addEventToRemoteVideo(eventParams) | |
344 | } | |
345 | ||
346 | // Do not wait the view system | |
347 | res.json(videoInstance.toFormatedJSON()) | |
348 | } | |
349 | ||
350 | function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | |
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)) | |
354 | } | |
355 | ||
356 | function removeVideo (req: express.Request, res: express.Response, next: express.NextFunction) { | |
357 | const videoInstance = res.locals.video | |
358 | ||
359 | videoInstance.destroy() | |
360 | .then(() => res.type('json').status(204).end()) | |
361 | .catch(err => { | |
362 | logger.error('Errors when removed the video.', { error: err }) | |
363 | return next(err) | |
364 | }) | |
365 | } | |
366 | ||
367 | function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { | |
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)) | |
371 | } |