]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/index.ts
Add ability to remove hls/webtorrent files
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / index.ts
CommitLineData
41fb13c3 1import express from 'express'
d6886027 2import { pickCommonVideoQuery } from '@server/helpers/query'
304a84d5 3import { doJSONRequest } from '@server/helpers/requests'
51353d9a 4import { VideoViews } from '@server/lib/video-views'
1c627fd8 5import { openapiOperationDoc } from '@server/middlewares/doc'
8054669f 6import { getServerActor } from '@server/models/application/application'
2760b454 7import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils'
304a84d5 8import { MVideoAccountLight } from '@server/types/models'
c0e8b12e 9import { HttpStatusCode } from '../../../../shared/models'
8054669f 10import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
c158a5fa
C
11import { buildNSFWFilter, getCountVideos } from '../../../helpers/express-utils'
12import { logger } from '../../../helpers/logger'
8dc8a34e 13import { getFormattedObjects } from '../../../helpers/utils'
304a84d5 14import { REMOTE_SCHEME, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers/constants'
8054669f
C
15import { sequelizeTypescript } from '../../../initializers/database'
16import { sendView } from '../../../lib/activitypub/send/send-view'
94a5ff8a 17import { JobQueue } from '../../../lib/job-queue'
8054669f 18import { Hooks } from '../../../lib/plugins/hooks'
65fcc311 19import {
ac81d1a0 20 asyncMiddleware,
90d4bb81 21 asyncRetryTransactionMiddleware,
ac81d1a0 22 authenticate,
8d427346 23 checkVideoFollowConstraints,
d525fc39 24 commonVideosFiltersValidator,
0883b324 25 optionalAuthenticate,
ac81d1a0
C
26 paginationValidator,
27 setDefaultPagination,
8054669f 28 setDefaultVideosSort,
09209296 29 videosCustomGetValidator,
ac81d1a0
C
30 videosGetValidator,
31 videosRemoveValidator,
c158a5fa 32 videosSortValidator
65fcc311 33} from '../../../middlewares'
3fd3ab2d 34import { VideoModel } from '../../../models/video/video'
65fcc311 35import { blacklistRouter } from './blacklist'
40e87e9e 36import { videoCaptionsRouter } from './captions'
8054669f 37import { videoCommentRouter } from './comment'
b46cf4b9 38import { filesRouter } from './files'
fbad87b0 39import { videoImportsRouter } from './import'
c6c0fa6c 40import { liveRouter } from './live'
8054669f
C
41import { ownershipVideoRouter } from './ownership'
42import { rateVideoRouter } from './rate'
c158a5fa
C
43import { updateRouter } from './update'
44import { uploadRouter } from './upload'
6e46de09 45import { watchingRouter } from './watching'
65fcc311 46
80e36cd9 47const auditLogger = auditLoggerFactory('videos')
65fcc311 48const videosRouter = express.Router()
8c308c2b 49
65fcc311
C
50videosRouter.use('/', blacklistRouter)
51videosRouter.use('/', rateVideoRouter)
bf1f6508 52videosRouter.use('/', videoCommentRouter)
40e87e9e 53videosRouter.use('/', videoCaptionsRouter)
fbad87b0 54videosRouter.use('/', videoImportsRouter)
74d63469 55videosRouter.use('/', ownershipVideoRouter)
6e46de09 56videosRouter.use('/', watchingRouter)
c6c0fa6c 57videosRouter.use('/', liveRouter)
c158a5fa
C
58videosRouter.use('/', uploadRouter)
59videosRouter.use('/', updateRouter)
b46cf4b9 60videosRouter.use('/', filesRouter)
d33242b0 61
c756bae0
RK
62videosRouter.get('/categories',
63 openapiOperationDoc({ operationId: 'getCategories' }),
64 listVideoCategories
65)
66videosRouter.get('/licences',
67 openapiOperationDoc({ operationId: 'getLicences' }),
68 listVideoLicences
69)
70videosRouter.get('/languages',
71 openapiOperationDoc({ operationId: 'getLanguages' }),
72 listVideoLanguages
73)
74videosRouter.get('/privacies',
75 openapiOperationDoc({ operationId: 'getPrivacies' }),
76 listVideoPrivacies
77)
6e07c3de 78
65fcc311 79videosRouter.get('/',
c756bae0 80 openapiOperationDoc({ operationId: 'getVideos' }),
65fcc311
C
81 paginationValidator,
82 videosSortValidator,
8054669f 83 setDefaultVideosSort,
f05a1c30 84 setDefaultPagination,
0883b324 85 optionalAuthenticate,
d525fc39 86 commonVideosFiltersValidator,
eb080476 87 asyncMiddleware(listVideos)
fbf1134e 88)
f6d6e7f8 89
9567011b 90videosRouter.get('/:id/description',
c756bae0 91 openapiOperationDoc({ operationId: 'getVideoDesc' }),
a2431b7d 92 asyncMiddleware(videosGetValidator),
9567011b
C
93 asyncMiddleware(getVideoDescription)
94)
65fcc311 95videosRouter.get('/:id',
1c627fd8 96 openapiOperationDoc({ operationId: 'getVideo' }),
6e46de09 97 optionalAuthenticate,
ca4b4b2e 98 asyncMiddleware(videosCustomGetValidator('for-api')),
8d427346 99 asyncMiddleware(checkVideoFollowConstraints),
98ab5dc8 100 getVideo
fbf1134e 101)
1f3e9fec 102videosRouter.post('/:id/views',
c756bae0 103 openapiOperationDoc({ operationId: 'addView' }),
51353d9a 104 asyncMiddleware(videosCustomGetValidator('only-video')),
1f3e9fec
C
105 asyncMiddleware(viewVideo)
106)
198b205c 107
65fcc311 108videosRouter.delete('/:id',
1c627fd8 109 openapiOperationDoc({ operationId: 'delVideo' }),
65fcc311 110 authenticate,
a2431b7d 111 asyncMiddleware(videosRemoveValidator),
90d4bb81 112 asyncRetryTransactionMiddleware(removeVideo)
fbf1134e 113)
198b205c 114
9f10b292 115// ---------------------------------------------------------------------------
c45f7f84 116
65fcc311
C
117export {
118 videosRouter
119}
c45f7f84 120
9f10b292 121// ---------------------------------------------------------------------------
c45f7f84 122
f6d6e7f8 123function listVideoCategories (_req: express.Request, res: express.Response) {
65fcc311 124 res.json(VIDEO_CATEGORIES)
6e07c3de
C
125}
126
f6d6e7f8 127function listVideoLicences (_req: express.Request, res: express.Response) {
65fcc311 128 res.json(VIDEO_LICENCES)
6f0c39e2
C
129}
130
f6d6e7f8 131function listVideoLanguages (_req: express.Request, res: express.Response) {
65fcc311 132 res.json(VIDEO_LANGUAGES)
3092476e
C
133}
134
f6d6e7f8 135function listVideoPrivacies (_req: express.Request, res: express.Response) {
fd45e8f4
C
136 res.json(VIDEO_PRIVACIES)
137}
138
98ab5dc8 139function getVideo (_req: express.Request, res: express.Response) {
ca4b4b2e 140 const video = res.locals.videoAPI
1f3e9fec 141
09209296
C
142 if (video.isOutdated()) {
143 JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } })
04b8c3fb
C
144 }
145
09209296 146 return res.json(video.toFormattedDetailsJSON())
1f3e9fec
C
147}
148
149async function viewVideo (req: express.Request, res: express.Response) {
51353d9a 150 const video = res.locals.onlyVideo
9e167724 151
490b595a 152 const ip = req.ip
51353d9a 153 const success = await VideoViews.Instance.processView({ video, ip })
e4bf7856 154
51353d9a 155 if (success) {
e4bf7856 156 const serverActor = await getServerActor()
51353d9a 157 await sendView(serverActor, video, undefined)
e4bf7856 158
51353d9a
C
159 Hooks.runAction('action:api.video.viewed', { video: video, ip })
160 }
e4bf7856 161
76148b27 162 return res.status(HttpStatusCode.NO_CONTENT_204).end()
9f10b292 163}
8c308c2b 164
9567011b 165async function getVideoDescription (req: express.Request, res: express.Response) {
453e83ea 166 const videoInstance = res.locals.videoAll
9567011b 167
c158a5fa
C
168 const description = videoInstance.isOwned()
169 ? videoInstance.description
170 : await fetchRemoteVideoDescription(videoInstance)
9567011b
C
171
172 return res.json({ description })
173}
174
04b8c3fb 175async function listVideos (req: express.Request, res: express.Response) {
2760b454
C
176 const serverActor = await getServerActor()
177
d6886027 178 const query = pickCommonVideoQuery(req.query)
fe987656
C
179 const countVideos = getCountVideos(req)
180
b4055e1c 181 const apiOptions = await Hooks.wrapObject({
d6886027
C
182 ...query,
183
2760b454
C
184 displayOnlyForFollower: {
185 actorId: serverActor.id,
186 orLocalVideos: true
187 },
1fd61899 188 nsfw: buildNSFWFilter(res, query.nsfw),
fe987656
C
189 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
190 countVideos
b4055e1c
C
191 }, 'filter:api.videos.list.params')
192
89cd1275
C
193 const resultList = await Hooks.wrapPromiseFun(
194 VideoModel.listForApi,
195 apiOptions,
b4055e1c
C
196 'filter:api.videos.list.result'
197 )
eb080476 198
2760b454 199 return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
9f10b292 200}
c45f7f84 201
c158a5fa 202async function removeVideo (_req: express.Request, res: express.Response) {
453e83ea 203 const videoInstance = res.locals.videoAll
91f6f169 204
3fd3ab2d 205 await sequelizeTypescript.transaction(async t => {
eb080476 206 await videoInstance.destroy({ transaction: t })
91f6f169 207 })
eb080476 208
993cef4b 209 auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON()))
eb080476 210 logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid)
90d4bb81 211
b4055e1c
C
212 Hooks.runAction('action:api.video.deleted', { video: videoInstance })
213
2d53be02
RK
214 return res.type('json')
215 .status(HttpStatusCode.NO_CONTENT_204)
216 .end()
9f10b292 217}
304a84d5
C
218
219// ---------------------------------------------------------------------------
220
221// FIXME: Should not exist, we rely on specific API
222async function fetchRemoteVideoDescription (video: MVideoAccountLight) {
223 const host = video.VideoChannel.Account.Actor.Server.host
224 const path = video.getDescriptionAPIPath()
225 const url = REMOTE_SCHEME.HTTP + '://' + host + path
226
227 const { body } = await doJSONRequest<any>(url)
228 return body.description || ''
229}