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