]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/static.ts
draft "security.txt" spec integration (#1020)
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
CommitLineData
4d4e5cd4 1import * as cors from 'cors'
c9d5c64f 2import { createReadStream } from 'fs-extra'
50d6de9c 3import * as express from 'express'
c75937d0 4import { CONFIG, ROUTE_CACHE_LIFETIME, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
50d6de9c 5import { VideosPreviewCache } from '../lib/cache'
98d3324d 6import { cacheRoute } from '../middlewares/cache'
02756fbd
C
7import { asyncMiddleware, videosGetValidator } from '../middlewares'
8import { VideoModel } from '../models/video/video'
40e87e9e 9import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
3f6d68d9
RK
10import { UserModel } from '../models/account/user'
11import { VideoCommentModel } from '../models/video/video-comment'
c75937d0 12import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
65fcc311 13
3f6d68d9 14const packageJSON = require('../../../package.json')
65fcc311
C
15const staticRouter = express.Router()
16
62945f06
C
17staticRouter.use(cors())
18
65fcc311 19/*
60862425 20 Cors is very important to let other servers access torrent and video files
65fcc311
C
21*/
22
23const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
24staticRouter.use(
25 STATIC_PATHS.TORRENTS,
26 cors(),
49379960 27 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
65fcc311 28)
02756fbd
C
29staticRouter.use(
30 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
31 asyncMiddleware(videosGetValidator),
32 asyncMiddleware(downloadTorrent)
33)
65fcc311
C
34
35// Videos path for webseeding
36const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
37staticRouter.use(
38 STATIC_PATHS.WEBSEED,
39 cors(),
57a81ff6 40 express.static(videosPhysicalPath)
65fcc311 41)
02756fbd
C
42staticRouter.use(
43 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
44 asyncMiddleware(videosGetValidator),
45 asyncMiddleware(downloadVideoFile)
46)
65fcc311
C
47
48// Thumbnails path for express
49const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
50staticRouter.use(
51 STATIC_PATHS.THUMBNAILS,
a8bf1d82 52 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
65fcc311
C
53)
54
c5911fd3
C
55const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
56staticRouter.use(
57 STATIC_PATHS.AVATARS,
a8bf1d82 58 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
c5911fd3
C
59)
60
40e87e9e 61// We don't have video previews, fetch them from the origin instance
65fcc311 62staticRouter.use(
f981dae8 63 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
eb080476 64 asyncMiddleware(getPreview)
65fcc311
C
65)
66
40e87e9e
C
67// We don't have video captions, fetch them from the origin instance
68staticRouter.use(
69 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
70 asyncMiddleware(getVideoCaption)
71)
72
ac235c37 73// robots.txt service
3f6d68d9 74staticRouter.get('/robots.txt',
98d3324d 75 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
3f6d68d9
RK
76 (_, res: express.Response) => {
77 res.type('text/plain')
78 return res.send(CONFIG.INSTANCE.ROBOTS)
79 }
80)
81
5447516b
AH
82// security.txt service
83staticRouter.get('/security.txt',
84 (_, res: express.Response) => {
85 return res.redirect(301, '/.well-known/security.txt')
86 }
87)
88
89staticRouter.get('/.well-known/security.txt',
90 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
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',
98d3324d 99 asyncMiddleware(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',
105 href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json'
106 }
107 ]
108 })
109 }
110)
111staticRouter.use('/nodeinfo/:version.json',
aad0ec24 112 asyncMiddleware(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',
118 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
119 (_, res: express.Response) => {
120 res.type('text/plain')
121 createReadStream('./server/static/dnt-policy/dnt-policy-1.0.txt').pipe(res)
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' })
129 }
130)
131
65fcc311
C
132// ---------------------------------------------------------------------------
133
134export {
135 staticRouter
136}
f981dae8
C
137
138// ---------------------------------------------------------------------------
139
eb080476 140async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
40e87e9e
C
141 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
142 if (!path) return res.sendStatus(404)
143
144 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
145}
146
147async function getVideoCaption (req: express.Request, res: express.Response) {
148 const path = await VideosCaptionCache.Instance.getFilePath({
149 videoId: req.params.videoId,
150 language: req.params.captionLanguage
151 })
eb080476 152 if (!path) return res.sendStatus(404)
f981dae8 153
eb080476 154 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
f981dae8 155}
02756fbd 156
3f6d68d9
RK
157async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) {
158 const { totalVideos } = await VideoModel.getStats()
159 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
160 const { totalUsers } = await UserModel.getStats()
161 let json = {}
162
163 if (req.params.version && (req.params.version === '2.0')) {
164 json = {
165 version: '2.0',
166 software: {
167 name: 'peertube',
168 version: packageJSON.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 },
185 localPosts: totalVideos,
186 localComments: totalLocalVideoComments
187 },
188 metadata: {
189 taxonomy: {
190 postsName: 'Videos'
191 },
192 nodeName: CONFIG.INSTANCE.NAME,
193 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
194 }
195 } as HttpNodeinfoDiasporaSoftwareNsSchema20
98d3324d 196 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
3f6d68d9
RK
197 } else {
198 json = { error: 'Nodeinfo schema version not handled' }
199 res.status(404)
200 }
201
98d3324d 202 return res.send(json).end()
3f6d68d9
RK
203}
204
02756fbd 205async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 206 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
207 if (!videoFile) return res.status(404).end()
208
209 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
210}
211
212async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 213 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
214 if (!videoFile) return res.status(404).end()
215
216 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
217}
218
9118bca3 219function getVideoAndFile (req: express.Request, res: express.Response) {
02756fbd
C
220 const resolution = parseInt(req.params.resolution, 10)
221 const video: VideoModel = res.locals.video
222
223 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
224
225 return { video, videoFile }
226}