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