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