]>
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' | |
8 | import { getVideoActivityPubUrl } from '@server/lib/activitypub/url' | |
1ef65f4c | 9 | import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' |
8054669f C |
10 | import { getVideoFilePath } from '@server/lib/video-paths' |
11 | import { getServerActor } from '@server/models/application/application' | |
053aed43 | 12 | import { MVideoFullLight } from '@server/types/models' |
1ef65f4c | 13 | import { VideoCreate, VideoState, VideoUpdate } from '../../../../shared' |
8054669f C |
14 | import { VideoFilter } from '../../../../shared/models/videos/video-query.type' |
15 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | |
16 | import { resetSequelizeInstance } from '../../../helpers/database-utils' | |
17 | import { buildNSFWFilter, createReqFiles, getCountVideos } from '../../../helpers/express-utils' | |
d57d1d83 | 18 | import { getMetadataFromFile, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils' |
da854ddd | 19 | import { logger } from '../../../helpers/logger' |
8dc8a34e | 20 | import { getFormattedObjects } from '../../../helpers/utils' |
8054669f | 21 | import { CONFIG } from '../../../initializers/config' |
b345a804 C |
22 | import { |
23 | DEFAULT_AUDIO_RESOLUTION, | |
24 | MIMETYPES, | |
25 | VIDEO_CATEGORIES, | |
26 | VIDEO_LANGUAGES, | |
27 | VIDEO_LICENCES, | |
a1587156 | 28 | VIDEO_PRIVACIES |
b345a804 | 29 | } from '../../../initializers/constants' |
8054669f C |
30 | import { sequelizeTypescript } from '../../../initializers/database' |
31 | import { sendView } from '../../../lib/activitypub/send/send-view' | |
8dc8a34e | 32 | import { federateVideoIfNeeded, fetchRemoteVideoDescription } from '../../../lib/activitypub/videos' |
94a5ff8a | 33 | import { JobQueue } from '../../../lib/job-queue' |
8054669f C |
34 | import { Notifier } from '../../../lib/notifier' |
35 | import { Hooks } from '../../../lib/plugins/hooks' | |
b5c0e955 | 36 | import { Redis } from '../../../lib/redis' |
1ef65f4c | 37 | import { generateVideoMiniature } from '../../../lib/thumbnail' |
8054669f | 38 | import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist' |
65fcc311 | 39 | import { |
ac81d1a0 | 40 | asyncMiddleware, |
90d4bb81 | 41 | asyncRetryTransactionMiddleware, |
ac81d1a0 | 42 | authenticate, |
8d427346 | 43 | checkVideoFollowConstraints, |
d525fc39 | 44 | commonVideosFiltersValidator, |
0883b324 | 45 | optionalAuthenticate, |
ac81d1a0 C |
46 | paginationValidator, |
47 | setDefaultPagination, | |
8054669f | 48 | setDefaultVideosSort, |
d57d1d83 | 49 | videoFileMetadataGetValidator, |
ac81d1a0 | 50 | videosAddValidator, |
09209296 | 51 | videosCustomGetValidator, |
ac81d1a0 C |
52 | videosGetValidator, |
53 | videosRemoveValidator, | |
ac81d1a0 | 54 | videosSortValidator, |
d57d1d83 | 55 | videosUpdateValidator |
65fcc311 | 56 | } from '../../../middlewares' |
8054669f | 57 | import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' |
3fd3ab2d C |
58 | import { VideoModel } from '../../../models/video/video' |
59 | import { VideoFileModel } from '../../../models/video/video-file' | |
65fcc311 C |
60 | import { abuseVideoRouter } from './abuse' |
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' |
65fcc311 | 69 | |
80e36cd9 | 70 | const auditLogger = auditLoggerFactory('videos') |
65fcc311 | 71 | const videosRouter = express.Router() |
9f10b292 | 72 | |
ac81d1a0 C |
73 | const reqVideoFileAdd = createReqFiles( |
74 | [ 'videofile', 'thumbnailfile', 'previewfile' ], | |
14e2014a | 75 | Object.assign({}, MIMETYPES.VIDEO.MIMETYPE_EXT, MIMETYPES.IMAGE.MIMETYPE_EXT), |
ac81d1a0 | 76 | { |
6040f87d C |
77 | videofile: CONFIG.STORAGE.TMP_DIR, |
78 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, | |
79 | previewfile: CONFIG.STORAGE.TMP_DIR | |
ac81d1a0 C |
80 | } |
81 | ) | |
82 | const reqVideoFileUpdate = createReqFiles( | |
83 | [ 'thumbnailfile', 'previewfile' ], | |
14e2014a | 84 | MIMETYPES.IMAGE.MIMETYPE_EXT, |
ac81d1a0 | 85 | { |
6040f87d C |
86 | thumbnailfile: CONFIG.STORAGE.TMP_DIR, |
87 | previewfile: CONFIG.STORAGE.TMP_DIR | |
ac81d1a0 C |
88 | } |
89 | ) | |
8c308c2b | 90 | |
65fcc311 C |
91 | videosRouter.use('/', abuseVideoRouter) |
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) { |
8b917537 C |
178 | // Processing the video could be long |
179 | // Set timeout to 10 minutes | |
180 | req.setTimeout(1000 * 60 * 10, () => { | |
181 | logger.error('Upload video has timed out.') | |
182 | return res.sendStatus(408) | |
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 |
2186386c | 193 | video.url = getVideoActivityPubUrl(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 RK |
198 | videoStreamingPlaylistId: null, |
199 | metadata: await getMetadataFromFile<any>(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 C |
397 | |
398 | return res.type('json').status(204).end() | |
9f10b292 | 399 | } |
8c308c2b | 400 | |
09209296 C |
401 | async function getVideo (req: express.Request, res: express.Response) { |
402 | // We need more attributes | |
403 | const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null | |
b4055e1c | 404 | |
89cd1275 C |
405 | const video = await Hooks.wrapPromiseFun( |
406 | VideoModel.loadForGetAPI, | |
453e83ea | 407 | { id: res.locals.onlyVideoWithRights.id, userId }, |
b4055e1c C |
408 | 'filter:api.video.get.result' |
409 | ) | |
1f3e9fec | 410 | |
09209296 C |
411 | if (video.isOutdated()) { |
412 | JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } }) | |
04b8c3fb C |
413 | } |
414 | ||
09209296 | 415 | return res.json(video.toFormattedDetailsJSON()) |
1f3e9fec C |
416 | } |
417 | ||
418 | async function viewVideo (req: express.Request, res: express.Response) { | |
2c8776fc | 419 | const videoInstance = res.locals.onlyImmutableVideo |
9e167724 | 420 | |
490b595a | 421 | const ip = req.ip |
0f6acda1 | 422 | const exists = await Redis.Instance.doesVideoIPViewExist(ip, videoInstance.uuid) |
b5c0e955 C |
423 | if (exists) { |
424 | logger.debug('View for ip %s and video %s already exists.', ip, videoInstance.uuid) | |
425 | return res.status(204).end() | |
426 | } | |
427 | ||
6b616860 C |
428 | await Promise.all([ |
429 | Redis.Instance.addVideoView(videoInstance.id), | |
430 | Redis.Instance.setIPVideoView(ip, videoInstance.uuid) | |
431 | ]) | |
b5c0e955 | 432 | |
a2377d15 | 433 | const serverActor = await getServerActor() |
1e7eb25f | 434 | await sendView(serverActor, videoInstance, undefined) |
9e167724 | 435 | |
b4055e1c C |
436 | Hooks.runAction('action:api.video.viewed', { video: videoInstance, ip }) |
437 | ||
1f3e9fec | 438 | return res.status(204).end() |
9f10b292 | 439 | } |
8c308c2b | 440 | |
9567011b | 441 | async function getVideoDescription (req: express.Request, res: express.Response) { |
453e83ea | 442 | const videoInstance = res.locals.videoAll |
9567011b C |
443 | let description = '' |
444 | ||
445 | if (videoInstance.isOwned()) { | |
446 | description = videoInstance.description | |
447 | } else { | |
571389d4 | 448 | description = await fetchRemoteVideoDescription(videoInstance) |
9567011b C |
449 | } |
450 | ||
451 | return res.json({ description }) | |
452 | } | |
453 | ||
8319d6ae RK |
454 | async function getVideoFileMetadata (req: express.Request, res: express.Response) { |
455 | const videoFile = await VideoFileModel.loadWithMetadata(toInt(req.params.videoFileId)) | |
583eb04b | 456 | |
8319d6ae RK |
457 | return res.json(videoFile.metadata) |
458 | } | |
459 | ||
04b8c3fb | 460 | async function listVideos (req: express.Request, res: express.Response) { |
fe987656 C |
461 | const countVideos = getCountVideos(req) |
462 | ||
b4055e1c | 463 | const apiOptions = await Hooks.wrapObject({ |
48dce1c9 C |
464 | start: req.query.start, |
465 | count: req.query.count, | |
466 | sort: req.query.sort, | |
06a05d5f | 467 | includeLocalVideos: true, |
d525fc39 C |
468 | categoryOneOf: req.query.categoryOneOf, |
469 | licenceOneOf: req.query.licenceOneOf, | |
470 | languageOneOf: req.query.languageOneOf, | |
471 | tagsOneOf: req.query.tagsOneOf, | |
472 | tagsAllOf: req.query.tagsAllOf, | |
473 | nsfw: buildNSFWFilter(res, req.query.nsfw), | |
48dce1c9 | 474 | filter: req.query.filter as VideoFilter, |
6e46de09 | 475 | withFiles: false, |
fe987656 C |
476 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, |
477 | countVideos | |
b4055e1c C |
478 | }, 'filter:api.videos.list.params') |
479 | ||
89cd1275 C |
480 | const resultList = await Hooks.wrapPromiseFun( |
481 | VideoModel.listForApi, | |
482 | apiOptions, | |
b4055e1c C |
483 | 'filter:api.videos.list.result' |
484 | ) | |
eb080476 C |
485 | |
486 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
9f10b292 | 487 | } |
c45f7f84 | 488 | |
eb080476 | 489 | async function removeVideo (req: express.Request, res: express.Response) { |
453e83ea | 490 | const videoInstance = res.locals.videoAll |
91f6f169 | 491 | |
3fd3ab2d | 492 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 493 | await videoInstance.destroy({ transaction: t }) |
91f6f169 | 494 | }) |
eb080476 | 495 | |
993cef4b | 496 | auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) |
eb080476 | 497 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) |
90d4bb81 | 498 | |
b4055e1c C |
499 | Hooks.runAction('action:api.video.deleted', { video: videoInstance }) |
500 | ||
90d4bb81 | 501 | return res.type('json').status(204).end() |
9f10b292 | 502 | } |