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