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