]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
93e1258c | 2 | import { extname, join } from 'path' |
2186386c | 3 | import { VideoCreate, VideoPrivacy, VideoState, VideoUpdate } from '../../../../shared' |
3a6f351b | 4 | import { getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' |
ac81d1a0 | 5 | import { processImage } from '../../../helpers/image-utils' |
da854ddd | 6 | import { logger } from '../../../helpers/logger' |
993cef4b | 7 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' |
06215f15 | 8 | import { getFormattedObjects, getServerActor } from '../../../helpers/utils' |
65fcc311 | 9 | import { |
cef534ed C |
10 | CONFIG, |
11 | MIMETYPES, | |
ac81d1a0 C |
12 | PREVIEWS_SIZE, |
13 | sequelizeTypescript, | |
14 | THUMBNAILS_SIZE, | |
15 | VIDEO_CATEGORIES, | |
16 | VIDEO_LANGUAGES, | |
17 | VIDEO_LICENCES, | |
3fd3ab2d C |
18 | VIDEO_PRIVACIES |
19 | } from '../../../initializers' | |
0f320037 C |
20 | import { |
21 | changeVideoChannelShare, | |
2186386c | 22 | federateVideoIfNeeded, |
0f320037 | 23 | fetchRemoteVideoDescription, |
2186386c | 24 | getVideoActivityPubUrl |
0f320037 | 25 | } from '../../../lib/activitypub' |
2186386c | 26 | import { sendCreateView } from '../../../lib/activitypub/send' |
94a5ff8a | 27 | import { JobQueue } from '../../../lib/job-queue' |
b5c0e955 | 28 | import { Redis } from '../../../lib/redis' |
65fcc311 | 29 | import { |
ac81d1a0 | 30 | asyncMiddleware, |
90d4bb81 | 31 | asyncRetryTransactionMiddleware, |
ac81d1a0 | 32 | authenticate, |
8d427346 | 33 | checkVideoFollowConstraints, |
d525fc39 | 34 | commonVideosFiltersValidator, |
0883b324 | 35 | optionalAuthenticate, |
ac81d1a0 C |
36 | paginationValidator, |
37 | setDefaultPagination, | |
38 | setDefaultSort, | |
39 | videosAddValidator, | |
09209296 | 40 | videosCustomGetValidator, |
ac81d1a0 C |
41 | videosGetValidator, |
42 | videosRemoveValidator, | |
ac81d1a0 C |
43 | videosSortValidator, |
44 | videosUpdateValidator | |
65fcc311 | 45 | } from '../../../middlewares' |
3fd3ab2d C |
46 | import { TagModel } from '../../../models/video/tag' |
47 | import { VideoModel } from '../../../models/video/video' | |
48 | import { VideoFileModel } from '../../../models/video/video-file' | |
65fcc311 C |
49 | import { abuseVideoRouter } from './abuse' |
50 | import { blacklistRouter } from './blacklist' | |
bf1f6508 | 51 | import { videoCommentRouter } from './comment' |
571389d4 | 52 | import { rateVideoRouter } from './rate' |
74d63469 | 53 | import { ownershipVideoRouter } from './ownership' |
0883b324 | 54 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' |
06215f15 | 55 | import { buildNSFWFilter, createReqFiles } from '../../../helpers/express-utils' |
2baea0c7 | 56 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' |
40e87e9e | 57 | import { videoCaptionsRouter } from './captions' |
fbad87b0 | 58 | import { videoImportsRouter } from './import' |
06215f15 | 59 | import { resetSequelizeInstance } from '../../../helpers/database-utils' |
14e2014a | 60 | import { move } from 'fs-extra' |
6e46de09 | 61 | import { watchingRouter } from './watching' |
cef534ed | 62 | import { Notifier } from '../../../lib/notifier' |
65fcc311 | 63 | |
80e36cd9 | 64 | const auditLogger = auditLoggerFactory('videos') |
65fcc311 | 65 | const videosRouter = express.Router() |
9f10b292 | 66 | |
ac81d1a0 C |
67 | const reqVideoFileAdd = createReqFiles( |
68 | [ 'videofile', 'thumbnailfile', 'previewfile' ], | |
14e2014a | 69 | Object.assign({}, MIMETYPES.VIDEO.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT), |
ac81d1a0 | 70 | { |
6040f87d C |
71 | videofile: CONFIG.STORAGE.TMP_DIR, |
72 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, | |
73 | previewfile: CONFIG.STORAGE.TMP_DIR | |
ac81d1a0 C |
74 | } |
75 | ) | |
76 | const reqVideoFileUpdate = createReqFiles( | |
77 | [ 'thumbnailfile', 'previewfile' ], | |
14e2014a | 78 | MIMETYPES.IMAGE.MIMETYPE_EXT, |
ac81d1a0 | 79 | { |
6040f87d C |
80 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, |
81 | previewfile: CONFIG.STORAGE.TMP_DIR | |
ac81d1a0 C |
82 | } |
83 | ) | |
8c308c2b | 84 | |
65fcc311 C |
85 | videosRouter.use('/', abuseVideoRouter) |
86 | videosRouter.use('/', blacklistRouter) | |
87 | videosRouter.use('/', rateVideoRouter) | |
bf1f6508 | 88 | videosRouter.use('/', videoCommentRouter) |
40e87e9e | 89 | videosRouter.use('/', videoCaptionsRouter) |
fbad87b0 | 90 | videosRouter.use('/', videoImportsRouter) |
74d63469 | 91 | videosRouter.use('/', ownershipVideoRouter) |
6e46de09 | 92 | videosRouter.use('/', watchingRouter) |
d33242b0 | 93 | |
65fcc311 C |
94 | videosRouter.get('/categories', listVideoCategories) |
95 | videosRouter.get('/licences', listVideoLicences) | |
96 | videosRouter.get('/languages', listVideoLanguages) | |
fd45e8f4 | 97 | videosRouter.get('/privacies', listVideoPrivacies) |
6e07c3de | 98 | |
65fcc311 C |
99 | videosRouter.get('/', |
100 | paginationValidator, | |
101 | videosSortValidator, | |
1174a847 | 102 | setDefaultSort, |
f05a1c30 | 103 | setDefaultPagination, |
0883b324 | 104 | optionalAuthenticate, |
d525fc39 | 105 | commonVideosFiltersValidator, |
eb080476 | 106 | asyncMiddleware(listVideos) |
fbf1134e | 107 | ) |
65fcc311 C |
108 | videosRouter.put('/:id', |
109 | authenticate, | |
ac81d1a0 | 110 | reqVideoFileUpdate, |
a2431b7d | 111 | asyncMiddleware(videosUpdateValidator), |
90d4bb81 | 112 | asyncRetryTransactionMiddleware(updateVideo) |
7b1f49de | 113 | ) |
e95561cd | 114 | videosRouter.post('/upload', |
65fcc311 | 115 | authenticate, |
ac81d1a0 | 116 | reqVideoFileAdd, |
3fd3ab2d | 117 | asyncMiddleware(videosAddValidator), |
90d4bb81 | 118 | asyncRetryTransactionMiddleware(addVideo) |
fbf1134e | 119 | ) |
9567011b C |
120 | |
121 | videosRouter.get('/:id/description', | |
a2431b7d | 122 | asyncMiddleware(videosGetValidator), |
9567011b C |
123 | asyncMiddleware(getVideoDescription) |
124 | ) | |
65fcc311 | 125 | videosRouter.get('/:id', |
6e46de09 | 126 | optionalAuthenticate, |
09209296 | 127 | asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), |
8d427346 | 128 | asyncMiddleware(checkVideoFollowConstraints), |
09209296 | 129 | asyncMiddleware(getVideo) |
fbf1134e | 130 | ) |
1f3e9fec C |
131 | videosRouter.post('/:id/views', |
132 | asyncMiddleware(videosGetValidator), | |
133 | asyncMiddleware(viewVideo) | |
134 | ) | |
198b205c | 135 | |
65fcc311 C |
136 | videosRouter.delete('/:id', |
137 | authenticate, | |
a2431b7d | 138 | asyncMiddleware(videosRemoveValidator), |
90d4bb81 | 139 | asyncRetryTransactionMiddleware(removeVideo) |
fbf1134e | 140 | ) |
198b205c | 141 | |
9f10b292 | 142 | // --------------------------------------------------------------------------- |
c45f7f84 | 143 | |
65fcc311 C |
144 | export { |
145 | videosRouter | |
146 | } | |
c45f7f84 | 147 | |
9f10b292 | 148 | // --------------------------------------------------------------------------- |
c45f7f84 | 149 | |
556ddc31 | 150 | function listVideoCategories (req: express.Request, res: express.Response) { |
65fcc311 | 151 | res.json(VIDEO_CATEGORIES) |
6e07c3de C |
152 | } |
153 | ||
556ddc31 | 154 | function listVideoLicences (req: express.Request, res: express.Response) { |
65fcc311 | 155 | res.json(VIDEO_LICENCES) |
6f0c39e2 C |
156 | } |
157 | ||
556ddc31 | 158 | function listVideoLanguages (req: express.Request, res: express.Response) { |
65fcc311 | 159 | res.json(VIDEO_LANGUAGES) |
3092476e C |
160 | } |
161 | ||
fd45e8f4 C |
162 | function listVideoPrivacies (req: express.Request, res: express.Response) { |
163 | res.json(VIDEO_PRIVACIES) | |
164 | } | |
165 | ||
90d4bb81 | 166 | async function addVideo (req: express.Request, res: express.Response) { |
8b917537 C |
167 | // Processing the video could be long |
168 | // Set timeout to 10 minutes | |
169 | req.setTimeout(1000 * 60 * 10, () => { | |
170 | logger.error('Upload video has timed out.') | |
171 | return res.sendStatus(408) | |
172 | }) | |
173 | ||
90d4bb81 | 174 | const videoPhysicalFile = req.files['videofile'][0] |
556ddc31 | 175 | const videoInfo: VideoCreate = req.body |
9f10b292 | 176 | |
e11f68a3 C |
177 | // Prepare data so we don't block the transaction |
178 | const videoData = { | |
179 | name: videoInfo.name, | |
180 | remote: false, | |
e11f68a3 C |
181 | category: videoInfo.category, |
182 | licence: videoInfo.licence, | |
183 | language: videoInfo.language, | |
2186386c C |
184 | commentsEnabled: videoInfo.commentsEnabled || false, |
185 | waitTranscoding: videoInfo.waitTranscoding || false, | |
186 | state: CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED, | |
187 | nsfw: videoInfo.nsfw || false, | |
e11f68a3 | 188 | description: videoInfo.description, |
2422c46b | 189 | support: videoInfo.support, |
e11f68a3 C |
190 | privacy: videoInfo.privacy, |
191 | duration: videoPhysicalFile['duration'], // duration was added by a previous middleware | |
192 | channelId: res.locals.videoChannel.id | |
193 | } | |
3fd3ab2d | 194 | const video = new VideoModel(videoData) |
2186386c | 195 | video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object |
eb080476 | 196 | |
2186386c | 197 | // Build the file object |
056aa7f2 | 198 | const { videoFileResolution } = await getVideoFileResolution(videoPhysicalFile.path) |
3a6f351b C |
199 | const fps = await getVideoFileFPS(videoPhysicalFile.path) |
200 | ||
e11f68a3 C |
201 | const videoFileData = { |
202 | extname: extname(videoPhysicalFile.filename), | |
056aa7f2 | 203 | resolution: videoFileResolution, |
3a6f351b C |
204 | size: videoPhysicalFile.size, |
205 | fps | |
e11f68a3 | 206 | } |
3fd3ab2d | 207 | const videoFile = new VideoFileModel(videoFileData) |
2186386c C |
208 | |
209 | // Move physical file | |
e11f68a3 | 210 | const videoDir = CONFIG.STORAGE.VIDEOS_DIR |
e11f68a3 | 211 | const destination = join(videoDir, video.getVideoFilename(videoFile)) |
14e2014a | 212 | await move(videoPhysicalFile.path, destination) |
e3a682a8 C |
213 | // This is important in case if there is another attempt in the retry process |
214 | videoPhysicalFile.filename = video.getVideoFilename(videoFile) | |
82815eb6 | 215 | videoPhysicalFile.path = destination |
ac81d1a0 C |
216 | |
217 | // Process thumbnail or create it from the video | |
218 | const thumbnailField = req.files['thumbnailfile'] | |
219 | if (thumbnailField) { | |
220 | const thumbnailPhysicalFile = thumbnailField[0] | |
221 | await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName()), THUMBNAILS_SIZE) | |
222 | } else { | |
223 | await video.createThumbnail(videoFile) | |
224 | } | |
93e1258c | 225 | |
ac81d1a0 C |
226 | // Process preview or create it from the video |
227 | const previewField = req.files['previewfile'] | |
228 | if (previewField) { | |
229 | const previewPhysicalFile = previewField[0] | |
230 | await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, video.getPreviewName()), PREVIEWS_SIZE) | |
231 | } else { | |
232 | await video.createPreview(videoFile) | |
233 | } | |
eb080476 | 234 | |
2186386c | 235 | // Create the torrent file |
ac81d1a0 | 236 | await video.createTorrentAndSetInfoHash(videoFile) |
eb080476 | 237 | |
94a5ff8a | 238 | const videoCreated = await sequelizeTypescript.transaction(async t => { |
e11f68a3 | 239 | const sequelizeOptions = { transaction: t } |
eb080476 | 240 | |
eb080476 C |
241 | const videoCreated = await video.save(sequelizeOptions) |
242 | // Do not forget to add video channel information to the created video | |
243 | videoCreated.VideoChannel = res.locals.videoChannel | |
7920c273 | 244 | |
eb080476 | 245 | videoFile.videoId = video.id |
eb080476 | 246 | await videoFile.save(sequelizeOptions) |
e11f68a3 C |
247 | |
248 | video.VideoFiles = [ videoFile ] | |
93e1258c | 249 | |
2baea0c7 | 250 | // Create tags |
2efd32f6 | 251 | if (videoInfo.tags !== undefined) { |
3fd3ab2d | 252 | const tagInstances = await TagModel.findOrCreateTags(videoInfo.tags, t) |
eb080476 | 253 | |
3fd3ab2d | 254 | await video.$set('Tags', tagInstances, sequelizeOptions) |
eb080476 C |
255 | video.Tags = tagInstances |
256 | } | |
257 | ||
2baea0c7 C |
258 | // Schedule an update in the future? |
259 | if (videoInfo.scheduleUpdate) { | |
260 | await ScheduleVideoUpdateModel.create({ | |
261 | videoId: video.id, | |
262 | updateAt: videoInfo.scheduleUpdate.updateAt, | |
263 | privacy: videoInfo.scheduleUpdate.privacy || null | |
264 | }, { transaction: t }) | |
265 | } | |
266 | ||
2186386c | 267 | await federateVideoIfNeeded(video, true, t) |
eb080476 | 268 | |
993cef4b | 269 | auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) |
cadb46d8 C |
270 | logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) |
271 | ||
272 | return videoCreated | |
273 | }) | |
94a5ff8a | 274 | |
e8d246d5 C |
275 | Notifier.Instance.notifyOnNewVideo(videoCreated) |
276 | ||
2186386c | 277 | if (video.state === VideoState.TO_TRANSCODE) { |
94a5ff8a C |
278 | // Put uuid because we don't have id auto incremented for now |
279 | const dataInput = { | |
0c948c16 C |
280 | videoUUID: videoCreated.uuid, |
281 | isNewVideo: true | |
94a5ff8a C |
282 | } |
283 | ||
284 | await JobQueue.Instance.createJob({ type: 'video-file', payload: dataInput }) | |
285 | } | |
286 | ||
90d4bb81 C |
287 | return res.json({ |
288 | video: { | |
289 | id: videoCreated.id, | |
290 | uuid: videoCreated.uuid | |
291 | } | |
292 | }).end() | |
ed04d94f C |
293 | } |
294 | ||
eb080476 | 295 | async function updateVideo (req: express.Request, res: express.Response) { |
3fd3ab2d | 296 | const videoInstance: VideoModel = res.locals.video |
7f4e7c36 | 297 | const videoFieldsSave = videoInstance.toJSON() |
80e36cd9 | 298 | const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON()) |
556ddc31 | 299 | const videoInfoToUpdate: VideoUpdate = req.body |
fd45e8f4 | 300 | const wasPrivateVideo = videoInstance.privacy === VideoPrivacy.PRIVATE |
cef534ed | 301 | const wasUnlistedVideo = videoInstance.privacy === VideoPrivacy.UNLISTED |
7b1f49de | 302 | |
ac81d1a0 C |
303 | // Process thumbnail or create it from the video |
304 | if (req.files && req.files['thumbnailfile']) { | |
305 | const thumbnailPhysicalFile = req.files['thumbnailfile'][0] | |
306 | await processImage(thumbnailPhysicalFile, join(CONFIG.STORAGE.THUMBNAILS_DIR, videoInstance.getThumbnailName()), THUMBNAILS_SIZE) | |
307 | } | |
308 | ||
309 | // Process preview or create it from the video | |
310 | if (req.files && req.files['previewfile']) { | |
311 | const previewPhysicalFile = req.files['previewfile'][0] | |
312 | await processImage(previewPhysicalFile, join(CONFIG.STORAGE.PREVIEWS_DIR, videoInstance.getPreviewName()), PREVIEWS_SIZE) | |
313 | } | |
314 | ||
eb080476 | 315 | try { |
e8d246d5 C |
316 | const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => { |
317 | const sequelizeOptions = { transaction: t } | |
0f320037 | 318 | const oldVideoChannel = videoInstance.VideoChannel |
7b1f49de | 319 | |
eb080476 C |
320 | if (videoInfoToUpdate.name !== undefined) videoInstance.set('name', videoInfoToUpdate.name) |
321 | if (videoInfoToUpdate.category !== undefined) videoInstance.set('category', videoInfoToUpdate.category) | |
322 | if (videoInfoToUpdate.licence !== undefined) videoInstance.set('licence', videoInfoToUpdate.licence) | |
323 | if (videoInfoToUpdate.language !== undefined) videoInstance.set('language', videoInfoToUpdate.language) | |
324 | if (videoInfoToUpdate.nsfw !== undefined) videoInstance.set('nsfw', videoInfoToUpdate.nsfw) | |
2186386c | 325 | if (videoInfoToUpdate.waitTranscoding !== undefined) videoInstance.set('waitTranscoding', videoInfoToUpdate.waitTranscoding) |
2422c46b | 326 | if (videoInfoToUpdate.support !== undefined) videoInstance.set('support', videoInfoToUpdate.support) |
eb080476 | 327 | if (videoInfoToUpdate.description !== undefined) videoInstance.set('description', videoInfoToUpdate.description) |
47564bbe | 328 | if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.set('commentsEnabled', videoInfoToUpdate.commentsEnabled) |
2922e048 JLB |
329 | if (videoInfoToUpdate.privacy !== undefined) { |
330 | const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) | |
331 | videoInstance.set('privacy', newPrivacy) | |
332 | ||
333 | if (wasPrivateVideo === true && newPrivacy !== VideoPrivacy.PRIVATE) { | |
334 | videoInstance.set('publishedAt', new Date()) | |
335 | } | |
336 | } | |
7b1f49de | 337 | |
54141398 | 338 | const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) |
7b1f49de | 339 | |
0f320037 | 340 | // Video tags update? |
2efd32f6 | 341 | if (videoInfoToUpdate.tags !== undefined) { |
3fd3ab2d | 342 | const tagInstances = await TagModel.findOrCreateTags(videoInfoToUpdate.tags, t) |
7b1f49de | 343 | |
0f320037 C |
344 | await videoInstanceUpdated.$set('Tags', tagInstances, sequelizeOptions) |
345 | videoInstanceUpdated.Tags = tagInstances | |
eb080476 | 346 | } |
7920c273 | 347 | |
0f320037 C |
348 | // Video channel update? |
349 | if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) { | |
6200d8d9 | 350 | await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t }) |
2186386c | 351 | videoInstanceUpdated.VideoChannel = res.locals.videoChannel |
0f320037 C |
352 | |
353 | if (wasPrivateVideo === false) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) | |
fd45e8f4 C |
354 | } |
355 | ||
2baea0c7 C |
356 | // Schedule an update in the future? |
357 | if (videoInfoToUpdate.scheduleUpdate) { | |
358 | await ScheduleVideoUpdateModel.upsert({ | |
359 | videoId: videoInstanceUpdated.id, | |
360 | updateAt: videoInfoToUpdate.scheduleUpdate.updateAt, | |
361 | privacy: videoInfoToUpdate.scheduleUpdate.privacy || null | |
362 | }, { transaction: t }) | |
e94fc297 C |
363 | } else if (videoInfoToUpdate.scheduleUpdate === null) { |
364 | await ScheduleVideoUpdateModel.deleteByVideoId(videoInstanceUpdated.id, t) | |
2baea0c7 C |
365 | } |
366 | ||
2186386c | 367 | const isNewVideo = wasPrivateVideo && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE |
5abb9fbb C |
368 | |
369 | // Don't send update if the video was unfederated | |
370 | if (!videoInstanceUpdated.VideoBlacklist || videoInstanceUpdated.VideoBlacklist.unfederated === false) { | |
371 | await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t) | |
372 | } | |
6fcd19ba | 373 | |
80e36cd9 | 374 | auditLogger.update( |
993cef4b | 375 | getAuditIdFromRes(res), |
80e36cd9 AB |
376 | new VideoAuditView(videoInstanceUpdated.toFormattedDetailsJSON()), |
377 | oldVideoAuditView | |
378 | ) | |
379 | logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid) | |
e8d246d5 C |
380 | |
381 | return videoInstanceUpdated | |
80e36cd9 | 382 | }) |
e8d246d5 C |
383 | |
384 | if (wasUnlistedVideo || wasPrivateVideo) { | |
385 | Notifier.Instance.notifyOnNewVideo(videoInstanceUpdated) | |
386 | } | |
eb080476 | 387 | } catch (err) { |
6fcd19ba C |
388 | // Force fields we want to update |
389 | // If the transaction is retried, sequelize will think the object has not changed | |
390 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
eb080476 | 391 | resetSequelizeInstance(videoInstance, videoFieldsSave) |
6fcd19ba C |
392 | |
393 | throw err | |
eb080476 | 394 | } |
90d4bb81 C |
395 | |
396 | return res.type('json').status(204).end() | |
9f10b292 | 397 | } |
8c308c2b | 398 | |
09209296 C |
399 | async function getVideo (req: express.Request, res: express.Response) { |
400 | // We need more attributes | |
401 | const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null | |
402 | const video: VideoModel = await VideoModel.loadForGetAPI(res.locals.video.id, undefined, userId) | |
1f3e9fec | 403 | |
09209296 C |
404 | if (video.isOutdated()) { |
405 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } }) | |
406 | .catch(err => logger.error('Cannot create AP refresher job for video %s.', video.url, { err })) | |
04b8c3fb C |
407 | } |
408 | ||
09209296 | 409 | return res.json(video.toFormattedDetailsJSON()) |
1f3e9fec C |
410 | } |
411 | ||
412 | async function viewVideo (req: express.Request, res: express.Response) { | |
818f7987 | 413 | const videoInstance = res.locals.video |
9e167724 | 414 | |
490b595a | 415 | const ip = req.ip |
6b616860 | 416 | const exists = await Redis.Instance.isVideoIPViewExists(ip, videoInstance.uuid) |
b5c0e955 C |
417 | if (exists) { |
418 | logger.debug('View for ip %s and video %s already exists.', ip, videoInstance.uuid) | |
419 | return res.status(204).end() | |
420 | } | |
421 | ||
6b616860 C |
422 | await Promise.all([ |
423 | Redis.Instance.addVideoView(videoInstance.id), | |
424 | Redis.Instance.setIPVideoView(ip, videoInstance.uuid) | |
425 | ]) | |
b5c0e955 | 426 | |
a2377d15 | 427 | const serverActor = await getServerActor() |
dbe6aa69 | 428 | await sendCreateView(serverActor, videoInstance, undefined) |
9e167724 | 429 | |
1f3e9fec | 430 | return res.status(204).end() |
9f10b292 | 431 | } |
8c308c2b | 432 | |
9567011b C |
433 | async function getVideoDescription (req: express.Request, res: express.Response) { |
434 | const videoInstance = res.locals.video | |
435 | let description = '' | |
436 | ||
437 | if (videoInstance.isOwned()) { | |
438 | description = videoInstance.description | |
439 | } else { | |
571389d4 | 440 | description = await fetchRemoteVideoDescription(videoInstance) |
9567011b C |
441 | } |
442 | ||
443 | return res.json({ description }) | |
444 | } | |
445 | ||
04b8c3fb | 446 | async function listVideos (req: express.Request, res: express.Response) { |
48dce1c9 C |
447 | const resultList = await VideoModel.listForApi({ |
448 | start: req.query.start, | |
449 | count: req.query.count, | |
450 | sort: req.query.sort, | |
06a05d5f | 451 | includeLocalVideos: true, |
d525fc39 C |
452 | categoryOneOf: req.query.categoryOneOf, |
453 | licenceOneOf: req.query.licenceOneOf, | |
454 | languageOneOf: req.query.languageOneOf, | |
455 | tagsOneOf: req.query.tagsOneOf, | |
456 | tagsAllOf: req.query.tagsAllOf, | |
457 | nsfw: buildNSFWFilter(res, req.query.nsfw), | |
48dce1c9 | 458 | filter: req.query.filter as VideoFilter, |
6e46de09 | 459 | withFiles: false, |
7ad9b984 | 460 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined |
48dce1c9 | 461 | }) |
eb080476 C |
462 | |
463 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 464 | } |
c45f7f84 | 465 | |
eb080476 | 466 | async function removeVideo (req: express.Request, res: express.Response) { |
3fd3ab2d | 467 | const videoInstance: VideoModel = res.locals.video |
91f6f169 | 468 | |
3fd3ab2d | 469 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 470 | await videoInstance.destroy({ transaction: t }) |
91f6f169 | 471 | }) |
eb080476 | 472 | |
993cef4b | 473 | auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) |
eb080476 | 474 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) |
90d4bb81 C |
475 | |
476 | return res.type('json').status(204).end() | |
9f10b292 | 477 | } |