diff options
Diffstat (limited to 'server/controllers/api/videos/index.ts')
-rw-r--r-- | server/controllers/api/videos/index.ts | 228 |
1 files changed, 0 insertions, 228 deletions
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts deleted file mode 100644 index 3cdd42289..000000000 --- a/server/controllers/api/videos/index.ts +++ /dev/null | |||
@@ -1,228 +0,0 @@ | |||
1 | import express from 'express' | ||
2 | import { pickCommonVideoQuery } from '@server/helpers/query' | ||
3 | import { doJSONRequest } from '@server/helpers/requests' | ||
4 | import { openapiOperationDoc } from '@server/middlewares/doc' | ||
5 | import { getServerActor } from '@server/models/application/application' | ||
6 | import { MVideoAccountLight } from '@server/types/models' | ||
7 | import { HttpStatusCode } from '../../../../shared/models' | ||
8 | import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger' | ||
9 | import { buildNSFWFilter, getCountVideos } from '../../../helpers/express-utils' | ||
10 | import { logger } from '../../../helpers/logger' | ||
11 | import { getFormattedObjects } from '../../../helpers/utils' | ||
12 | import { REMOTE_SCHEME, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../../initializers/constants' | ||
13 | import { sequelizeTypescript } from '../../../initializers/database' | ||
14 | import { JobQueue } from '../../../lib/job-queue' | ||
15 | import { Hooks } from '../../../lib/plugins/hooks' | ||
16 | import { | ||
17 | apiRateLimiter, | ||
18 | asyncMiddleware, | ||
19 | asyncRetryTransactionMiddleware, | ||
20 | authenticate, | ||
21 | checkVideoFollowConstraints, | ||
22 | commonVideosFiltersValidator, | ||
23 | optionalAuthenticate, | ||
24 | paginationValidator, | ||
25 | setDefaultPagination, | ||
26 | setDefaultVideosSort, | ||
27 | videosCustomGetValidator, | ||
28 | videosGetValidator, | ||
29 | videosRemoveValidator, | ||
30 | videosSortValidator | ||
31 | } from '../../../middlewares' | ||
32 | import { guessAdditionalAttributesFromQuery } from '../../../models/video/formatter' | ||
33 | import { VideoModel } from '../../../models/video/video' | ||
34 | import { blacklistRouter } from './blacklist' | ||
35 | import { videoCaptionsRouter } from './captions' | ||
36 | import { videoCommentRouter } from './comment' | ||
37 | import { filesRouter } from './files' | ||
38 | import { videoImportsRouter } from './import' | ||
39 | import { liveRouter } from './live' | ||
40 | import { ownershipVideoRouter } from './ownership' | ||
41 | import { videoPasswordRouter } from './passwords' | ||
42 | import { rateVideoRouter } from './rate' | ||
43 | import { videoSourceRouter } from './source' | ||
44 | import { statsRouter } from './stats' | ||
45 | import { storyboardRouter } from './storyboard' | ||
46 | import { studioRouter } from './studio' | ||
47 | import { tokenRouter } from './token' | ||
48 | import { transcodingRouter } from './transcoding' | ||
49 | import { updateRouter } from './update' | ||
50 | import { uploadRouter } from './upload' | ||
51 | import { viewRouter } from './view' | ||
52 | |||
53 | const auditLogger = auditLoggerFactory('videos') | ||
54 | const videosRouter = express.Router() | ||
55 | |||
56 | videosRouter.use(apiRateLimiter) | ||
57 | |||
58 | videosRouter.use('/', blacklistRouter) | ||
59 | videosRouter.use('/', statsRouter) | ||
60 | videosRouter.use('/', rateVideoRouter) | ||
61 | videosRouter.use('/', videoCommentRouter) | ||
62 | videosRouter.use('/', studioRouter) | ||
63 | videosRouter.use('/', videoCaptionsRouter) | ||
64 | videosRouter.use('/', videoImportsRouter) | ||
65 | videosRouter.use('/', ownershipVideoRouter) | ||
66 | videosRouter.use('/', viewRouter) | ||
67 | videosRouter.use('/', liveRouter) | ||
68 | videosRouter.use('/', uploadRouter) | ||
69 | videosRouter.use('/', updateRouter) | ||
70 | videosRouter.use('/', filesRouter) | ||
71 | videosRouter.use('/', transcodingRouter) | ||
72 | videosRouter.use('/', tokenRouter) | ||
73 | videosRouter.use('/', videoPasswordRouter) | ||
74 | videosRouter.use('/', storyboardRouter) | ||
75 | videosRouter.use('/', videoSourceRouter) | ||
76 | |||
77 | videosRouter.get('/categories', | ||
78 | openapiOperationDoc({ operationId: 'getCategories' }), | ||
79 | listVideoCategories | ||
80 | ) | ||
81 | videosRouter.get('/licences', | ||
82 | openapiOperationDoc({ operationId: 'getLicences' }), | ||
83 | listVideoLicences | ||
84 | ) | ||
85 | videosRouter.get('/languages', | ||
86 | openapiOperationDoc({ operationId: 'getLanguages' }), | ||
87 | listVideoLanguages | ||
88 | ) | ||
89 | videosRouter.get('/privacies', | ||
90 | openapiOperationDoc({ operationId: 'getPrivacies' }), | ||
91 | listVideoPrivacies | ||
92 | ) | ||
93 | |||
94 | videosRouter.get('/', | ||
95 | openapiOperationDoc({ operationId: 'getVideos' }), | ||
96 | paginationValidator, | ||
97 | videosSortValidator, | ||
98 | setDefaultVideosSort, | ||
99 | setDefaultPagination, | ||
100 | optionalAuthenticate, | ||
101 | commonVideosFiltersValidator, | ||
102 | asyncMiddleware(listVideos) | ||
103 | ) | ||
104 | |||
105 | // TODO: remove, deprecated in 5.0 now we send the complete description in VideoDetails | ||
106 | videosRouter.get('/:id/description', | ||
107 | openapiOperationDoc({ operationId: 'getVideoDesc' }), | ||
108 | asyncMiddleware(videosGetValidator), | ||
109 | asyncMiddleware(getVideoDescription) | ||
110 | ) | ||
111 | |||
112 | videosRouter.get('/:id', | ||
113 | openapiOperationDoc({ operationId: 'getVideo' }), | ||
114 | optionalAuthenticate, | ||
115 | asyncMiddleware(videosCustomGetValidator('for-api')), | ||
116 | asyncMiddleware(checkVideoFollowConstraints), | ||
117 | asyncMiddleware(getVideo) | ||
118 | ) | ||
119 | |||
120 | videosRouter.delete('/:id', | ||
121 | openapiOperationDoc({ operationId: 'delVideo' }), | ||
122 | authenticate, | ||
123 | asyncMiddleware(videosRemoveValidator), | ||
124 | asyncRetryTransactionMiddleware(removeVideo) | ||
125 | ) | ||
126 | |||
127 | // --------------------------------------------------------------------------- | ||
128 | |||
129 | export { | ||
130 | videosRouter | ||
131 | } | ||
132 | |||
133 | // --------------------------------------------------------------------------- | ||
134 | |||
135 | function listVideoCategories (_req: express.Request, res: express.Response) { | ||
136 | res.json(VIDEO_CATEGORIES) | ||
137 | } | ||
138 | |||
139 | function listVideoLicences (_req: express.Request, res: express.Response) { | ||
140 | res.json(VIDEO_LICENCES) | ||
141 | } | ||
142 | |||
143 | function listVideoLanguages (_req: express.Request, res: express.Response) { | ||
144 | res.json(VIDEO_LANGUAGES) | ||
145 | } | ||
146 | |||
147 | function listVideoPrivacies (_req: express.Request, res: express.Response) { | ||
148 | res.json(VIDEO_PRIVACIES) | ||
149 | } | ||
150 | |||
151 | async function getVideo (_req: express.Request, res: express.Response) { | ||
152 | const videoId = res.locals.videoAPI.id | ||
153 | const userId = res.locals.oauth?.token.User.id | ||
154 | |||
155 | const video = await Hooks.wrapObject(res.locals.videoAPI, 'filter:api.video.get.result', { id: videoId, userId }) | ||
156 | |||
157 | if (video.isOutdated()) { | ||
158 | JobQueue.Instance.createJobAsync({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } }) | ||
159 | } | ||
160 | |||
161 | return res.json(video.toFormattedDetailsJSON()) | ||
162 | } | ||
163 | |||
164 | async function getVideoDescription (req: express.Request, res: express.Response) { | ||
165 | const videoInstance = res.locals.videoAll | ||
166 | |||
167 | const description = videoInstance.isOwned() | ||
168 | ? videoInstance.description | ||
169 | : await fetchRemoteVideoDescription(videoInstance) | ||
170 | |||
171 | return res.json({ description }) | ||
172 | } | ||
173 | |||
174 | async function listVideos (req: express.Request, res: express.Response) { | ||
175 | const serverActor = await getServerActor() | ||
176 | |||
177 | const query = pickCommonVideoQuery(req.query) | ||
178 | const countVideos = getCountVideos(req) | ||
179 | |||
180 | const apiOptions = await Hooks.wrapObject({ | ||
181 | ...query, | ||
182 | |||
183 | displayOnlyForFollower: { | ||
184 | actorId: serverActor.id, | ||
185 | orLocalVideos: true | ||
186 | }, | ||
187 | nsfw: buildNSFWFilter(res, query.nsfw), | ||
188 | user: res.locals.oauth ? res.locals.oauth.token.User : undefined, | ||
189 | countVideos | ||
190 | }, 'filter:api.videos.list.params') | ||
191 | |||
192 | const resultList = await Hooks.wrapPromiseFun( | ||
193 | VideoModel.listForApi, | ||
194 | apiOptions, | ||
195 | 'filter:api.videos.list.result' | ||
196 | ) | ||
197 | |||
198 | return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query))) | ||
199 | } | ||
200 | |||
201 | async function removeVideo (req: express.Request, res: express.Response) { | ||
202 | const videoInstance = res.locals.videoAll | ||
203 | |||
204 | await sequelizeTypescript.transaction(async t => { | ||
205 | await videoInstance.destroy({ transaction: t }) | ||
206 | }) | ||
207 | |||
208 | auditLogger.delete(getAuditIdFromRes(res), new VideoAuditView(videoInstance.toFormattedDetailsJSON())) | ||
209 | logger.info('Video with name %s and uuid %s deleted.', videoInstance.name, videoInstance.uuid) | ||
210 | |||
211 | Hooks.runAction('action:api.video.deleted', { video: videoInstance, req, res }) | ||
212 | |||
213 | return res.type('json') | ||
214 | .status(HttpStatusCode.NO_CONTENT_204) | ||
215 | .end() | ||
216 | } | ||
217 | |||
218 | // --------------------------------------------------------------------------- | ||
219 | |||
220 | // FIXME: Should not exist, we rely on specific API | ||
221 | async function fetchRemoteVideoDescription (video: MVideoAccountLight) { | ||
222 | const host = video.VideoChannel.Account.Actor.Server.host | ||
223 | const path = video.getDescriptionAPIPath() | ||
224 | const url = REMOTE_SCHEME.HTTP + '://' + host + path | ||
225 | |||
226 | const { body } = await doJSONRequest<any>(url) | ||
227 | return body.description || '' | ||
228 | } | ||