]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
8054669f | 2 | import { move } from 'fs-extra' |
3f6b7aa1 | 3 | import { extname } from 'path' |
8054669f C |
4 | import toInt from 'validator/lib/toInt' |
5 | import { addOptimizeOrMergeAudioJob } from '@server/helpers/video' | |
6 | import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent' | |
7 | import { changeVideoChannelShare } from '@server/lib/activitypub/share' | |
de94ac86 | 8 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' |
7a4ea932 | 9 | import { LiveManager } from '@server/lib/live-manager' |
1ef65f4c | 10 | import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' |
8054669f C |
11 | import { getVideoFilePath } from '@server/lib/video-paths' |
12 | import { getServerActor } from '@server/models/application/application' | |
053aed43 | 13 | import { MVideoFullLight } from '@server/types/models' |
1ef65f4c | 14 | import { VideoCreate, VideoState, VideoUpdate } from '../../../../shared' |
8054669f C |
15 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' |
16 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | |
17 | import { resetSequelizeInstance } from '../../../helpers/database-utils' | |
18 | import { buildNSFWFilter, createReqFiles, getCountVideos } from '../../../helpers/express-utils' | |
daf6e480 | 19 | import { getMetadataFromFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffprobe-utils' |
da854ddd | 20 | import { logger } from '../../../helpers/logger' |
8dc8a34e | 21 | import { getFormattedObjects } from '../../../helpers/utils' |
8054669f | 22 | import { CONFIG } from '../../../initializers/config' |
b345a804 C |
23 | import { |
24 | DEFAULT_AUDIO_RESOLUTION, | |
25 | MIMETYPES, | |
26 | VIDEO_CATEGORIES, | |
27 | VIDEO_LANGUAGES, | |
28 | VIDEO_LICENCES, | |
a1587156 | 29 | VIDEO_PRIVACIES |
b345a804 | 30 | } from '../../../initializers/constants' |
8054669f C |
31 | import { sequelizeTypescript } from '../../../initializers/database' |
32 | import { sendView } from '../../../lib/activitypub/send/send-view' | |
8dc8a34e | 33 | import { federateVideoIfNeeded, fetchRemoteVideoDescription } from '../../../lib/activitypub/videos' |
94a5ff8a | 34 | import { JobQueue } from '../../../lib/job-queue' |
8054669f C |
35 | import { Notifier } from '../../../lib/notifier' |
36 | import { Hooks } from '../../../lib/plugins/hooks' | |
b5c0e955 | 37 | import { Redis } from '../../../lib/redis' |
1ef65f4c | 38 | import { generateVideoMiniature } from '../../../lib/thumbnail' |
8054669f | 39 | import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist' |
65fcc311 | 40 | import { |
ac81d1a0 | 41 | asyncMiddleware, |
90d4bb81 | 42 | asyncRetryTransactionMiddleware, |
ac81d1a0 | 43 | authenticate, |
8d427346 | 44 | checkVideoFollowConstraints, |
d525fc39 | 45 | commonVideosFiltersValidator, |
0883b324 | 46 | optionalAuthenticate, |
ac81d1a0 C |
47 | paginationValidator, |
48 | setDefaultPagination, | |
8054669f | 49 | setDefaultVideosSort, |
d57d1d83 | 50 | videoFileMetadataGetValidator, |
ac81d1a0 | 51 | videosAddValidator, |
09209296 | 52 | videosCustomGetValidator, |
ac81d1a0 C |
53 | videosGetValidator, |
54 | videosRemoveValidator, | |
ac81d1a0 | 55 | videosSortValidator, |
d57d1d83 | 56 | videosUpdateValidator |
65fcc311 | 57 | } from '../../../middlewares' |
8054669f | 58 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' |
3fd3ab2d C |
59 | import { VideoModel } from '../../../models/video/video' |
60 | import { VideoFileModel } from '../../../models/video/video-file' | |
65fcc311 | 61 | import { blacklistRouter } from './blacklist' |
40e87e9e | 62 | import { videoCaptionsRouter } from './captions' |
8054669f | 63 | import { videoCommentRouter } from './comment' |
fbad87b0 | 64 | import { videoImportsRouter } from './import' |
c6c0fa6c | 65 | import { liveRouter } from './live' |
8054669f C |
66 | import { ownershipVideoRouter } from './ownership' |
67 | import { rateVideoRouter } from './rate' | |
6e46de09 | 68 | import { watchingRouter } from './watching' |
2d53be02 | 69 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
65fcc311 | 70 | |
80e36cd9 | 71 | const auditLogger = auditLoggerFactory('videos') |
65fcc311 | 72 | const videosRouter = express.Router() |
9f10b292 | 73 | |
ac81d1a0 C |
74 | const reqVideoFileAdd = createReqFiles( |
75 | [ 'videofile', 'thumbnailfile', 'previewfile' ], | |
14e2014a | 76 | Object.assign({}, MIMETYPES.VIDEO.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT), |
ac81d1a0 | 77 | { |
6040f87d C |
78 | videofile: CONFIG.STORAGE.TMP_DIR, |
79 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, | |
80 | previewfile: CONFIG.STORAGE.TMP_DIR | |
ac81d1a0 C |
81 | } |
82 | ) | |
83 | const reqVideoFileUpdate = createReqFiles( | |
84 | [ 'thumbnailfile', 'previewfile' ], | |
14e2014a | 85 | MIMETYPES.IMAGE.MIMETYPE_EXT, |
ac81d1a0 | 86 | { |
6040f87d C |
87 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, |
88 | previewfile: CONFIG.STORAGE.TMP_DIR | |
ac81d1a0 C |
89 | } |
90 | ) | |
8c308c2b | 91 | |
65fcc311 C |
92 | videosRouter.use('/', blacklistRouter) |
93 | videosRouter.use('/', rateVideoRouter) | |
bf1f6508 | 94 | videosRouter.use('/', videoCommentRouter) |
40e87e9e | 95 | videosRouter.use('/', videoCaptionsRouter) |
fbad87b0 | 96 | videosRouter.use('/', videoImportsRouter) |
74d63469 | 97 | videosRouter.use('/', ownershipVideoRouter) |
6e46de09 | 98 | videosRouter.use('/', watchingRouter) |
c6c0fa6c | 99 | videosRouter.use('/', liveRouter) |
d33242b0 | 100 | |
65fcc311 C |
101 | videosRouter.get('/categories', listVideoCategories) |
102 | videosRouter.get('/licences', listVideoLicences) | |
103 | videosRouter.get('/languages', listVideoLanguages) | |
fd45e8f4 | 104 | videosRouter.get('/privacies', listVideoPrivacies) |
6e07c3de | 105 | |
65fcc311 C |
106 | videosRouter.get('/', |
107 | paginationValidator, | |
108 | videosSortValidator, | |
8054669f | 109 | setDefaultVideosSort, |
f05a1c30 | 110 | setDefaultPagination, |
0883b324 | 111 | optionalAuthenticate, |
d525fc39 | 112 | commonVideosFiltersValidator, |
eb080476 | 113 | asyncMiddleware(listVideos) |
fbf1134e | 114 | ) |
65fcc311 C |
115 | videosRouter.put('/:id', |
116 | authenticate, | |
ac81d1a0 | 117 | reqVideoFileUpdate, |
a2431b7d | 118 | asyncMiddleware(videosUpdateValidator), |
90d4bb81 | 119 | asyncRetryTransactionMiddleware(updateVideo) |
7b1f49de | 120 | ) |
e95561cd | 121 | videosRouter.post('/upload', |
65fcc311 | 122 | authenticate, |
ac81d1a0 | 123 | reqVideoFileAdd, |
3fd3ab2d | 124 | asyncMiddleware(videosAddValidator), |
90d4bb81 | 125 | asyncRetryTransactionMiddleware(addVideo) |
fbf1134e | 126 | ) |
9567011b C |
127 | |
128 | videosRouter.get('/:id/description', | |
a2431b7d | 129 | asyncMiddleware(videosGetValidator), |
9567011b C |
130 | asyncMiddleware(getVideoDescription) |
131 | ) | |
8319d6ae RK |
132 | videosRouter.get('/:id/metadata/:videoFileId', |
133 | asyncMiddleware(videoFileMetadataGetValidator), | |
134 | asyncMiddleware(getVideoFileMetadata) | |
135 | ) | |
65fcc311 | 136 | videosRouter.get('/:id', |
6e46de09 | 137 | optionalAuthenticate, |
09209296 | 138 | asyncMiddleware(videosCustomGetValidator('only-video-with-rights')), |
8d427346 | 139 | asyncMiddleware(checkVideoFollowConstraints), |
09209296 | 140 | asyncMiddleware(getVideo) |
fbf1134e | 141 | ) |
1f3e9fec | 142 | videosRouter.post('/:id/views', |
2c8776fc | 143 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), |
1f3e9fec C |
144 | asyncMiddleware(viewVideo) |
145 | ) | |
198b205c | 146 | |
65fcc311 C |
147 | videosRouter.delete('/:id', |
148 | authenticate, | |
a2431b7d | 149 | asyncMiddleware(videosRemoveValidator), |
90d4bb81 | 150 | asyncRetryTransactionMiddleware(removeVideo) |
fbf1134e | 151 | ) |
198b205c | 152 | |
9f10b292 | 153 | // --------------------------------------------------------------------------- |
c45f7f84 | 154 | |
65fcc311 C |
155 | export { |
156 | videosRouter | |
157 | } | |
c45f7f84 | 158 | |
9f10b292 | 159 | // --------------------------------------------------------------------------- |
c45f7f84 | 160 | |
556ddc31 | 161 | function listVideoCategories (req: express.Request, res: express.Response) { |
65fcc311 | 162 | res.json(VIDEO_CATEGORIES) |
6e07c3de C |
163 | } |
164 | ||
556ddc31 | 165 | function listVideoLicences (req: express.Request, res: express.Response) { |
65fcc311 | 166 | res.json(VIDEO_LICENCES) |
6f0c39e2 C |
167 | } |
168 | ||
556ddc31 | 169 | function listVideoLanguages (req: express.Request, res: express.Response) { |
65fcc311 | 170 | res.json(VIDEO_LANGUAGES) |
3092476e C |
171 | } |
172 | ||
fd45e8f4 C |
173 | function listVideoPrivacies (req: express.Request, res: express.Response) { |
174 | res.json(VIDEO_PRIVACIES) | |
175 | } | |
176 | ||
90d4bb81 | 177 | async function addVideo (req: express.Request, res: express.Response) { |
bb4ba6d9 | 178 | // Uploading the video could be long |
d4132d3f | 179 | // Set timeout to 10 minutes, as Express's default is 2 minutes |
8b917537 C |
180 | req.setTimeout(1000 * 60 * 10, () => { |
181 | logger.error('Upload video has timed out.') | |
2d53be02 | 182 | return res.sendStatus(HttpStatusCode.REQUEST_TIMEOUT_408) |
8b917537 C |
183 | }) |
184 | ||
90d4bb81 | 185 | const videoPhysicalFile = req.files['videofile'][0] |
556ddc31 | 186 | const videoInfo: VideoCreate = req.body |
9f10b292 | 187 | |
1ef65f4c C |
188 | const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id) |
189 | videoData.state = CONFIG.TRANSCODING.ENABLED ? VideoState.TO_TRANSCODE : VideoState.PUBLISHED | |
190 | videoData.duration = videoPhysicalFile['duration'] // duration was added by a previous middleware | |
7ccddd7b | 191 | |
af4ae64f | 192 | const video = new VideoModel(videoData) as MVideoFullLight |
de94ac86 | 193 | video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object |
eb080476 | 194 | |
46a6db24 | 195 | const videoFile = new VideoFileModel({ |
e11f68a3 | 196 | extname: extname(videoPhysicalFile.filename), |
d7a25329 | 197 | size: videoPhysicalFile.size, |
8319d6ae | 198 | videoStreamingPlaylistId: null, |
daf6e480 | 199 | metadata: await getMetadataFromFile(videoPhysicalFile.path) |
46a6db24 | 200 | }) |
2186386c | 201 | |
ad3405d0 C |
202 | if (videoFile.isAudio()) { |
203 | videoFile.resolution = DEFAULT_AUDIO_RESOLUTION | |
204 | } else { | |
536598cf C |
205 | videoFile.fps = await getVideoFileFPS(videoPhysicalFile.path) |
206 | videoFile.resolution = (await getVideoFileResolution(videoPhysicalFile.path)).videoFileResolution | |
536598cf C |
207 | } |
208 | ||
2186386c | 209 | // Move physical file |
d7a25329 | 210 | const destination = getVideoFilePath(video, videoFile) |
14e2014a | 211 | await move(videoPhysicalFile.path, destination) |
e3a682a8 | 212 | // This is important in case if there is another attempt in the retry process |
d7a25329 | 213 | videoPhysicalFile.filename = getVideoFilePath(video, videoFile) |
82815eb6 | 214 | videoPhysicalFile.path = destination |
ac81d1a0 | 215 | |
1ef65f4c C |
216 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ |
217 | video, | |
218 | files: req.files, | |
219 | fallback: type => generateVideoMiniature(video, videoFile, type) | |
220 | }) | |
eb080476 | 221 | |
2186386c | 222 | // Create the torrent file |
d7a25329 | 223 | await createTorrentAndSetInfoHash(video, videoFile) |
eb080476 | 224 | |
5b77537c | 225 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { |
e11f68a3 | 226 | const sequelizeOptions = { transaction: t } |
eb080476 | 227 | |
453e83ea | 228 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight |
e8bafea3 | 229 | |
3acc5084 C |
230 | await videoCreated.addAndSaveThumbnail(thumbnailModel, t) |
231 | await videoCreated.addAndSaveThumbnail(previewModel, t) | |
e8bafea3 | 232 | |
eb080476 C |
233 | // Do not forget to add video channel information to the created video |
234 | videoCreated.VideoChannel = res.locals.videoChannel | |
7920c273 | 235 | |
eb080476 | 236 | videoFile.videoId = video.id |
eb080476 | 237 | await videoFile.save(sequelizeOptions) |
e11f68a3 C |
238 | |
239 | video.VideoFiles = [ videoFile ] | |
93e1258c | 240 | |
1ef65f4c | 241 | await setVideoTags({ video, tags: videoInfo.tags, transaction: t }) |
eb080476 | 242 | |
2baea0c7 C |
243 | // Schedule an update in the future? |
244 | if (videoInfo.scheduleUpdate) { | |
245 | await ScheduleVideoUpdateModel.create({ | |
246 | videoId: video.id, | |
247 | updateAt: videoInfo.scheduleUpdate.updateAt, | |
248 | privacy: videoInfo.scheduleUpdate.privacy || null | |
249 | }, { transaction: t }) | |
250 | } | |
251 | ||
5b77537c | 252 | await autoBlacklistVideoIfNeeded({ |
6691c522 C |
253 | video, |
254 | user: res.locals.oauth.token.User, | |
255 | isRemote: false, | |
256 | isNew: true, | |
257 | transaction: t | |
258 | }) | |
5b77537c | 259 | await federateVideoIfNeeded(video, true, t) |
eb080476 | 260 | |
993cef4b | 261 | auditLogger.create(getAuditIdFromRes(res), new VideoAuditView(videoCreated.toFormattedDetailsJSON())) |
cadb46d8 C |
262 | logger.info('Video with name %s and uuid %s created.', videoInfo.name, videoCreated.uuid) |
263 | ||
5b77537c | 264 | return { videoCreated } |
cadb46d8 | 265 | }) |
94a5ff8a | 266 | |
5b77537c | 267 | Notifier.Instance.notifyOnNewVideoIfNeeded(videoCreated) |
e8d246d5 | 268 | |
2186386c | 269 | if (video.state === VideoState.TO_TRANSCODE) { |
d57d1d83 | 270 | await addOptimizeOrMergeAudioJob(videoCreated, videoFile) |
94a5ff8a C |
271 | } |
272 | ||
b4055e1c C |
273 | Hooks.runAction('action:api.video.uploaded', { video: videoCreated }) |
274 | ||
90d4bb81 C |
275 | return res.json({ |
276 | video: { | |
277 | id: videoCreated.id, | |
278 | uuid: videoCreated.uuid | |
279 | } | |
c6c0fa6c | 280 | }) |
ed04d94f C |
281 | } |
282 | ||
eb080476 | 283 | async function updateVideo (req: express.Request, res: express.Response) { |
453e83ea | 284 | const videoInstance = res.locals.videoAll |
7f4e7c36 | 285 | const videoFieldsSave = videoInstance.toJSON() |
80e36cd9 | 286 | const oldVideoAuditView = new VideoAuditView(videoInstance.toFormattedDetailsJSON()) |
556ddc31 | 287 | const videoInfoToUpdate: VideoUpdate = req.body |
46a6db24 | 288 | |
22a73cb8 C |
289 | const wasConfidentialVideo = videoInstance.isConfidential() |
290 | const hadPrivacyForFederation = videoInstance.hasPrivacyForFederation() | |
7b1f49de | 291 | |
1ef65f4c C |
292 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ |
293 | video: videoInstance, | |
294 | files: req.files, | |
295 | fallback: () => Promise.resolve(undefined), | |
296 | automaticallyGenerated: false | |
297 | }) | |
ac81d1a0 | 298 | |
eb080476 | 299 | try { |
e8d246d5 C |
300 | const videoInstanceUpdated = await sequelizeTypescript.transaction(async t => { |
301 | const sequelizeOptions = { transaction: t } | |
0f320037 | 302 | const oldVideoChannel = videoInstance.VideoChannel |
7b1f49de | 303 | |
6691c522 C |
304 | if (videoInfoToUpdate.name !== undefined) videoInstance.name = videoInfoToUpdate.name |
305 | if (videoInfoToUpdate.category !== undefined) videoInstance.category = videoInfoToUpdate.category | |
306 | if (videoInfoToUpdate.licence !== undefined) videoInstance.licence = videoInfoToUpdate.licence | |
307 | if (videoInfoToUpdate.language !== undefined) videoInstance.language = videoInfoToUpdate.language | |
308 | if (videoInfoToUpdate.nsfw !== undefined) videoInstance.nsfw = videoInfoToUpdate.nsfw | |
309 | if (videoInfoToUpdate.waitTranscoding !== undefined) videoInstance.waitTranscoding = videoInfoToUpdate.waitTranscoding | |
310 | if (videoInfoToUpdate.support !== undefined) videoInstance.support = videoInfoToUpdate.support | |
311 | if (videoInfoToUpdate.description !== undefined) videoInstance.description = videoInfoToUpdate.description | |
312 | if (videoInfoToUpdate.commentsEnabled !== undefined) videoInstance.commentsEnabled = videoInfoToUpdate.commentsEnabled | |
313 | if (videoInfoToUpdate.downloadEnabled !== undefined) videoInstance.downloadEnabled = videoInfoToUpdate.downloadEnabled | |
b718fd22 C |
314 | |
315 | if (videoInfoToUpdate.originallyPublishedAt !== undefined && videoInfoToUpdate.originallyPublishedAt !== null) { | |
1735c825 | 316 | videoInstance.originallyPublishedAt = new Date(videoInfoToUpdate.originallyPublishedAt) |
1e74f19a | 317 | } |
318 | ||
22a73cb8 | 319 | let isNewVideo = false |
2922e048 | 320 | if (videoInfoToUpdate.privacy !== undefined) { |
22a73cb8 | 321 | isNewVideo = videoInstance.isNewVideo(videoInfoToUpdate.privacy) |
2922e048 | 322 | |
22a73cb8 C |
323 | const newPrivacy = parseInt(videoInfoToUpdate.privacy.toString(), 10) |
324 | videoInstance.setPrivacy(newPrivacy) | |
46a6db24 | 325 | |
22a73cb8 C |
326 | // Unfederate the video if the new privacy is not compatible with federation |
327 | if (hadPrivacyForFederation && !videoInstance.hasPrivacyForFederation()) { | |
46a6db24 C |
328 | await VideoModel.sendDelete(videoInstance, { transaction: t }) |
329 | } | |
2922e048 | 330 | } |
7b1f49de | 331 | |
453e83ea | 332 | const videoInstanceUpdated = await videoInstance.save(sequelizeOptions) as MVideoFullLight |
7b1f49de | 333 | |
3acc5084 C |
334 | if (thumbnailModel) await videoInstanceUpdated.addAndSaveThumbnail(thumbnailModel, t) |
335 | if (previewModel) await videoInstanceUpdated.addAndSaveThumbnail(previewModel, t) | |
e8bafea3 | 336 | |
0f320037 | 337 | // Video tags update? |
1ef65f4c C |
338 | await setVideoTags({ |
339 | video: videoInstanceUpdated, | |
340 | tags: videoInfoToUpdate.tags, | |
341 | transaction: t, | |
342 | defaultValue: videoInstanceUpdated.Tags | |
343 | }) | |
7920c273 | 344 | |
0f320037 C |
345 | // Video channel update? |
346 | if (res.locals.videoChannel && videoInstanceUpdated.channelId !== res.locals.videoChannel.id) { | |
6200d8d9 | 347 | await videoInstanceUpdated.$set('VideoChannel', res.locals.videoChannel, { transaction: t }) |
2186386c | 348 | videoInstanceUpdated.VideoChannel = res.locals.videoChannel |
0f320037 | 349 | |
22a73cb8 | 350 | if (hadPrivacyForFederation === true) await changeVideoChannelShare(videoInstanceUpdated, oldVideoChannel, t) |
fd45e8f4 C |
351 | } |
352 | ||
2baea0c7 C |
353 | // Schedule an update in the future? |
354 | if (videoInfoToUpdate.scheduleUpdate) { | |
355 | await ScheduleVideoUpdateModel.upsert({ | |
356 | videoId: videoInstanceUpdated.id, | |
357 | updateAt: videoInfoToUpdate.scheduleUpdate.updateAt, | |
358 | privacy: videoInfoToUpdate.scheduleUpdate.privacy || null | |
359 | }, { transaction: t }) | |
e94fc297 C |
360 | } else if (videoInfoToUpdate.scheduleUpdate === null) { |
361 | await ScheduleVideoUpdateModel.deleteByVideoId(videoInstanceUpdated.id, t) | |
2baea0c7 C |
362 | } |
363 | ||
6691c522 C |
364 | await autoBlacklistVideoIfNeeded({ |
365 | video: videoInstanceUpdated, | |
366 | user: res.locals.oauth.token.User, | |
367 | isRemote: false, | |
368 | isNew: false, | |
369 | transaction: t | |
370 | }) | |
371 | ||
5b77537c | 372 | await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t) |
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 | 383 | |
22a73cb8 | 384 | if (wasConfidentialVideo) { |
5b77537c | 385 | Notifier.Instance.notifyOnNewVideoIfNeeded(videoInstanceUpdated) |
e8d246d5 | 386 | } |
b4055e1c | 387 | |
7294aab0 | 388 | Hooks.runAction('action:api.video.updated', { video: videoInstanceUpdated, body: req.body }) |
eb080476 | 389 | } catch (err) { |
6fcd19ba C |
390 | // Force fields we want to update |
391 | // If the transaction is retried, sequelize will think the object has not changed | |
392 | // So it will skip the SQL request, even if the last one was ROLLBACKed! | |
eb080476 | 393 | resetSequelizeInstance(videoInstance, videoFieldsSave) |
6fcd19ba C |
394 | |
395 | throw err | |
eb080476 | 396 | } |
90d4bb81 | 397 | |
2d53be02 RK |
398 | return res.type('json') |
399 | .status(HttpStatusCode.NO_CONTENT_204) | |
400 | .end() | |
9f10b292 | 401 | } |
8c308c2b | 402 | |
09209296 C |
403 | async function getVideo (req: express.Request, res: express.Response) { |
404 | // We need more attributes | |
405 | const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null | |
b4055e1c | 406 | |
89cd1275 C |
407 | const video = await Hooks.wrapPromiseFun( |
408 | VideoModel.loadForGetAPI, | |
453e83ea | 409 | { id: res.locals.onlyVideoWithRights.id, userId }, |
b4055e1c C |
410 | 'filter:api.video.get.result' |
411 | ) | |
1f3e9fec | 412 | |
09209296 C |
413 | if (video.isOutdated()) { |
414 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } }) | |
04b8c3fb C |
415 | } |
416 | ||
09209296 | 417 | return res.json(video.toFormattedDetailsJSON()) |
1f3e9fec C |
418 | } |
419 | ||
420 | async function viewVideo (req: express.Request, res: express.Response) { | |
e4bf7856 | 421 | const immutableVideoAttrs = res.locals.onlyImmutableVideo |
9e167724 | 422 | |
490b595a | 423 | const ip = req.ip |
e4bf7856 | 424 | const exists = await Redis.Instance.doesVideoIPViewExist(ip, immutableVideoAttrs.uuid) |
b5c0e955 | 425 | if (exists) { |
e4bf7856 | 426 | logger.debug('View for ip %s and video %s already exists.', ip, immutableVideoAttrs.uuid) |
2d53be02 | 427 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |
b5c0e955 C |
428 | } |
429 | ||
e4bf7856 | 430 | const video = await VideoModel.load(immutableVideoAttrs.id) |
b5c0e955 | 431 | |
e4bf7856 C |
432 | const promises: Promise<any>[] = [ |
433 | Redis.Instance.setIPVideoView(ip, video.uuid, video.isLive) | |
434 | ] | |
9e167724 | 435 | |
e4bf7856 | 436 | let federateView = true |
b4055e1c | 437 | |
e4bf7856 C |
438 | // Increment our live manager |
439 | if (video.isLive && video.isOwned()) { | |
440 | LiveManager.Instance.addViewTo(video.id) | |
441 | ||
442 | // Views of our local live will be sent by our live manager | |
443 | federateView = false | |
444 | } | |
445 | ||
446 | // Increment our video views cache counter | |
447 | if (!video.isLive) { | |
448 | promises.push(Redis.Instance.addVideoView(video.id)) | |
449 | } | |
450 | ||
451 | if (federateView) { | |
452 | const serverActor = await getServerActor() | |
453 | promises.push(sendView(serverActor, video, undefined)) | |
454 | } | |
455 | ||
456 | await Promise.all(promises) | |
457 | ||
458 | Hooks.runAction('action:api.video.viewed', { video, ip }) | |
459 | ||
2d53be02 | 460 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) |
9f10b292 | 461 | } |
8c308c2b | 462 | |
9567011b | 463 | async function getVideoDescription (req: express.Request, res: express.Response) { |
453e83ea | 464 | const videoInstance = res.locals.videoAll |
9567011b C |
465 | let description = '' |
466 | ||
467 | if (videoInstance.isOwned()) { | |
468 | description = videoInstance.description | |
469 | } else { | |
571389d4 | 470 | description = await fetchRemoteVideoDescription(videoInstance) |
9567011b C |
471 | } |
472 | ||
473 | return res.json({ description }) | |
474 | } | |
475 | ||
8319d6ae RK |
476 | async function getVideoFileMetadata (req: express.Request, res: express.Response) { |
477 | const videoFile = await VideoFileModel.loadWithMetadata(toInt(req.params.videoFileId)) | |
583eb04b | 478 | |
8319d6ae RK |
479 | return res.json(videoFile.metadata) |
480 | } | |
481 | ||
04b8c3fb | 482 | async function listVideos (req: express.Request, res: express.Response) { |
fe987656 C |
483 | const countVideos = getCountVideos(req) |
484 | ||
b4055e1c | 485 | const apiOptions = await Hooks.wrapObject({ |
48dce1c9 C |
486 | start: req.query.start, |
487 | count: req.query.count, | |
488 | sort: req.query.sort, | |
06a05d5f | 489 | includeLocalVideos: true, |
d525fc39 C |
490 | categoryOneOf: req.query.categoryOneOf, |
491 | licenceOneOf: req.query.licenceOneOf, | |
492 | languageOneOf: req.query.languageOneOf, | |
493 | tagsOneOf: req.query.tagsOneOf, | |
494 | tagsAllOf: req.query.tagsAllOf, | |
495 | nsfw: buildNSFWFilter(res, req.query.nsfw), | |
48dce1c9 | 496 | filter: req.query.filter as VideoFilter, |
6e46de09 | 497 | withFiles: false, |
fe987656 C |
498 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, |
499 | countVideos | |
b4055e1c C |
500 | }, 'filter:api.videos.list.params') |
501 | ||
89cd1275 C |
502 | const resultList = await Hooks.wrapPromiseFun( |
503 | VideoModel.listForApi, | |
504 | apiOptions, | |
b4055e1c C |
505 | 'filter:api.videos.list.result' |
506 | ) | |
eb080476 C |
507 | |
508 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 509 | } |
c45f7f84 | 510 | |
eb080476 | 511 | async function removeVideo (req: express.Request, res: express.Response) { |
453e83ea | 512 | const videoInstance = res.locals.videoAll |
91f6f169 | 513 | |
3fd3ab2d | 514 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 515 | await videoInstance.destroy({ transaction: t }) |
91f6f169 | 516 | }) |
eb080476 | 517 | |
993cef4b | 518 | auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) |
eb080476 | 519 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) |
90d4bb81 | 520 | |
b4055e1c C |
521 | Hooks.runAction('action:api.video.deleted', { video: videoInstance }) |
522 | ||
2d53be02 RK |
523 | return res.type('json') |
524 | .status(HttpStatusCode.NO_CONTENT_204) | |
525 | .end() | |
9f10b292 | 526 | } |