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