]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/static.ts
client(hotkeys): remove seekstep VLC behavior
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
CommitLineData
4d4e5cd4 1import * as cors from 'cors'
50d6de9c 2import * as express from 'express'
a8b1b404
C
3import { join } from 'path'
4import { getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config'
5import { serveIndexHTML } from '@server/lib/client-html'
6import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths'
7import { MVideoFile, MVideoFullLight } from '@server/types/models'
8import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
9import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type'
10import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
11import { root } from '../helpers/core-utils'
12import { CONFIG, isEmailEnabled } from '../initializers/config'
9c6ca37f 13import {
4c1c1709
C
14 CONSTRAINTS_FIELDS,
15 DEFAULT_THEME_NAME,
34dd7cb4
C
16 HLS_STREAMING_PLAYLIST_DIRECTORY,
17 PEERTUBE_VERSION,
9c6ca37f
C
18 ROUTE_CACHE_LIFETIME,
19 STATIC_DOWNLOAD_PATHS,
20 STATIC_MAX_AGE,
6dd9de95 21 STATIC_PATHS,
4c1c1709 22 WEBSERVER
74dc3bca 23} from '../initializers/constants'
a8b1b404
C
24import { getThemeOrDefault } from '../lib/plugins/theme-utils'
25import { getEnabledResolutions } from '../lib/video-transcoding'
eccf70f0 26import { asyncMiddleware, videosDownloadValidator } from '../middlewares'
a8b1b404 27import { cacheRoute } from '../middlewares/cache'
3f6d68d9 28import { UserModel } from '../models/account/user'
a8b1b404 29import { VideoModel } from '../models/video/video'
3f6d68d9 30import { VideoCommentModel } from '../models/video/video-comment'
65fcc311
C
31
32const staticRouter = express.Router()
33
62945f06
C
34staticRouter.use(cors())
35
65fcc311 36/*
60862425 37 Cors is very important to let other servers access torrent and video files
65fcc311
C
38*/
39
40const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
41staticRouter.use(
42 STATIC_PATHS.TORRENTS,
43 cors(),
49379960 44 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
65fcc311 45)
02756fbd
C
46staticRouter.use(
47 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
eccf70f0 48 asyncMiddleware(videosDownloadValidator),
a1587156 49 downloadTorrent
02756fbd 50)
d7a25329
C
51staticRouter.use(
52 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+)-hls.torrent',
eccf70f0 53 asyncMiddleware(videosDownloadValidator),
a1587156 54 downloadHLSVideoFileTorrent
d7a25329 55)
65fcc311
C
56
57// Videos path for webseeding
65fcc311
C
58staticRouter.use(
59 STATIC_PATHS.WEBSEED,
60 cors(),
b9fffa29 61 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
65fcc311 62)
6040f87d 63staticRouter.use(
b9fffa29 64 STATIC_PATHS.REDUNDANCY,
6040f87d 65 cors(),
b9fffa29 66 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
6040f87d
C
67)
68
02756fbd
C
69staticRouter.use(
70 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
eccf70f0 71 asyncMiddleware(videosDownloadValidator),
a1587156 72 downloadVideoFile
02756fbd 73)
65fcc311 74
d7a25329 75staticRouter.use(
efcd6f2e 76 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
eccf70f0 77 asyncMiddleware(videosDownloadValidator),
a1587156 78 downloadHLSVideoFile
d7a25329
C
79)
80
09209296
C
81// HLS
82staticRouter.use(
9c6ca37f 83 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
09209296 84 cors(),
9c6ca37f 85 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
09209296
C
86)
87
65fcc311
C
88// Thumbnails path for express
89const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
90staticRouter.use(
91 STATIC_PATHS.THUMBNAILS,
cd4cb177 92 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
65fcc311
C
93)
94
ac235c37 95// robots.txt service
3f6d68d9 96staticRouter.get('/robots.txt',
f2f0eda5 97 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ROBOTS)),
3f6d68d9
RK
98 (_, res: express.Response) => {
99 res.type('text/plain')
100 return res.send(CONFIG.INSTANCE.ROBOTS)
101 }
102)
103
f2eb23cd
RK
104staticRouter.all('/teapot',
105 getCup,
106 asyncMiddleware(serveIndexHTML)
107)
108
5447516b
AH
109// security.txt service
110staticRouter.get('/security.txt',
111 (_, res: express.Response) => {
2d53be02 112 return res.redirect(HttpStatusCode.MOVED_PERMANENTLY_301, '/.well-known/security.txt')
5447516b
AH
113 }
114)
115
116staticRouter.get('/.well-known/security.txt',
f2f0eda5 117 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
5447516b
AH
118 (_, res: express.Response) => {
119 res.type('text/plain')
120 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
121 }
122)
123
3f6d68d9
RK
124// nodeinfo service
125staticRouter.use('/.well-known/nodeinfo',
f2f0eda5 126 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)),
3f6d68d9
RK
127 (_, res: express.Response) => {
128 return res.json({
129 links: [
130 {
131 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
6dd9de95 132 href: WEBSERVER.URL + '/nodeinfo/2.0.json'
3f6d68d9
RK
133 }
134 ]
135 })
136 }
137)
138staticRouter.use('/nodeinfo/:version.json',
f2f0eda5 139 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)),
3f6d68d9
RK
140 asyncMiddleware(generateNodeinfo)
141)
ac235c37 142
aad0ec24
RK
143// dnt-policy.txt service (see https://www.eff.org/dnt-policy)
144staticRouter.use('/.well-known/dnt-policy.txt',
f2f0eda5 145 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
aad0ec24
RK
146 (_, res: express.Response) => {
147 res.type('text/plain')
aac0118d 148
d1105b97 149 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
aad0ec24
RK
150 }
151)
152
153// dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
154staticRouter.use('/.well-known/dnt/',
155 (_, res: express.Response) => {
156 res.json({ tracking: 'N' })
31414127
RK
157 }
158)
159
160staticRouter.use('/.well-known/change-password',
161 (_, res: express.Response) => {
162 res.redirect('/my-account/settings')
aad0ec24
RK
163 }
164)
165
3ddb1ec5
C
166staticRouter.use('/.well-known/host-meta',
167 (_, res: express.Response) => {
03371ad9 168 res.type('application/xml')
3ddb1ec5
C
169
170 const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
171 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
172 ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
173 '</XRD>'
174
175 res.send(xml).end()
176 }
177)
178
65fcc311
C
179// ---------------------------------------------------------------------------
180
181export {
182 staticRouter
183}
f981dae8
C
184
185// ---------------------------------------------------------------------------
186
536598cf 187async function generateNodeinfo (req: express.Request, res: express.Response) {
3f6d68d9
RK
188 const { totalVideos } = await VideoModel.getStats()
189 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
47d8e266 190 const { totalUsers, totalMonthlyActiveUsers, totalHalfYearActiveUsers } = await UserModel.getStats()
3f6d68d9
RK
191 let json = {}
192
193 if (req.params.version && (req.params.version === '2.0')) {
194 json = {
195 version: '2.0',
196 software: {
197 name: 'peertube',
66170ca8 198 version: PEERTUBE_VERSION
3f6d68d9
RK
199 },
200 protocols: [
201 'activitypub'
202 ],
203 services: {
204 inbound: [],
205 outbound: [
206 'atom1.0',
207 'rss2.0'
208 ]
209 },
210 openRegistrations: CONFIG.SIGNUP.ENABLED,
211 usage: {
212 users: {
47d8e266
C
213 total: totalUsers,
214 activeMonth: totalMonthlyActiveUsers,
215 activeHalfyear: totalHalfYearActiveUsers
3f6d68d9
RK
216 },
217 localPosts: totalVideos,
218 localComments: totalLocalVideoComments
219 },
220 metadata: {
221 taxonomy: {
222 postsName: 'Videos'
223 },
224 nodeName: CONFIG.INSTANCE.NAME,
174e0855
RK
225 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
226 nodeConfig: {
9677fca7
RK
227 search: {
228 remoteUri: {
229 users: CONFIG.SEARCH.REMOTE_URI.USERS,
230 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
231 }
232 },
174e0855
RK
233 plugin: {
234 registered: getRegisteredPlugins()
235 },
236 theme: {
237 registered: getRegisteredThemes(),
238 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
239 },
240 email: {
4c1c1709 241 enabled: isEmailEnabled()
174e0855
RK
242 },
243 contactForm: {
244 enabled: CONFIG.CONTACT_FORM.ENABLED
245 },
246 transcoding: {
247 hls: {
248 enabled: CONFIG.TRANSCODING.HLS.ENABLED
249 },
250 webtorrent: {
251 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
252 },
c6c0fa6c
C
253 enabledResolutions: getEnabledResolutions('vod')
254 },
255 live: {
256 enabled: CONFIG.LIVE.ENABLED,
257 transcoding: {
258 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
259 enabledResolutions: getEnabledResolutions('live')
260 }
174e0855
RK
261 },
262 import: {
263 videos: {
264 http: {
265 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
266 },
267 torrent: {
268 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
269 }
270 }
271 },
272 autoBlacklist: {
273 videos: {
274 ofUsers: {
275 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
276 }
277 }
278 },
279 avatar: {
280 file: {
281 size: {
282 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
283 },
284 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
285 }
286 },
287 video: {
288 image: {
289 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
290 size: {
291 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
292 }
293 },
294 file: {
295 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
296 }
297 },
298 videoCaption: {
299 file: {
300 size: {
301 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
302 },
303 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
304 }
305 },
306 user: {
307 videoQuota: CONFIG.USER.VIDEO_QUOTA,
308 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
309 },
310 trending: {
311 videos: {
312 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
313 }
314 },
315 tracker: {
316 enabled: CONFIG.TRACKER.ENABLED
317 }
318 }
3f6d68d9
RK
319 }
320 } as HttpNodeinfoDiasporaSoftwareNsSchema20
98d3324d 321 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
3f6d68d9
RK
322 } else {
323 json = { error: 'Nodeinfo schema version not handled' }
2d53be02 324 res.status(HttpStatusCode.NOT_FOUND_404)
3f6d68d9
RK
325 }
326
98d3324d 327 return res.send(json).end()
3f6d68d9
RK
328}
329
a1587156 330function downloadTorrent (req: express.Request, res: express.Response) {
d7a25329
C
331 const video = res.locals.videoAll
332
333 const videoFile = getVideoFile(req, video.VideoFiles)
2d53be02 334 if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end()
d7a25329
C
335
336 return res.download(getTorrentFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
337}
338
a1587156 339function downloadHLSVideoFileTorrent (req: express.Request, res: express.Response) {
d7a25329
C
340 const video = res.locals.videoAll
341
342 const playlist = getHLSPlaylist(video)
2d53be02 343 if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end
d7a25329
C
344
345 const videoFile = getVideoFile(req, playlist.VideoFiles)
2d53be02 346 if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end()
02756fbd 347
d7a25329 348 return res.download(getTorrentFilePath(playlist, videoFile), `${video.name}-${videoFile.resolution}p-hls.torrent`)
02756fbd
C
349}
350
a1587156 351function downloadVideoFile (req: express.Request, res: express.Response) {
d7a25329
C
352 const video = res.locals.videoAll
353
354 const videoFile = getVideoFile(req, video.VideoFiles)
2d53be02 355 if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end()
02756fbd 356
d7a25329 357 return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
02756fbd
C
358}
359
a1587156 360function downloadHLSVideoFile (req: express.Request, res: express.Response) {
453e83ea 361 const video = res.locals.videoAll
d7a25329 362 const playlist = getHLSPlaylist(video)
2d53be02 363 if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end
d7a25329
C
364
365 const videoFile = getVideoFile(req, playlist.VideoFiles)
2d53be02 366 if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end()
d7a25329
C
367
368 const filename = `${video.name}-${videoFile.resolution}p-${playlist.getStringType()}${videoFile.extname}`
369 return res.download(getVideoFilePath(playlist, videoFile), filename)
370}
371
372function getVideoFile (req: express.Request, files: MVideoFile[]) {
373 const resolution = parseInt(req.params.resolution, 10)
374 return files.find(f => f.resolution === resolution)
375}
02756fbd 376
d7a25329
C
377function getHLSPlaylist (video: MVideoFullLight) {
378 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
379 if (!playlist) return undefined
02756fbd 380
d7a25329 381 return Object.assign(playlist, { Video: video })
02756fbd 382}
f2eb23cd
RK
383
384function getCup (req: express.Request, res: express.Response, next: express.NextFunction) {
385 res.status(HttpStatusCode.I_AM_A_TEAPOT_418)
386 res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1')
387 res.setHeader('Safe', 'if-sepia-awake')
388
389 return next()
390}