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