]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/static.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
CommitLineData
41fb13c3
C
1import cors from 'cors'
2import express from 'express'
a8b1b404 3import { join } from 'path'
a8b1b404 4import { serveIndexHTML } from '@server/lib/client-html'
2539932e 5import { ServerConfigManager } from '@server/lib/server-config-manager'
c0e8b12e 6import { HttpStatusCode } from '@shared/models'
2b02c520 7import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo/nodeinfo.model'
06aad801 8import { root } from '@shared/core-utils'
a8b1b404 9import { CONFIG, isEmailEnabled } from '../initializers/config'
9c6ca37f 10import {
4c1c1709
C
11 CONSTRAINTS_FIELDS,
12 DEFAULT_THEME_NAME,
34dd7cb4
C
13 HLS_STREAMING_PLAYLIST_DIRECTORY,
14 PEERTUBE_VERSION,
9c6ca37f 15 ROUTE_CACHE_LIFETIME,
9c6ca37f 16 STATIC_MAX_AGE,
6dd9de95 17 STATIC_PATHS,
4c1c1709 18 WEBSERVER
74dc3bca 19} from '../initializers/constants'
a8b1b404 20import { getThemeOrDefault } from '../lib/plugins/theme-utils'
90a8bd30 21import { asyncMiddleware } from '../middlewares'
20bafcb6 22import { cacheRoute } from '../middlewares/cache/cache'
7d9ba5c0 23import { UserModel } from '../models/user/user'
a8b1b404 24import { VideoModel } from '../models/video/video'
3f6d68d9 25import { VideoCommentModel } from '../models/video/video-comment'
65fcc311
C
26
27const staticRouter = express.Router()
28
62945f06
C
29staticRouter.use(cors())
30
65fcc311 31/*
60862425 32 Cors is very important to let other servers access torrent and video files
65fcc311
C
33*/
34
90a8bd30 35// Videos path for webseed
65fcc311
C
36staticRouter.use(
37 STATIC_PATHS.WEBSEED,
b9fffa29 38 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
65fcc311 39)
6040f87d 40staticRouter.use(
b9fffa29 41 STATIC_PATHS.REDUNDANCY,
b9fffa29 42 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
6040f87d
C
43)
44
09209296
C
45// HLS
46staticRouter.use(
9c6ca37f 47 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
09209296 48 cors(),
9c6ca37f 49 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
09209296
C
50)
51
65fcc311
C
52// Thumbnails path for express
53const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
54staticRouter.use(
55 STATIC_PATHS.THUMBNAILS,
cd4cb177 56 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
65fcc311
C
57)
58
ac235c37 59// robots.txt service
3f6d68d9 60staticRouter.get('/robots.txt',
20bafcb6 61 cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS),
3f6d68d9
RK
62 (_, res: express.Response) => {
63 res.type('text/plain')
42cc1887 64
3f6d68d9
RK
65 return res.send(CONFIG.INSTANCE.ROBOTS)
66 }
67)
68
f2eb23cd
RK
69staticRouter.all('/teapot',
70 getCup,
71 asyncMiddleware(serveIndexHTML)
72)
73
5447516b
AH
74// security.txt service
75staticRouter.get('/security.txt',
76 (_, res: express.Response) => {
2d53be02 77 return res.redirect(HttpStatusCode.MOVED_PERMANENTLY_301, '/.well-known/security.txt')
5447516b
AH
78 }
79)
80
81staticRouter.get('/.well-known/security.txt',
20bafcb6 82 cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT),
5447516b
AH
83 (_, res: express.Response) => {
84 res.type('text/plain')
85 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
86 }
87)
88
3f6d68d9
RK
89// nodeinfo service
90staticRouter.use('/.well-known/nodeinfo',
20bafcb6 91 cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO),
3f6d68d9
RK
92 (_, res: express.Response) => {
93 return res.json({
94 links: [
95 {
96 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
6dd9de95 97 href: WEBSERVER.URL + '/nodeinfo/2.0.json'
3f6d68d9
RK
98 }
99 ]
100 })
101 }
102)
103staticRouter.use('/nodeinfo/:version.json',
20bafcb6 104 cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO),
3f6d68d9
RK
105 asyncMiddleware(generateNodeinfo)
106)
ac235c37 107
aad0ec24
RK
108// dnt-policy.txt service (see https://www.eff.org/dnt-policy)
109staticRouter.use('/.well-known/dnt-policy.txt',
20bafcb6 110 cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY),
aad0ec24
RK
111 (_, res: express.Response) => {
112 res.type('text/plain')
aac0118d 113
d1105b97 114 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
aad0ec24
RK
115 }
116)
117
118// dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
119staticRouter.use('/.well-known/dnt/',
120 (_, res: express.Response) => {
121 res.json({ tracking: 'N' })
31414127
RK
122 }
123)
124
125staticRouter.use('/.well-known/change-password',
126 (_, res: express.Response) => {
127 res.redirect('/my-account/settings')
aad0ec24
RK
128 }
129)
130
3ddb1ec5
C
131staticRouter.use('/.well-known/host-meta',
132 (_, res: express.Response) => {
03371ad9 133 res.type('application/xml')
3ddb1ec5
C
134
135 const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
136 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
137 ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
138 '</XRD>'
139
140 res.send(xml).end()
141 }
142)
143
65fcc311
C
144// ---------------------------------------------------------------------------
145
146export {
147 staticRouter
148}
f981dae8
C
149
150// ---------------------------------------------------------------------------
151
536598cf 152async function generateNodeinfo (req: express.Request, res: express.Response) {
3f6d68d9
RK
153 const { totalVideos } = await VideoModel.getStats()
154 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
47d8e266 155 const { totalUsers, totalMonthlyActiveUsers, totalHalfYearActiveUsers } = await UserModel.getStats()
3f6d68d9 156
ec908b4a
C
157 if (!req.params.version || req.params.version !== '2.0') {
158 return res.fail({
159 status: HttpStatusCode.NOT_FOUND_404,
160 message: 'Nodeinfo schema version not handled'
161 })
162 }
163
164 const json = {
165 version: '2.0',
166 software: {
167 name: 'peertube',
168 version: PEERTUBE_VERSION
169 },
170 protocols: [
171 'activitypub'
172 ],
173 services: {
174 inbound: [],
175 outbound: [
176 'atom1.0',
177 'rss2.0'
178 ]
179 },
180 openRegistrations: CONFIG.SIGNUP.ENABLED,
181 usage: {
182 users: {
183 total: totalUsers,
184 activeMonth: totalMonthlyActiveUsers,
185 activeHalfyear: totalHalfYearActiveUsers
3f6d68d9 186 },
ec908b4a
C
187 localPosts: totalVideos,
188 localComments: totalLocalVideoComments
189 },
190 metadata: {
191 taxonomy: {
192 postsName: 'Videos'
3f6d68d9 193 },
ec908b4a
C
194 nodeName: CONFIG.INSTANCE.NAME,
195 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
196 nodeConfig: {
197 search: {
198 remoteUri: {
199 users: CONFIG.SEARCH.REMOTE_URI.USERS,
200 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
201 }
3f6d68d9 202 },
ec908b4a
C
203 plugin: {
204 registered: ServerConfigManager.Instance.getRegisteredPlugins()
3f6d68d9 205 },
ec908b4a
C
206 theme: {
207 registered: ServerConfigManager.Instance.getRegisteredThemes(),
208 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
209 },
210 email: {
211 enabled: isEmailEnabled()
212 },
213 contactForm: {
214 enabled: CONFIG.CONTACT_FORM.ENABLED
215 },
216 transcoding: {
217 hls: {
218 enabled: CONFIG.TRANSCODING.HLS.ENABLED
174e0855 219 },
ec908b4a
C
220 webtorrent: {
221 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
174e0855 222 },
ec908b4a
C
223 enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('vod')
224 },
225 live: {
226 enabled: CONFIG.LIVE.ENABLED,
174e0855 227 transcoding: {
ec908b4a
C
228 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
229 enabledResolutions: ServerConfigManager.Instance.getEnabledResolutions('live')
230 }
231 },
232 import: {
233 videos: {
234 http: {
235 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
174e0855 236 },
ec908b4a
C
237 torrent: {
238 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
174e0855 239 }
ec908b4a
C
240 }
241 },
242 autoBlacklist: {
243 videos: {
244 ofUsers: {
245 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
174e0855 246 }
ec908b4a
C
247 }
248 },
249 avatar: {
250 file: {
251 size: {
252 max: CONSTRAINTS_FIELDS.ACTORS.IMAGE.FILE_SIZE.max
174e0855 253 },
ec908b4a
C
254 extensions: CONSTRAINTS_FIELDS.ACTORS.IMAGE.EXTNAME
255 }
256 },
257 video: {
258 image: {
259 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
260 size: {
261 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
174e0855
RK
262 }
263 },
ec908b4a
C
264 file: {
265 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
266 }
267 },
268 videoCaption: {
269 file: {
270 size: {
271 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
272 },
273 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
174e0855 274 }
ec908b4a
C
275 },
276 user: {
277 videoQuota: CONFIG.USER.VIDEO_QUOTA,
278 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
279 },
280 trending: {
281 videos: {
282 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
283 }
284 },
285 tracker: {
286 enabled: CONFIG.TRACKER.ENABLED
174e0855 287 }
3f6d68d9 288 }
ec908b4a
C
289 }
290 } as HttpNodeinfoDiasporaSoftwareNsSchema20
3f6d68d9 291
ec908b4a
C
292 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
293 .send(json)
294 .end()
3f6d68d9
RK
295}
296
f2eb23cd
RK
297function getCup (req: express.Request, res: express.Response, next: express.NextFunction) {
298 res.status(HttpStatusCode.I_AM_A_TEAPOT_418)
299 res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1')
300 res.setHeader('Safe', 'if-sepia-awake')
301
302 return next()
303}