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