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