]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/index.ts
Add ability to reset our password
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
1 import * as express from 'express'
2 import { extname, join } from 'path'
3 import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared'
4 import { renamePromise } from '../../../helpers/core-utils'
5 import { retryTransactionWrapper } from '../../../helpers/database-utils'
6 import { getVideoFileHeight } from '../../../helpers/ffmpeg-utils'
7 import { logger } from '../../../helpers/logger'
8 import { createReqFiles, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils'
9 import {
10 CONFIG, sequelizeTypescript, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT,
11 VIDEO_PRIVACIES
12 } from '../../../initializers'
13 import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub'
14 import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
15 import { JobQueue } from '../../../lib/job-queue'
16 import {
17 asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setDefaultPagination, videosAddValidator, videosGetValidator,
18 videosRemoveValidator, videosSearchValidator, videosSortValidator, videosUpdateValidator
19 } from '../../../middlewares'
20 import { TagModel } from '../../../models/video/tag'
21 import { VideoModel } from '../../../models/video/video'
22 import { VideoFileModel } from '../../../models/video/video-file'
23 import { abuseVideoRouter } from './abuse'
24 import { blacklistRouter } from './blacklist'
25 import { videoChannelRouter } from './channel'
26 import { videoCommentRouter } from './comment'
27 import { rateVideoRouter } from './rate'
28
29 const videosRouter = express.Router()
30
31 const reqVideoFile = createReqFiles('videofile', CONFIG.STORAGE.VIDEOS_DIR, VIDEO_MIMETYPE_EXT)
32
33 videosRouter.use('/', abuseVideoRouter)
34 videosRouter.use('/', blacklistRouter)
35 videosRouter.use('/', rateVideoRouter)
36 videosRouter.use('/', videoChannelRouter)
37 videosRouter.use('/', videoCommentRouter)
38
39 videosRouter.get('/categories', listVideoCategories)
40 videosRouter.get('/licences', listVideoLicences)
41 videosRouter.get('/languages', listVideoLanguages)
42 videosRouter.get('/privacies', listVideoPrivacies)
43
44 videosRouter.get('/',
45 paginationValidator,
46 videosSortValidator,
47 setDefaultSort,
48 setDefaultPagination,
49 asyncMiddleware(listVideos)
50 )
51 videosRouter.get('/search',
52 videosSearchValidator,
53 paginationValidator,
54 videosSortValidator,
55 setDefaultSort,
56 setDefaultPagination,
57 asyncMiddleware(searchVideos)
58 )
59 videosRouter.put('/:id',
60 authenticate,
61 asyncMiddleware(videosUpdateValidator),
62 asyncMiddleware(updateVideoRetryWrapper)
63 )
64 videosRouter.post('/upload',
65 authenticate,
66 reqVideoFile,
67 asyncMiddleware(videosAddValidator),
68 asyncMiddleware(addVideoRetryWrapper)
69 )
70
71 videosRouter.get('/:id/description',
72 asyncMiddleware(videosGetValidator),
73 asyncMiddleware(getVideoDescription)
74 )
75 videosRouter.get('/:id',
76 asyncMiddleware(videosGetValidator),
77 getVideo
78 )
79 videosRouter.post('/:id/views',
80 asyncMiddleware(videosGetValidator),
81 asyncMiddleware(viewVideo)
82 )
83
84 videosRouter.delete('/:id',
85 authenticate,
86 asyncMiddleware(videosRemoveValidator),
87 asyncMiddleware(removeVideoRetryWrapper)
88 )
89
90 // ---------------------------------------------------------------------------
91
92 export {
93 videosRouter
94 }
95
96 // ---------------------------------------------------------------------------
97
98 function listVideoCategories (req: express.Request, res: express.Response) {
99 res.json(VIDEO_CATEGORIES)
100 }
101
102 function listVideoLicences (req: express.Request, res: express.Response) {
103 res.json(VIDEO_LICENCES)
104 }
105
106 function listVideoLanguages (req: express.Request, res: express.Response) {
107 res.json(VIDEO_LANGUAGES)
108 }
109
110 function 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
116 async 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
132 async 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 const videoCreated = await sequelizeTypescript.transaction(async t => {
180 const sequelizeOptions = { transaction: t }
181
182 const videoCreated = await video.save(sequelizeOptions)
183 // Do not forget to add video channel information to the created video
184 videoCreated.VideoChannel = res.locals.videoChannel
185
186 videoFile.videoId = video.id
187 await videoFile.save(sequelizeOptions)
188
189 video.VideoFiles = [ videoFile ]
190
191 if (videoInfo.tags) {
192 const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t)
193
194 await video.$set('Tags', tagInstances, sequelizeOptions)
195 video.Tags = tagInstances
196 }
197
198 // Let transcoding job send the video to friends because the video file extension might change
199 if (CONFIG.TRANSCODING.ENABLED === true) return videoCreated
200 // Don't send video to remote servers, it is private
201 if (video.privacy === VideoPrivacy.PRIVATE) return videoCreated
202
203 await sendCreateVideo(video, t)
204 await shareVideoByServerAndChannel(video, t)
205
206 logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid)
207
208 return videoCreated
209 })
210
211 if (CONFIG.TRANSCODING.ENABLED === true) {
212 // Put uuid because we don't have id auto incremented for now
213 const dataInput = {
214 videoUUID: videoCreated.uuid
215 }
216
217 await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput })
218 }
219
220 return videoCreated
221 }
222
223 async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
224 const options = {
225 arguments: [ req, res ],
226 errorMessage: 'Cannot update the video with many retries.'
227 }
228
229 await retryTransactionWrapper(updateVideo, options)
230
231 return res.type('json').status(204).end()
232 }
233
234 async function updateVideo (req: express.Request, res: express.Response) {
235 const videoInstance: VideoModel = res.locals.video
236 const videoFieldsSave = videoInstance.toJSON()
237 const videoInfoToUpdate: VideoUpdate = req.body
238 const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE
239
240 try {
241 await sequelizeTypescript.transaction(async t => {
242 const sequelizeOptions = {
243 transaction: t
244 }
245
246 if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name)
247 if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category)
248 if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence)
249 if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language)
250 if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw)
251 if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', parseInt(videoInfoToUpdate.privacy.toString(), 10))
252 if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description)
253 if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled)
254
255 const videoInstanceUpdated = await videoInstance.save(sequelizeOptions)
256
257 if (videoInfoToUpdate.tags) {
258 const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t)
259
260 await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
261 videoInstance.Tags = tagInstances
262 }
263
264 // Now we'll update the video's meta data to our friends
265 if (wasPrivateVideo === false) {
266 await sendUpdateVideo(videoInstanceUpdated, t)
267 }
268
269 // Video is not private anymore, send a create action to remote servers
270 if (wasPrivateVideo === true && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE) {
271 await sendCreateVideo(videoInstanceUpdated, t)
272 await shareVideoByServerAndChannel(videoInstanceUpdated, t)
273 }
274 })
275
276 logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
277 } catch (err) {
278 // Force fields we want to update
279 // If the transaction is retried, sequelize will think the object has not changed
280 // So it will skip the SQL request, even if the last one was ROLLBACKed!
281 resetSequelizeInstance(videoInstance, videoFieldsSave)
282
283 throw err
284 }
285 }
286
287 function getVideo (req: express.Request, res: express.Response) {
288 const videoInstance = res.locals.video
289
290 return res.json(videoInstance.toFormattedDetailsJSON())
291 }
292
293 async function viewVideo (req: express.Request, res: express.Response) {
294 const videoInstance = res.locals.video
295
296 await videoInstance.increment('views')
297 const serverAccount = await getServerActor()
298
299 if (videoInstance.isOwned()) {
300 await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
301 } else {
302 await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
303 }
304
305 return res.status(204).end()
306 }
307
308 async function getVideoDescription (req: express.Request, res: express.Response) {
309 const videoInstance = res.locals.video
310 let description = ''
311
312 if (videoInstance.isOwned()) {
313 description = videoInstance.description
314 } else {
315 description = await fetchRemoteVideoDescription(videoInstance)
316 }
317
318 return res.json({ description })
319 }
320
321 async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
322 const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort)
323
324 return res.json(getFormattedObjects(resultList.data, resultList.total))
325 }
326
327 async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
328 const options = {
329 arguments: [ req, res ],
330 errorMessage: 'Cannot remove the video with many retries.'
331 }
332
333 await retryTransactionWrapper(removeVideo, options)
334
335 return res.type('json').status(204).end()
336 }
337
338 async function removeVideo (req: express.Request, res: express.Response) {
339 const videoInstance: VideoModel = res.locals.video
340
341 await sequelizeTypescript.transaction(async t => {
342 await videoInstance.destroy({ transaction: t })
343 })
344
345 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
346 }
347
348 async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
349 const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags(
350 req.query.search,
351 req.query.start,
352 req.query.count,
353 req.query.sort
354 )
355
356 return res.json(getFormattedObjects(resultList.data, resultList.total))
357 }