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