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