]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/static.ts
Add plugin translation system
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
CommitLineData
4d4e5cd4 1import * as cors from 'cors'
50d6de9c 2import * as express from 'express'
9c6ca37f 3import {
66170ca8 4 HLS_STREAMING_PLAYLIST_DIRECTORY, PEERTUBE_VERSION,
9c6ca37f
C
5 ROUTE_CACHE_LIFETIME,
6 STATIC_DOWNLOAD_PATHS,
7 STATIC_MAX_AGE,
6dd9de95
C
8 STATIC_PATHS,
9 WEBSERVER
74dc3bca 10} from '../initializers/constants'
d74d29ad 11import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
98d3324d 12import { cacheRoute } from '../middlewares/cache'
02756fbd
C
13import { asyncMiddleware, videosGetValidator } from '../middlewares'
14import { VideoModel } from '../models/video/video'
3f6d68d9
RK
15import { UserModel } from '../models/account/user'
16import { VideoCommentModel } from '../models/video/video-comment'
c75937d0 17import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
aac0118d
C
18import { join } from 'path'
19import { root } from '../helpers/core-utils'
6dd9de95 20import { CONFIG } from '../initializers/config'
65fcc311
C
21
22const staticRouter = express.Router()
23
62945f06
C
24staticRouter.use(cors())
25
65fcc311 26/*
60862425 27 Cors is very important to let other servers access torrent and video files
65fcc311
C
28*/
29
30const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
31staticRouter.use(
32 STATIC_PATHS.TORRENTS,
33 cors(),
49379960 34 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
65fcc311 35)
02756fbd
C
36staticRouter.use(
37 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
38 asyncMiddleware(videosGetValidator),
39 asyncMiddleware(downloadTorrent)
40)
65fcc311
C
41
42// Videos path for webseeding
65fcc311
C
43staticRouter.use(
44 STATIC_PATHS.WEBSEED,
45 cors(),
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,
6040f87d 50 cors(),
b9fffa29 51 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
6040f87d
C
52)
53
02756fbd
C
54staticRouter.use(
55 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
56 asyncMiddleware(videosGetValidator),
57 asyncMiddleware(downloadVideoFile)
58)
65fcc311 59
09209296
C
60// HLS
61staticRouter.use(
9c6ca37f 62 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
09209296 63 cors(),
9c6ca37f 64 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
09209296
C
65)
66
65fcc311
C
67// Thumbnails path for express
68const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
69staticRouter.use(
70 STATIC_PATHS.THUMBNAILS,
a8bf1d82 71 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
65fcc311
C
72)
73
c5911fd3
C
74const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
75staticRouter.use(
76 STATIC_PATHS.AVATARS,
a8bf1d82 77 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
c5911fd3
C
78)
79
40e87e9e 80// We don't have video previews, fetch them from the origin instance
65fcc311 81staticRouter.use(
f981dae8 82 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
eb080476 83 asyncMiddleware(getPreview)
65fcc311
C
84)
85
40e87e9e
C
86// We don't have video captions, fetch them from the origin instance
87staticRouter.use(
88 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
89 asyncMiddleware(getVideoCaption)
90)
91
ac235c37 92// robots.txt service
3f6d68d9 93staticRouter.get('/robots.txt',
98d3324d 94 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
3f6d68d9
RK
95 (_, res: express.Response) => {
96 res.type('text/plain')
97 return res.send(CONFIG.INSTANCE.ROBOTS)
98 }
99)
100
5447516b
AH
101// security.txt service
102staticRouter.get('/security.txt',
103 (_, res: express.Response) => {
104 return res.redirect(301, '/.well-known/security.txt')
105 }
106)
107
108staticRouter.get('/.well-known/security.txt',
109 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
110 (_, res: express.Response) => {
111 res.type('text/plain')
112 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
113 }
114)
115
3f6d68d9
RK
116// nodeinfo service
117staticRouter.use('/.well-known/nodeinfo',
98d3324d 118 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
3f6d68d9
RK
119 (_, res: express.Response) => {
120 return res.json({
121 links: [
122 {
123 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
6dd9de95 124 href: WEBSERVER.URL + '/nodeinfo/2.0.json'
3f6d68d9
RK
125 }
126 ]
127 })
128 }
129)
130staticRouter.use('/nodeinfo/:version.json',
aad0ec24 131 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
3f6d68d9
RK
132 asyncMiddleware(generateNodeinfo)
133)
ac235c37 134
aad0ec24
RK
135// dnt-policy.txt service (see https://www.eff.org/dnt-policy)
136staticRouter.use('/.well-known/dnt-policy.txt',
137 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
138 (_, res: express.Response) => {
139 res.type('text/plain')
aac0118d 140
d1105b97 141 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
aad0ec24
RK
142 }
143)
144
145// dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
146staticRouter.use('/.well-known/dnt/',
147 (_, res: express.Response) => {
148 res.json({ tracking: 'N' })
31414127
RK
149 }
150)
151
152staticRouter.use('/.well-known/change-password',
153 (_, res: express.Response) => {
154 res.redirect('/my-account/settings')
aad0ec24
RK
155 }
156)
157
3ddb1ec5
C
158staticRouter.use('/.well-known/host-meta',
159 (_, res: express.Response) => {
03371ad9 160 res.type('application/xml')
3ddb1ec5
C
161
162 const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
163 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
164 ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
165 '</XRD>'
166
167 res.send(xml).end()
168 }
169)
170
65fcc311
C
171// ---------------------------------------------------------------------------
172
173export {
174 staticRouter
175}
f981dae8
C
176
177// ---------------------------------------------------------------------------
178
e8bafea3 179async function getPreview (req: express.Request, res: express.Response) {
3acc5084
C
180 const result = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
181 if (!result) return res.sendStatus(404)
40e87e9e 182
3acc5084 183 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE })
40e87e9e
C
184}
185
186async function getVideoCaption (req: express.Request, res: express.Response) {
3acc5084 187 const result = await VideosCaptionCache.Instance.getFilePath({
40e87e9e
C
188 videoId: req.params.videoId,
189 language: req.params.captionLanguage
190 })
3acc5084 191 if (!result) return res.sendStatus(404)
f981dae8 192
3acc5084 193 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE })
f981dae8 194}
02756fbd 195
536598cf 196async function generateNodeinfo (req: express.Request, res: express.Response) {
3f6d68d9
RK
197 const { totalVideos } = await VideoModel.getStats()
198 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
199 const { totalUsers } = await UserModel.getStats()
200 let json = {}
201
202 if (req.params.version && (req.params.version === '2.0')) {
203 json = {
204 version: '2.0',
205 software: {
206 name: 'peertube',
66170ca8 207 version: PEERTUBE_VERSION
3f6d68d9
RK
208 },
209 protocols: [
210 'activitypub'
211 ],
212 services: {
213 inbound: [],
214 outbound: [
215 'atom1.0',
216 'rss2.0'
217 ]
218 },
219 openRegistrations: CONFIG.SIGNUP.ENABLED,
220 usage: {
221 users: {
222 total: totalUsers
223 },
224 localPosts: totalVideos,
225 localComments: totalLocalVideoComments
226 },
227 metadata: {
228 taxonomy: {
229 postsName: 'Videos'
230 },
231 nodeName: CONFIG.INSTANCE.NAME,
232 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
233 }
234 } as HttpNodeinfoDiasporaSoftwareNsSchema20
98d3324d 235 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
3f6d68d9
RK
236 } else {
237 json = { error: 'Nodeinfo schema version not handled' }
238 res.status(404)
239 }
240
98d3324d 241 return res.send(json).end()
3f6d68d9
RK
242}
243
02756fbd 244async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 245 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
246 if (!videoFile) return res.status(404).end()
247
248 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
249}
250
251async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 252 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
253 if (!videoFile) return res.status(404).end()
254
255 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
256}
257
9118bca3 258function getVideoAndFile (req: express.Request, res: express.Response) {
02756fbd 259 const resolution = parseInt(req.params.resolution, 10)
dae86118 260 const video = res.locals.video
02756fbd
C
261
262 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
263
264 return { video, videoFile }
265}