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