]>
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 { cacheRoute } from '../middlewares/cache' | |
13 | import { asyncMiddleware, videosGetValidator } from '../middlewares' | |
14 | import { VideoModel } from '../models/video/video' | |
15 | import { UserModel } from '../models/account/user' | |
16 | import { VideoCommentModel } from '../models/video/video-comment' | |
17 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' | |
18 | import { join } from 'path' | |
19 | import { root } from '../helpers/core-utils' | |
20 | import { CONFIG } from '../initializers/config' | |
21 | import { getPreview, getVideoCaption } from './lazy-static' | |
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 | // DEPRECATED: use lazy-static route instead | |
76 | const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR | |
77 | staticRouter.use( | |
78 | STATIC_PATHS.AVATARS, | |
79 | express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist | |
80 | ) | |
81 | ||
82 | // DEPRECATED: use lazy-static route instead | |
83 | staticRouter.use( | |
84 | STATIC_PATHS.PREVIEWS + ':uuid.jpg', | |
85 | asyncMiddleware(getPreview) | |
86 | ) | |
87 | ||
88 | // DEPRECATED: use lazy-static route instead | |
89 | staticRouter.use( | |
90 | STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt', | |
91 | asyncMiddleware(getVideoCaption) | |
92 | ) | |
93 | ||
94 | // robots.txt service | |
95 | staticRouter.get('/robots.txt', | |
96 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)), | |
97 | (_, res: express.Response) => { | |
98 | res.type('text/plain') | |
99 | return res.send(CONFIG.INSTANCE.ROBOTS) | |
100 | } | |
101 | ) | |
102 | ||
103 | // security.txt service | |
104 | staticRouter.get('/security.txt', | |
105 | (_, res: express.Response) => { | |
106 | return res.redirect(301, '/.well-known/security.txt') | |
107 | } | |
108 | ) | |
109 | ||
110 | staticRouter.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 | ||
118 | // nodeinfo service | |
119 | staticRouter.use('/.well-known/nodeinfo', | |
120 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)), | |
121 | (_, res: express.Response) => { | |
122 | return res.json({ | |
123 | links: [ | |
124 | { | |
125 | rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', | |
126 | href: WEBSERVER.URL + '/nodeinfo/2.0.json' | |
127 | } | |
128 | ] | |
129 | }) | |
130 | } | |
131 | ) | |
132 | staticRouter.use('/nodeinfo/:version.json', | |
133 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)), | |
134 | asyncMiddleware(generateNodeinfo) | |
135 | ) | |
136 | ||
137 | // dnt-policy.txt service (see https://www.eff.org/dnt-policy) | |
138 | staticRouter.use('/.well-known/dnt-policy.txt', | |
139 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)), | |
140 | (_, res: express.Response) => { | |
141 | res.type('text/plain') | |
142 | ||
143 | return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt')) | |
144 | } | |
145 | ) | |
146 | ||
147 | // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource) | |
148 | staticRouter.use('/.well-known/dnt/', | |
149 | (_, res: express.Response) => { | |
150 | res.json({ tracking: 'N' }) | |
151 | } | |
152 | ) | |
153 | ||
154 | staticRouter.use('/.well-known/change-password', | |
155 | (_, res: express.Response) => { | |
156 | res.redirect('/my-account/settings') | |
157 | } | |
158 | ) | |
159 | ||
160 | staticRouter.use('/.well-known/host-meta', | |
161 | (_, res: express.Response) => { | |
162 | res.type('application/xml') | |
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 | ||
173 | // --------------------------------------------------------------------------- | |
174 | ||
175 | export { | |
176 | staticRouter | |
177 | } | |
178 | ||
179 | // --------------------------------------------------------------------------- | |
180 | ||
181 | async function generateNodeinfo (req: express.Request, res: express.Response) { | |
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', | |
192 | version: PEERTUBE_VERSION | |
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 | |
220 | res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"') | |
221 | } else { | |
222 | json = { error: 'Nodeinfo schema version not handled' } | |
223 | res.status(404) | |
224 | } | |
225 | ||
226 | return res.send(json).end() | |
227 | } | |
228 | ||
229 | async function downloadTorrent (req: express.Request, res: express.Response) { | |
230 | const { video, videoFile } = getVideoAndFile(req, res) | |
231 | if (!videoFile) return res.status(404).end() | |
232 | ||
233 | return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`) | |
234 | } | |
235 | ||
236 | async function downloadVideoFile (req: express.Request, res: express.Response) { | |
237 | const { video, videoFile } = getVideoAndFile(req, res) | |
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 | ||
243 | function getVideoAndFile (req: express.Request, res: express.Response) { | |
244 | const resolution = parseInt(req.params.resolution, 10) | |
245 | const video = res.locals.videoAll | |
246 | ||
247 | const videoFile = video.VideoFiles.find(f => f.resolution === resolution) | |
248 | ||
249 | return { video, videoFile } | |
250 | } |