]>
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' |
94a5ff8a | 15 | import { JobQueue } from '../../../lib/job-queue' |
65fcc311 | 16 | import { |
f05a1c30 | 17 | asyncMiddleware, authenticate, paginationValidator, setDefaultSort, setDefaultPagination, 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, |
f05a1c30 | 48 | setDefaultPagination, |
eb080476 | 49 | asyncMiddleware(listVideos) |
fbf1134e | 50 | ) |
f3aaa9a9 C |
51 | videosRouter.get('/search', |
52 | videosSearchValidator, | |
53 | paginationValidator, | |
54 | videosSortValidator, | |
1174a847 | 55 | setDefaultSort, |
f05a1c30 | 56 | setDefaultPagination, |
f3aaa9a9 C |
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 | |
94a5ff8a | 179 | const videoCreated = await sequelizeTypescript.transaction(async t => { |
e11f68a3 | 180 | const sequelizeOptions = { transaction: t } |
eb080476 | 181 | |
eb080476 C |
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 | |
7920c273 | 185 | |
eb080476 | 186 | videoFile.videoId = video.id |
eb080476 | 187 | await videoFile.save(sequelizeOptions) |
e11f68a3 C |
188 | |
189 | video.VideoFiles = [ videoFile ] | |
93e1258c | 190 | |
eb080476 | 191 | if (videoInfo.tags) { |
3fd3ab2d | 192 | const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t) |
eb080476 | 193 | |
3fd3ab2d | 194 | await video.$set('Tags', tagInstances, sequelizeOptions) |
eb080476 C |
195 | video.Tags = tagInstances |
196 | } | |
197 | ||
198 | // Let transcoding job send the video to friends because the video file extension might change | |
cadb46d8 | 199 | if (CONFIG.TRANSCODING.ENABLED === true) return videoCreated |
60862425 | 200 | // Don't send video to remote servers, it is private |
cadb46d8 | 201 | if (video.privacy === VideoPrivacy.PRIVATE) return videoCreated |
eb080476 | 202 | |
50d6de9c | 203 | await sendCreateVideo(video, t) |
a7d647c4 | 204 | await shareVideoByServerAndChannel(video, t) |
eb080476 | 205 | |
cadb46d8 C |
206 | logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) |
207 | ||
208 | return videoCreated | |
209 | }) | |
94a5ff8a C |
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 | |
7b1f49de C |
221 | } |
222 | ||
eb080476 | 223 | async function updateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
d6a5b018 C |
224 | const options = { |
225 | arguments: [ req, res ], | |
226 | errorMessage: 'Cannot update the video with many retries.' | |
227 | } | |
ed04d94f | 228 | |
eb080476 C |
229 | await retryTransactionWrapper(updateVideo, options) |
230 | ||
231 | return res.type('json').status(204).end() | |
ed04d94f C |
232 | } |
233 | ||
eb080476 | 234 | async function updateVideo (req: express.Request, res: express.Response) { |
3fd3ab2d | 235 | const videoInstance: VideoModel = res.locals.video |
7f4e7c36 | 236 | const videoFieldsSave = videoInstance.toJSON() |
556ddc31 | 237 | const videoInfoToUpdate: VideoUpdate = req.body |
fd45e8f4 | 238 | const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE |
7b1f49de | 239 | |
eb080476 | 240 | try { |
3fd3ab2d | 241 | await sequelizeTypescript.transaction(async t => { |
eb080476 C |
242 | const sequelizeOptions = { |
243 | transaction: t | |
244 | } | |
7b1f49de | 245 | |
eb080476 C |
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) | |
f595d394 | 251 | if (videoInfoToUpdate.privacy !== undefined) videoInstance.set('privacy', parseInt(videoInfoToUpdate.privacy.toString(), 10)) |
eb080476 | 252 | if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description) |
47564bbe | 253 | if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled) |
7b1f49de | 254 | |
54141398 | 255 | const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) |
7b1f49de | 256 | |
eb080476 | 257 | if (videoInfoToUpdate.tags) { |
3fd3ab2d | 258 | const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t) |
7b1f49de | 259 | |
3fd3ab2d | 260 | await videoInstance.$set('Tags', tagInstances, sequelizeOptions) |
eb080476 C |
261 | videoInstance.Tags = tagInstances |
262 | } | |
7920c273 | 263 | |
eb080476 | 264 | // Now we'll update the video's meta data to our friends |
fd45e8f4 | 265 | if (wasPrivateVideo === false) { |
54141398 | 266 | await sendUpdateVideo(videoInstanceUpdated, t) |
fd45e8f4 C |
267 | } |
268 | ||
60862425 | 269 | // Video is not private anymore, send a create action to remote servers |
f595d394 | 270 | if (wasPrivateVideo === true && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE) { |
50d6de9c | 271 | await sendCreateVideo(videoInstanceUpdated, t) |
a7d647c4 | 272 | await shareVideoByServerAndChannel(videoInstanceUpdated, t) |
fd45e8f4 | 273 | } |
eb080476 | 274 | }) |
6fcd19ba | 275 | |
eb080476 C |
276 | logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid) |
277 | } catch (err) { | |
6fcd19ba C |
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! | |
eb080476 | 281 | resetSequelizeInstance(videoInstance, videoFieldsSave) |
6fcd19ba C |
282 | |
283 | throw err | |
eb080476 | 284 | } |
9f10b292 | 285 | } |
8c308c2b | 286 | |
1f3e9fec C |
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) { | |
818f7987 | 294 | const videoInstance = res.locals.video |
9e167724 | 295 | |
1f3e9fec | 296 | await videoInstance.increment('views') |
50d6de9c | 297 | const serverAccount = await getServerActor() |
40ff5707 | 298 | |
9e167724 | 299 | if (videoInstance.isOwned()) { |
1f3e9fec | 300 | await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined) |
e4c87ec2 | 301 | } else { |
1f3e9fec | 302 | await sendCreateViewToOrigin(serverAccount, videoInstance, undefined) |
9e167724 C |
303 | } |
304 | ||
1f3e9fec | 305 | return res.status(204).end() |
9f10b292 | 306 | } |
8c308c2b | 307 | |
9567011b C |
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 { | |
571389d4 | 315 | description = await fetchRemoteVideoDescription(videoInstance) |
9567011b C |
316 | } |
317 | ||
318 | return res.json({ description }) | |
319 | } | |
320 | ||
eb080476 | 321 | async function listVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 322 | const resultList = await VideoModel.listForApi(req.query.start, req.query.count, req.query.sort) |
eb080476 C |
323 | |
324 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 325 | } |
c45f7f84 | 326 | |
eb080476 | 327 | async function removeVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { |
91f6f169 C |
328 | const options = { |
329 | arguments: [ req, res ], | |
330 | errorMessage: 'Cannot remove the video with many retries.' | |
331 | } | |
8c308c2b | 332 | |
eb080476 C |
333 | await retryTransactionWrapper(removeVideo, options) |
334 | ||
335 | return res.type('json').status(204).end() | |
91f6f169 C |
336 | } |
337 | ||
eb080476 | 338 | async function removeVideo (req: express.Request, res: express.Response) { |
3fd3ab2d | 339 | const videoInstance: VideoModel = res.locals.video |
91f6f169 | 340 | |
3fd3ab2d | 341 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 342 | await videoInstance.destroy({ transaction: t }) |
91f6f169 | 343 | }) |
eb080476 C |
344 | |
345 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) | |
9f10b292 | 346 | } |
8c308c2b | 347 | |
eb080476 | 348 | async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { |
3fd3ab2d | 349 | const resultList = await VideoModel.searchAndPopulateAccountAndServerAndTags( |
f3aaa9a9 | 350 | req.query.search, |
eb080476 C |
351 | req.query.start, |
352 | req.query.count, | |
353 | req.query.sort | |
354 | ) | |
355 | ||
356 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 357 | } |