]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/videos/index.ts
Set sort refractoring
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { extname, join } from 'path'
3import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared'
4import { renamePromise } from '../../../helpers/core-utils'
5import { retryTransactionWrapper } from '../../../helpers/database-utils'
6import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils'
7import { logger } from '../../../helpers/logger'
8import { createReqFiles, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils'
9import {
10 CONFIG, sequelizeTypescript, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT,
11 VIDEO_PRIVACIES
12} from '../../../initializers'
13import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub'
14import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
15import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler'
16import {
17 asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setPagination, videosAddValidator, videosGetValidator,
18 videosRemoveValidator, videosSearchValidator, videosSortValidator, videosUpdateValidator
19} from '../../../middlewares'
20import { TagModel } from '../../../models/video/tag'
21import { VideoModel } from '../../../models/video/video'
22import { VideoFileModel } from '../../../models/video/video-file'
23import { abuseVideoRouter } from './abuse'
24import { blacklistRouter } from './blacklist'
25import { videoChannelRouter } from './channel'
26import { videoCommentRouter } from './comment'
27import { rateVideoRouter } from './rate'
28
29const videosRouter = express.Router()
30
31const reqVideoFile = createReqFiles('videofile', CONFIG.STORAGE.VIDEOS_DIR, VIDEO_MIMETYPE_EXT)
32
33videosRouter.use('/', abuseVideoRouter)
34videosRouter.use('/', blacklistRouter)
35videosRouter.use('/', rateVideoRouter)
36videosRouter.use('/', videoChannelRouter)
37videosRouter.use('/', videoCommentRouter)
38
39videosRouter.get('/categories', listVideoCategories)
40videosRouter.get('/licences', listVideoLicences)
41videosRouter.get('/languages', listVideoLanguages)
42videosRouter.get('/privacies', listVideoPrivacies)
43
44videosRouter.get('/',
45 paginationValidator,
46 videosSortValidator,
47 setDefaultSort,
48 setPagination,
49 asyncMiddleware(listVideos)
50)
51videosRouter.get('/search',
52 videosSearchValidator,
53 paginationValidator,
54 videosSortValidator,
55 setDefaultSort,
56 setPagination,
57 asyncMiddleware(searchVideos)
58)
59videosRouter.put('/:id',
60 authenticate,
61 asyncMiddleware(videosUpdateValidator),
62 asyncMiddleware(updateVideoRetryWrapper)
63)
64videosRouter.post('/upload',
65 authenticate,
66 reqVideoFile,
67 asyncMiddleware(videosAddValidator),
68 asyncMiddleware(addVideoRetryWrapper)
69)
70
71videosRouter.get('/:id/description',
72 asyncMiddleware(videosGetValidator),
73 asyncMiddleware(getVideoDescription)
74)
75videosRouter.get('/:id',
76 asyncMiddleware(videosGetValidator),
77 getVideo
78)
79videosRouter.post('/:id/views',
80 asyncMiddleware(videosGetValidator),
81 asyncMiddleware(viewVideo)
82)
83
84videosRouter.delete('/:id',
85 authenticate,
86 asyncMiddleware(videosRemoveValidator),
87 asyncMiddleware(removeVideoRetryWrapper)
88)
89
90// ---------------------------------------------------------------------------
91
92export {
93 videosRouter
94}
95
96// ---------------------------------------------------------------------------
97
98function listVideoCategories (req: express.Request, res: express.Response) {
99 res.json(VIDEO_CATEGORIES)
100}
101
102function listVideoLicences (req: express.Request, res: express.Response) {
103 res.json(VIDEO_LICENCES)
104}
105
106function listVideoLanguages (req: express.Request, res: express.Response) {
107 res.json(VIDEO_LANGUAGES)
108}
109
110function listVideoPrivacies (req: express.Request, res: express.Response) {
111 res.json(VIDEO_PRIVACIES)
112}
113
114// Wrapper to video add that retry the function if there is a database error
115// We need this because we run the transaction in SERIALIZABLE isolation that can fail
116async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
117 const options = {
118 arguments: [ req, res, req.files['videofile'][0] ],
119 errorMessage: 'Cannot insert the video with many retries.'
120 }
121
122 const video = await retryTransactionWrapper(addVideo, options)
123
124 res.json({
125 video: {
126 id: video.id,
127 uuid: video.uuid
128 }
129 }).end()
130}
131
132async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) {
133 const videoInfo: VideoCreate = req.body
134
135 // Prepare data so we don't block the transaction
136 const videoData = {
137 name: videoInfo.name,
138 remote: false,
139 extname: extname(videoPhysicalFile.filename),
140 category: videoInfo.category,
141 licence: videoInfo.licence,
142 language: videoInfo.language,
143 commentsEnabled: videoInfo.commentsEnabled,
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
221async 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
232async 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 if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled)
252
253 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
254
255 if (videoInfoToUpdate.tags) {
256 const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
257
258 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
259 videoInstance.Tags = tagInstances
260 }
261
262 // Now we'll update the video's meta data to our friends
263 if (wasPrivateVideo === false) {
264 await sendUpdateVideo(videoInstanceUpdated, t)
265 }
266
267 // Video is not private anymore, send a create action to remote servers
268 if (wasPrivateVideo === true && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE) {
269 await sendCreateVideo(videoInstanceUpdated, t)
270 await shareVideoByServerAndChannel(videoInstanceUpdated, t)
271 }
272 })
273
274 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
275 } catch (err) {
276 // Force fields we want to update
277 // If the transaction is retried, sequelize will think the object has not changed
278 // So it will skip the SQL request, even if the last one was ROLLBACKed!
279 resetSequelizeInstance(videoInstance, videoFieldsSave)
280
281 throw err
282 }
283}
284
285function getVideo (req: express.Request, res: express.Response) {
286 const videoInstance = res.locals.video
287
288 return res.json(videoInstance.toFormattedDetailsJSON())
289}
290
291async function viewVideo (req: express.Request, res: express.Response) {
292 const videoInstance = res.locals.video
293
294 await videoInstance.increment('views')
295 const serverAccount = await getServerActor()
296
297 if (videoInstance.isOwned()) {
298 await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
299 } else {
300 await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
301 }
302
303 return res.status(204).end()
304}
305
306async function getVideoDescription (req: express.Request, res: express.Response) {
307 const videoInstance = res.locals.video
308 let description = ''
309
310 if (videoInstance.isOwned()) {
311 description = videoInstance.description
312 } else {
313 description = await fetchRemoteVideoDescription(videoInstance)
314 }
315
316 return res.json({ description })
317}
318
319async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
320 const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort)
321
322 return res.json(getFormattedObjects(resultList.data, resultList.total))
323}
324
325async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
326 const options = {
327 arguments: [ req, res ],
328 errorMessage: 'Cannot remove the video with many retries.'
329 }
330
331 await retryTransactionWrapper(removeVideo, options)
332
333 return res.type('json').status(204).end()
334}
335
336async function removeVideo (req: express.Request, res: express.Response) {
337 const videoInstance: VideoModel = res.locals.video
338
339 await sequelizeTypescript.transaction(async t => {
340 await videoInstance.destroy({ transaction: t })
341 })
342
343 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
344}
345
346async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
347 const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags(
348 req.query.search,
349 req.query.start,
350 req.query.count,
351 req.query.sort
352 )
353
354 return res.json(getFormattedObjects(resultList.data, resultList.total))
355}