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