]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/static.ts
Add sitemap
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
... / ...
CommitLineData
1import * as cors from 'cors'
2import * as express from 'express'
3import { CONFIG, ROUTE_CACHE_LIFETIME, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
4import { VideosPreviewCache } from '../lib/cache'
5import { cacheRoute } from '../middlewares/cache'
6import { asyncMiddleware, videosGetValidator } from '../middlewares'
7import { VideoModel } from '../models/video/video'
8import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
9import { UserModel } from '../models/account/user'
10import { VideoCommentModel } from '../models/video/video-comment'
11import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
12import { join } from 'path'
13import { root } from '../helpers/core-utils'
14
15const packageJSON = require('../../../package.json')
16const staticRouter = express.Router()
17
18staticRouter.use(cors())
19
20/*
21 Cors is very important to let other servers access torrent and video files
22*/
23
24const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
25staticRouter.use(
26 STATIC_PATHS.TORRENTS,
27 cors(),
28 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
29)
30staticRouter.use(
31 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
32 asyncMiddleware(videosGetValidator),
33 asyncMiddleware(downloadTorrent)
34)
35
36// Videos path for webseeding
37staticRouter.use(
38 STATIC_PATHS.WEBSEED,
39 cors(),
40 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
41)
42staticRouter.use(
43 STATIC_PATHS.REDUNDANCY,
44 cors(),
45 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
46)
47
48staticRouter.use(
49 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
50 asyncMiddleware(videosGetValidator),
51 asyncMiddleware(downloadVideoFile)
52)
53
54// Thumbnails path for express
55const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
56staticRouter.use(
57 STATIC_PATHS.THUMBNAILS,
58 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
59)
60
61const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
62staticRouter.use(
63 STATIC_PATHS.AVATARS,
64 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
65)
66
67// We don't have video previews, fetch them from the origin instance
68staticRouter.use(
69 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
70 asyncMiddleware(getPreview)
71)
72
73// We don't have video captions, fetch them from the origin instance
74staticRouter.use(
75 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
76 asyncMiddleware(getVideoCaption)
77)
78
79// robots.txt service
80staticRouter.get('/robots.txt',
81 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
82 (_, res: express.Response) => {
83 res.type('text/plain')
84 return res.send(CONFIG.INSTANCE.ROBOTS)
85 }
86)
87
88// security.txt service
89staticRouter.get('/security.txt',
90 (_, res: express.Response) => {
91 return res.redirect(301, '/.well-known/security.txt')
92 }
93)
94
95staticRouter.get('/.well-known/security.txt',
96 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
97 (_, res: express.Response) => {
98 res.type('text/plain')
99 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
100 }
101)
102
103// nodeinfo service
104staticRouter.use('/.well-known/nodeinfo',
105 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
106 (_, res: express.Response) => {
107 return res.json({
108 links: [
109 {
110 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
111 href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json'
112 }
113 ]
114 })
115 }
116)
117staticRouter.use('/nodeinfo/:version.json',
118 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
119 asyncMiddleware(generateNodeinfo)
120)
121
122// dnt-policy.txt service (see https://www.eff.org/dnt-policy)
123staticRouter.use('/.well-known/dnt-policy.txt',
124 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
125 (_, res: express.Response) => {
126 res.type('text/plain')
127
128 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
129 }
130)
131
132// dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
133staticRouter.use('/.well-known/dnt/',
134 (_, res: express.Response) => {
135 res.json({ tracking: 'N' })
136 }
137)
138
139// ---------------------------------------------------------------------------
140
141export {
142 staticRouter
143}
144
145// ---------------------------------------------------------------------------
146
147async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
148 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
149 if (!path) return res.sendStatus(404)
150
151 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
152}
153
154async function getVideoCaption (req: express.Request, res: express.Response) {
155 const path = await VideosCaptionCache.Instance.getFilePath({
156 videoId: req.params.videoId,
157 language: req.params.captionLanguage
158 })
159 if (!path) return res.sendStatus(404)
160
161 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
162}
163
164async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) {
165 const { totalVideos } = await VideoModel.getStats()
166 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
167 const { totalUsers } = await UserModel.getStats()
168 let json = {}
169
170 if (req.params.version && (req.params.version === '2.0')) {
171 json = {
172 version: '2.0',
173 software: {
174 name: 'peertube',
175 version: packageJSON.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 },
192 localPosts: totalVideos,
193 localComments: totalLocalVideoComments
194 },
195 metadata: {
196 taxonomy: {
197 postsName: 'Videos'
198 },
199 nodeName: CONFIG.INSTANCE.NAME,
200 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
201 }
202 } as HttpNodeinfoDiasporaSoftwareNsSchema20
203 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
204 } else {
205 json = { error: 'Nodeinfo schema version not handled' }
206 res.status(404)
207 }
208
209 return res.send(json).end()
210}
211
212async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
213 const { video, videoFile } = getVideoAndFile(req, res)
214 if (!videoFile) return res.status(404).end()
215
216 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
217}
218
219async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
220 const { video, videoFile } = getVideoAndFile(req, res)
221 if (!videoFile) return res.status(404).end()
222
223 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
224}
225
226function getVideoAndFile (req: express.Request, res: express.Response) {
227 const resolution = parseInt(req.params.resolution, 10)
228 const video: VideoModel = res.locals.video
229
230 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
231
232 return { video, videoFile }
233}