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