]>
Commit | Line | Data |
---|---|---|
1 | import * as cors from 'cors' | |
2 | import * as express from 'express' | |
3 | import { | |
4 | HLS_STREAMING_PLAYLIST_DIRECTORY, | |
5 | PEERTUBE_VERSION, | |
6 | ROUTE_CACHE_LIFETIME, | |
7 | STATIC_DOWNLOAD_PATHS, | |
8 | STATIC_MAX_AGE, | |
9 | STATIC_PATHS, | |
10 | WEBSERVER | |
11 | } from '../initializers/constants' | |
12 | import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' | |
13 | import { cacheRoute } from '../middlewares/cache' | |
14 | import { asyncMiddleware, videosGetValidator } from '../middlewares' | |
15 | import { VideoModel } from '../models/video/video' | |
16 | import { UserModel } from '../models/account/user' | |
17 | import { VideoCommentModel } from '../models/video/video-comment' | |
18 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' | |
19 | import { join } from 'path' | |
20 | import { root } from '../helpers/core-utils' | |
21 | import { CONFIG } from '../initializers/config' | |
22 | ||
23 | const staticRouter = express.Router() | |
24 | ||
25 | staticRouter.use(cors()) | |
26 | ||
27 | /* | |
28 | Cors is very important to let other servers access torrent and video files | |
29 | */ | |
30 | ||
31 | const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR | |
32 | staticRouter.use( | |
33 | STATIC_PATHS.TORRENTS, | |
34 | cors(), | |
35 | express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file | |
36 | ) | |
37 | staticRouter.use( | |
38 | STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent', | |
39 | asyncMiddleware(videosGetValidator), | |
40 | asyncMiddleware(downloadTorrent) | |
41 | ) | |
42 | ||
43 | // Videos path for webseeding | |
44 | staticRouter.use( | |
45 | STATIC_PATHS.WEBSEED, | |
46 | cors(), | |
47 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video | |
48 | ) | |
49 | staticRouter.use( | |
50 | STATIC_PATHS.REDUNDANCY, | |
51 | cors(), | |
52 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video | |
53 | ) | |
54 | ||
55 | staticRouter.use( | |
56 | STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', | |
57 | asyncMiddleware(videosGetValidator), | |
58 | asyncMiddleware(downloadVideoFile) | |
59 | ) | |
60 | ||
61 | // HLS | |
62 | staticRouter.use( | |
63 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, | |
64 | cors(), | |
65 | express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist | |
66 | ) | |
67 | ||
68 | // Thumbnails path for express | |
69 | const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR | |
70 | staticRouter.use( | |
71 | STATIC_PATHS.THUMBNAILS, | |
72 | express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist | |
73 | ) | |
74 | ||
75 | const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR | |
76 | staticRouter.use( | |
77 | STATIC_PATHS.AVATARS, | |
78 | express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist | |
79 | ) | |
80 | ||
81 | // We don't have video previews, fetch them from the origin instance | |
82 | staticRouter.use( | |
83 | STATIC_PATHS.PREVIEWS + ':uuid.jpg', | |
84 | asyncMiddleware(getPreview) | |
85 | ) | |
86 | ||
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 | ||
93 | // robots.txt service | |
94 | staticRouter.get('/robots.txt', | |
95 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)), | |
96 | (_, res: express.Response) => { | |
97 | res.type('text/plain') | |
98 | return res.send(CONFIG.INSTANCE.ROBOTS) | |
99 | } | |
100 | ) | |
101 | ||
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 | ||
117 | // nodeinfo service | |
118 | staticRouter.use('/.well-known/nodeinfo', | |
119 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)), | |
120 | (_, res: express.Response) => { | |
121 | return res.json({ | |
122 | links: [ | |
123 | { | |
124 | rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', | |
125 | href: WEBSERVER.URL + '/nodeinfo/2.0.json' | |
126 | } | |
127 | ] | |
128 | }) | |
129 | } | |
130 | ) | |
131 | staticRouter.use('/nodeinfo/:version.json', | |
132 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)), | |
133 | asyncMiddleware(generateNodeinfo) | |
134 | ) | |
135 | ||
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') | |
141 | ||
142 | return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt')) | |
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' }) | |
150 | } | |
151 | ) | |
152 | ||
153 | staticRouter.use('/.well-known/change-password', | |
154 | (_, res: express.Response) => { | |
155 | res.redirect('/my-account/settings') | |
156 | } | |
157 | ) | |
158 | ||
159 | staticRouter.use('/.well-known/host-meta', | |
160 | (_, res: express.Response) => { | |
161 | res.type('application/xml') | |
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 | ||
172 | // --------------------------------------------------------------------------- | |
173 | ||
174 | export { | |
175 | staticRouter | |
176 | } | |
177 | ||
178 | // --------------------------------------------------------------------------- | |
179 | ||
180 | async function getPreview (req: express.Request, res: express.Response) { | |
181 | const result = await VideosPreviewCache.Instance.getFilePath(req.params.uuid) | |
182 | if (!result) return res.sendStatus(404) | |
183 | ||
184 | return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE }) | |
185 | } | |
186 | ||
187 | async function getVideoCaption (req: express.Request, res: express.Response) { | |
188 | const result = await VideosCaptionCache.Instance.getFilePath({ | |
189 | videoId: req.params.videoId, | |
190 | language: req.params.captionLanguage | |
191 | }) | |
192 | if (!result) return res.sendStatus(404) | |
193 | ||
194 | return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE }) | |
195 | } | |
196 | ||
197 | async function generateNodeinfo (req: express.Request, res: express.Response) { | |
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', | |
208 | version: PEERTUBE_VERSION | |
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 | |
236 | res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"') | |
237 | } else { | |
238 | json = { error: 'Nodeinfo schema version not handled' } | |
239 | res.status(404) | |
240 | } | |
241 | ||
242 | return res.send(json).end() | |
243 | } | |
244 | ||
245 | async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) { | |
246 | const { video, videoFile } = getVideoAndFile(req, res) | |
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) { | |
253 | const { video, videoFile } = getVideoAndFile(req, res) | |
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 | ||
259 | function getVideoAndFile (req: express.Request, res: express.Response) { | |
260 | const resolution = parseInt(req.params.resolution, 10) | |
261 | const video = res.locals.video | |
262 | ||
263 | const videoFile = video.VideoFiles.find(f => f.resolution === resolution) | |
264 | ||
265 | return { video, videoFile } | |
266 | } |