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