]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
93e1258c | 2 | import { extname, join } from 'path' |
571389d4 | 3 | import { VideoCreate, VideoPrivacy, VideoUpdate } from '../../../../shared' |
da854ddd C |
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' | |
47564bbe | 8 | import { createReqFiles, getFormattedObjects, getServerActor, resetSequelizeInstance } from '../../../helpers/utils' |
65fcc311 | 9 | import { |
da854ddd | 10 | CONFIG, sequelizeTypescript, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, |
3fd3ab2d C |
11 | VIDEO_PRIVACIES |
12 | } from '../../../initializers' | |
da854ddd | 13 | import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub' |
50d6de9c | 14 | import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send' |
3fd3ab2d | 15 | import { transcodingJobScheduler } from '../../../lib/jobs/transcoding-job-scheduler' |
65fcc311 | 16 | import { |
1174a847 | 17 | asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setPagination, videosAddValidator, videosGetValidator, |
da854ddd | 18 | videosRemoveValidator, videosSearchValidator, videosSortValidator, videosUpdateValidator |
65fcc311 | 19 | } from '../../../middlewares' |
3fd3ab2d C |
20 | import { TagModel } from '../../../models/video/tag' |
21 | import { VideoModel } from '../../../models/video/video' | |
22 | import { VideoFileModel } from '../../../models/video/video-file' | |
65fcc311 C |
23 | import { abuseVideoRouter } from './abuse' |
24 | import { blacklistRouter } from './blacklist' | |
72c7248b | 25 | import { videoChannelRouter } from './channel' |
bf1f6508 | 26 | import { videoCommentRouter } from './comment' |
571389d4 | 27 | import { rateVideoRouter } from './rate' |
65fcc311 C |
28 | |
29 | const videosRouter = express.Router() | |
9f10b292 | 30 | |
c5911fd3 | 31 | const reqVideoFile = createReqFiles('videofile', CONFIG.STORAGE.VIDEOS_DIR, VIDEO_MIMETYPE_EXT) |
8c308c2b | 32 | |
65fcc311 C |
33 | videosRouter.use('/', abuseVideoRouter) |
34 | videosRouter.use('/', blacklistRouter) | |
35 | videosRouter.use('/', rateVideoRouter) | |
72c7248b | 36 | videosRouter.use('/', videoChannelRouter) |
bf1f6508 | 37 | videosRouter.use('/', videoCommentRouter) |
d33242b0 | 38 | |
65fcc311 C |
39 | videosRouter.get('/categories', listVideoCategories) |
40 | videosRouter.get('/licences', listVideoLicences) | |
41 | videosRouter.get('/languages', listVideoLanguages) | |
fd45e8f4 | 42 | videosRouter.get('/privacies', listVideoPrivacies) |
6e07c3de | 43 | |
65fcc311 C |
44 | videosRouter.get('/', |
45 | paginationValidator, | |
46 | videosSortValidator, | |
1174a847 | 47 | setDefaultSort, |
65fcc311 | 48 | setPagination, |
eb080476 | 49 | asyncMiddleware(listVideos) |
fbf1134e | 50 | ) |
f3aaa9a9 C |
51 | videosRouter.get('/search', |
52 | videosSearchValidator, | |
53 | paginationValidator, | |
54 | videosSortValidator, | |
1174a847 | 55 | setDefaultSort, |
f3aaa9a9 C |
56 | setPagination, |
57 | asyncMiddleware(searchVideos) | |
58 | ) | |
65fcc311 C |
59 | videosRouter.put('/:id', |
60 | authenticate, | |
a2431b7d | 61 | asyncMiddleware(videosUpdateValidator), |
eb080476 | 62 | asyncMiddleware(updateVideoRetryWrapper) |
7b1f49de | 63 | ) |
e95561cd | 64 | videosRouter.post('/upload', |
65fcc311 | 65 | authenticate, |
c5911fd3 | 66 | reqVideoFile, |
3fd3ab2d | 67 | asyncMiddleware(videosAddValidator), |
eb080476 | 68 | asyncMiddleware(addVideoRetryWrapper) |
fbf1134e | 69 | ) |
9567011b C |
70 | |
71 | videosRouter.get('/:id/description', | |
a2431b7d | 72 | asyncMiddleware(videosGetValidator), |
9567011b C |
73 | asyncMiddleware(getVideoDescription) |
74 | ) | |
65fcc311 | 75 | videosRouter.get('/:id', |
a2431b7d | 76 | asyncMiddleware(videosGetValidator), |
68ce3ae0 | 77 | getVideo |
fbf1134e | 78 | ) |
1f3e9fec C |
79 | videosRouter.post('/:id/views', |
80 | asyncMiddleware(videosGetValidator), | |
81 | asyncMiddleware(viewVideo) | |
82 | ) | |
198b205c | 83 | |
65fcc311 C |
84 | videosRouter.delete('/:id', |
85 | authenticate, | |
a2431b7d | 86 | asyncMiddleware(videosRemoveValidator), |
eb080476 | 87 | asyncMiddleware(removeVideoRetryWrapper) |
fbf1134e | 88 | ) |
198b205c | 89 | |
9f10b292 | 90 | // --------------------------------------------------------------------------- |
c45f7f84 | 91 | |
65fcc311 C |
92 | export { |
93 | videosRouter | |
94 | } | |
c45f7f84 | 95 | |
9f10b292 | 96 | // --------------------------------------------------------------------------- |
c45f7f84 | 97 | |
556ddc31 | 98 | function listVideoCategories (req: express.Request, res: express.Response) { |
65fcc311 | 99 | res.json(VIDEO_CATEGORIES) |
6e07c3de C |
100 | } |
101 | ||
556ddc31 | 102 | function listVideoLicences (req: express.Request, res: express.Response) { |
65fcc311 | 103 | res.json(VIDEO_LICENCES) |
6f0c39e2 C |
104 | } |
105 | ||
556ddc31 | 106 | function listVideoLanguages (req: express.Request, res: express.Response) { |
65fcc311 | 107 | res.json(VIDEO_LANGUAGES) |
3092476e C |
108 | } |
109 | ||
fd45e8f4 C |
110 | function listVideoPrivacies (req: express.Request, res: express.Response) { |
111 | res.json(VIDEO_PRIVACIES) | |
112 | } | |
113 | ||
ed04d94f C |
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 | |
eb080476 | 116 | async function addVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
d6a5b018 | 117 | const options = { |
556ddc31 | 118 | arguments: [ req, res, req.files['videofile'][0] ], |
d6a5b018 C |
119 | errorMessage: 'Cannot insert the video with many retries.' |
120 | } | |
ed04d94f | 121 | |
cadb46d8 | 122 | const video = await retryTransactionWrapper(addVideo, options) |
eb080476 | 123 | |
cadb46d8 C |
124 | res.json({ |
125 | video: { | |
126 | id: video.id, | |
127 | uuid: video.uuid | |
128 | } | |
129 | }).end() | |
ed04d94f C |
130 | } |
131 | ||
e11f68a3 | 132 | async function addVideo (req: express.Request, res: express.Response, videoPhysicalFile: Express.Multer.File) { |
556ddc31 | 133 | const videoInfo: VideoCreate = req.body |
9f10b292 | 134 | |
e11f68a3 C |
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, | |
47564bbe | 143 | commentsEnabled: videoInfo.commentsEnabled, |
e11f68a3 C |
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 | } | |
3fd3ab2d | 150 | const video = new VideoModel(videoData) |
e11f68a3 | 151 | video.url = getVideoActivityPubUrl(video) |
eb080476 | 152 | |
e11f68a3 C |
153 | const videoFilePath = join(CONFIG.STORAGE.VIDEOS_DIR, videoPhysicalFile.filename) |
154 | const videoFileHeight = await getVideoFileHeight(videoFilePath) | |
6fcd19ba | 155 | |
e11f68a3 C |
156 | const videoFileData = { |
157 | extname: extname(videoPhysicalFile.filename), | |
158 | resolution: videoFileHeight, | |
159 | size: videoPhysicalFile.size | |
160 | } | |
3fd3ab2d | 161 | const videoFile = new VideoFileModel(videoFileData) |
e11f68a3 C |
162 | const videoDir = CONFIG.STORAGE.VIDEOS_DIR |
163 | const source = join(videoDir, videoPhysicalFile.filename) | |
164 | const destination = join(videoDir, video.getVideoFilename(videoFile)) | |
93e1258c | 165 | |
e11f68a3 C |
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) | |
eb080476 | 169 | |
e11f68a3 | 170 | const tasks = [] |
eb080476 | 171 | |
e11f68a3 C |
172 | tasks.push( |
173 | video.createTorrentAndSetInfoHash(videoFile), | |
174 | video.createThumbnail(videoFile), | |
175 | video.createPreview(videoFile) | |
176 | ) | |
177 | await Promise.all(tasks) | |
eb080476 | 178 | |
3fd3ab2d | 179 | return sequelizeTypescript.transaction(async t => { |
e11f68a3 | 180 | const sequelizeOptions = { transaction: t } |
eb080476 C |
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 | ||
e11f68a3 | 188 | await transcodingJobScheduler.createJob(t, 'videoFileOptimizer', dataInput) |
eb080476 | 189 | } |
93e1258c | 190 | |
eb080476 C |
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 | |
7920c273 | 194 | |
eb080476 | 195 | videoFile.videoId = video.id |
eb080476 | 196 | await videoFile.save(sequelizeOptions) |
e11f68a3 C |
197 | |
198 | video.VideoFiles = [ videoFile ] | |
93e1258c | 199 | |
eb080476 | 200 | if (videoInfo.tags) { |
3fd3ab2d | 201 | const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t) |
eb080476 | 202 | |
3fd3ab2d | 203 | await video.$set('Tags', tagInstances, sequelizeOptions) |
eb080476 C |
204 | video.Tags = tagInstances |
205 | } | |
206 | ||
207 | // Let transcoding job send the video to friends because the video file extension might change | |
cadb46d8 | 208 | if (CONFIG.TRANSCODING.ENABLED === true) return videoCreated |
60862425 | 209 | // Don't send video to remote servers, it is private |
cadb46d8 | 210 | if (video.privacy === VideoPrivacy.PRIVATE) return videoCreated |
eb080476 | 211 | |
50d6de9c | 212 | await sendCreateVideo(video, t) |
a7d647c4 | 213 | await shareVideoByServerAndChannel(video, t) |
eb080476 | 214 | |
cadb46d8 C |
215 | logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) |
216 | ||
217 | return videoCreated | |
218 | }) | |
7b1f49de C |
219 | } |
220 | ||
eb080476 | 221 | async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
d6a5b018 C |
222 | const options = { |
223 | arguments: [ req, res ], | |
224 | errorMessage: 'Cannot update the video with many retries.' | |
225 | } | |
ed04d94f | 226 | |
eb080476 C |
227 | await retryTransactionWrapper(updateVideo, options) |
228 | ||
229 | return res.type('json').status(204).end() | |
ed04d94f C |
230 | } |
231 | ||
eb080476 | 232 | async function updateVideo (req: express.Request, res: express.Response) { |
3fd3ab2d | 233 | const videoInstance: VideoModel = res.locals.video |
7f4e7c36 | 234 | const videoFieldsSave = videoInstance.toJSON() |
556ddc31 | 235 | const videoInfoToUpdate: VideoUpdate = req.body |
fd45e8f4 | 236 | const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE |
7b1f49de | 237 | |
eb080476 | 238 | try { |
3fd3ab2d | 239 | await sequelizeTypescript.transaction(async t => { |
eb080476 C |
240 | const sequelizeOptions = { |
241 | transaction: t | |
242 | } | |
7b1f49de | 243 | |
eb080476 C |
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) | |
f595d394 | 249 | if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', parseInt(videoInfoToUpdate.privacy.toString(), 10)) |
eb080476 | 250 | if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description) |
47564bbe | 251 | if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled) |
7b1f49de | 252 | |
54141398 | 253 | const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) |
7b1f49de | 254 | |
eb080476 | 255 | if (videoInfoToUpdate.tags) { |
3fd3ab2d | 256 | const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t) |
7b1f49de | 257 | |
3fd3ab2d | 258 | await videoInstance.$set('Tags', tagInstances, sequelizeOptions) |
eb080476 C |
259 | videoInstance.Tags = tagInstances |
260 | } | |
7920c273 | 261 | |
eb080476 | 262 | // Now we'll update the video's meta data to our friends |
fd45e8f4 | 263 | if (wasPrivateVideo === false) { |
54141398 | 264 | await sendUpdateVideo(videoInstanceUpdated, t) |
fd45e8f4 C |
265 | } |
266 | ||
60862425 | 267 | // Video is not private anymore, send a create action to remote servers |
f595d394 | 268 | if (wasPrivateVideo === true && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE) { |
50d6de9c | 269 | await sendCreateVideo(videoInstanceUpdated, t) |
a7d647c4 | 270 | await shareVideoByServerAndChannel(videoInstanceUpdated, t) |
fd45e8f4 | 271 | } |
eb080476 | 272 | }) |
6fcd19ba | 273 | |
eb080476 C |
274 | logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid) |
275 | } catch (err) { | |
6fcd19ba C |
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! | |
eb080476 | 279 | resetSequelizeInstance(videoInstance, videoFieldsSave) |
6fcd19ba C |
280 | |
281 | throw err | |
eb080476 | 282 | } |
9f10b292 | 283 | } |
8c308c2b | 284 | |
1f3e9fec C |
285 | function getVideo (req: express.Request, res: express.Response) { |
286 | const videoInstance = res.locals.video | |
287 | ||
288 | return res.json(videoInstance.toFormattedDetailsJSON()) | |
289 | } | |
290 | ||
291 | async function viewVideo (req: express.Request, res: express.Response) { | |
818f7987 | 292 | const videoInstance = res.locals.video |
9e167724 | 293 | |
1f3e9fec | 294 | await videoInstance.increment('views') |
50d6de9c | 295 | const serverAccount = await getServerActor() |
40ff5707 | 296 | |
9e167724 | 297 | if (videoInstance.isOwned()) { |
1f3e9fec | 298 | await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined) |
e4c87ec2 | 299 | } else { |
1f3e9fec | 300 | await sendCreateViewToOrigin(serverAccount, videoInstance, undefined) |
9e167724 C |
301 | } |
302 | ||
1f3e9fec | 303 | return res.status(204).end() |
9f10b292 | 304 | } |
8c308c2b | 305 | |
9567011b C |
306 | async 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 { | |
571389d4 | 313 | description = await fetchRemoteVideoDescription(videoInstance) |
9567011b C |
314 | } |
315 | ||
316 | return res.json({ description }) | |
317 | } | |
318 | ||
eb080476 | 319 | async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 320 | const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 C |
321 | |
322 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 323 | } |
c45f7f84 | 324 | |
eb080476 | 325 | async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
91f6f169 C |
326 | const options = { |
327 | arguments: [ req, res ], | |
328 | errorMessage: 'Cannot remove the video with many retries.' | |
329 | } | |
8c308c2b | 330 | |
eb080476 C |
331 | await retryTransactionWrapper(removeVideo, options) |
332 | ||
333 | return res.type('json').status(204).end() | |
91f6f169 C |
334 | } |
335 | ||
eb080476 | 336 | async function removeVideo (req: express.Request, res: express.Response) { |
3fd3ab2d | 337 | const videoInstance: VideoModel = res.locals.video |
91f6f169 | 338 | |
3fd3ab2d | 339 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 340 | await videoInstance.destroy({ transaction: t }) |
91f6f169 | 341 | }) |
eb080476 C |
342 | |
343 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) | |
9f10b292 | 344 | } |
8c308c2b | 345 | |
eb080476 | 346 | async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 347 | const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags( |
f3aaa9a9 | 348 | req.query.search, |
eb080476 C |
349 | req.query.start, |
350 | req.query.count, | |
351 | req.query.sort | |
352 | ) | |
353 | ||
354 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 355 | } |