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