]>
Commit | Line | Data |
---|---|---|
1 | import * as cors from 'cors' | |
2 | import * as express from 'express' | |
3 | import { | |
4 | CONSTRAINTS_FIELDS, | |
5 | DEFAULT_THEME_NAME, | |
6 | HLS_STREAMING_PLAYLIST_DIRECTORY, | |
7 | PEERTUBE_VERSION, | |
8 | ROUTE_CACHE_LIFETIME, | |
9 | STATIC_DOWNLOAD_PATHS, | |
10 | STATIC_MAX_AGE, | |
11 | STATIC_PATHS, | |
12 | WEBSERVER | |
13 | } from '../initializers/constants' | |
14 | import { cacheRoute } from '../middlewares/cache' | |
15 | import { asyncMiddleware, videosDownloadValidator } from '../middlewares' | |
16 | import { VideoModel } from '../models/video/video' | |
17 | import { UserModel } from '../models/account/user' | |
18 | import { VideoCommentModel } from '../models/video/video-comment' | |
19 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' | |
20 | import { join } from 'path' | |
21 | import { root } from '../helpers/core-utils' | |
22 | import { CONFIG, isEmailEnabled } from '../initializers/config' | |
23 | import { getPreview, getVideoCaption } from './lazy-static' | |
24 | import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type' | |
25 | import { MVideoFile, MVideoFullLight } from '@server/types/models' | |
26 | import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths' | |
27 | import { getThemeOrDefault } from '../lib/plugins/theme-utils' | |
28 | import { getEnabledResolutions, getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config' | |
29 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | |
30 | import { serveIndexHTML } from '@server/lib/client-html' | |
31 | ||
32 | const staticRouter = express.Router() | |
33 | ||
34 | staticRouter.use(cors()) | |
35 | ||
36 | /* | |
37 | Cors is very important to let other servers access torrent and video files | |
38 | */ | |
39 | ||
40 | const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR | |
41 | staticRouter.use( | |
42 | STATIC_PATHS.TORRENTS, | |
43 | cors(), | |
44 | express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file | |
45 | ) | |
46 | staticRouter.use( | |
47 | STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent', | |
48 | asyncMiddleware(videosDownloadValidator), | |
49 | downloadTorrent | |
50 | ) | |
51 | staticRouter.use( | |
52 | STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+)-hls.torrent', | |
53 | asyncMiddleware(videosDownloadValidator), | |
54 | downloadHLSVideoFileTorrent | |
55 | ) | |
56 | ||
57 | // Videos path for webseeding | |
58 | staticRouter.use( | |
59 | STATIC_PATHS.WEBSEED, | |
60 | cors(), | |
61 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video | |
62 | ) | |
63 | staticRouter.use( | |
64 | STATIC_PATHS.REDUNDANCY, | |
65 | cors(), | |
66 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video | |
67 | ) | |
68 | ||
69 | staticRouter.use( | |
70 | STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', | |
71 | asyncMiddleware(videosDownloadValidator), | |
72 | downloadVideoFile | |
73 | ) | |
74 | ||
75 | staticRouter.use( | |
76 | STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', | |
77 | asyncMiddleware(videosDownloadValidator), | |
78 | downloadHLSVideoFile | |
79 | ) | |
80 | ||
81 | // HLS | |
82 | staticRouter.use( | |
83 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, | |
84 | cors(), | |
85 | express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist | |
86 | ) | |
87 | ||
88 | // Thumbnails path for express | |
89 | const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR | |
90 | staticRouter.use( | |
91 | STATIC_PATHS.THUMBNAILS, | |
92 | express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist | |
93 | ) | |
94 | ||
95 | // DEPRECATED: use lazy-static route instead | |
96 | const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR | |
97 | staticRouter.use( | |
98 | STATIC_PATHS.AVATARS, | |
99 | express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist | |
100 | ) | |
101 | ||
102 | // DEPRECATED: use lazy-static route instead | |
103 | staticRouter.use( | |
104 | STATIC_PATHS.PREVIEWS + ':uuid.jpg', | |
105 | asyncMiddleware(getPreview) | |
106 | ) | |
107 | ||
108 | // DEPRECATED: use lazy-static route instead | |
109 | staticRouter.use( | |
110 | STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt', | |
111 | asyncMiddleware(getVideoCaption) | |
112 | ) | |
113 | ||
114 | // robots.txt service | |
115 | staticRouter.get('/robots.txt', | |
116 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ROBOTS)), | |
117 | (_, res: express.Response) => { | |
118 | res.type('text/plain') | |
119 | return res.send(CONFIG.INSTANCE.ROBOTS) | |
120 | } | |
121 | ) | |
122 | ||
123 | staticRouter.all('/teapot', | |
124 | getCup, | |
125 | asyncMiddleware(serveIndexHTML) | |
126 | ) | |
127 | ||
128 | // security.txt service | |
129 | staticRouter.get('/security.txt', | |
130 | (_, res: express.Response) => { | |
131 | return res.redirect(HttpStatusCode.MOVED_PERMANENTLY_301, '/.well-known/security.txt') | |
132 | } | |
133 | ) | |
134 | ||
135 | staticRouter.get('/.well-known/security.txt', | |
136 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.SECURITYTXT)), | |
137 | (_, res: express.Response) => { | |
138 | res.type('text/plain') | |
139 | return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT) | |
140 | } | |
141 | ) | |
142 | ||
143 | // nodeinfo service | |
144 | staticRouter.use('/.well-known/nodeinfo', | |
145 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)), | |
146 | (_, res: express.Response) => { | |
147 | return res.json({ | |
148 | links: [ | |
149 | { | |
150 | rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', | |
151 | href: WEBSERVER.URL + '/nodeinfo/2.0.json' | |
152 | } | |
153 | ] | |
154 | }) | |
155 | } | |
156 | ) | |
157 | staticRouter.use('/nodeinfo/:version.json', | |
158 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)), | |
159 | asyncMiddleware(generateNodeinfo) | |
160 | ) | |
161 | ||
162 | // dnt-policy.txt service (see https://www.eff.org/dnt-policy) | |
163 | staticRouter.use('/.well-known/dnt-policy.txt', | |
164 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.DNT_POLICY)), | |
165 | (_, res: express.Response) => { | |
166 | res.type('text/plain') | |
167 | ||
168 | return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt')) | |
169 | } | |
170 | ) | |
171 | ||
172 | // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource) | |
173 | staticRouter.use('/.well-known/dnt/', | |
174 | (_, res: express.Response) => { | |
175 | res.json({ tracking: 'N' }) | |
176 | } | |
177 | ) | |
178 | ||
179 | staticRouter.use('/.well-known/change-password', | |
180 | (_, res: express.Response) => { | |
181 | res.redirect('/my-account/settings') | |
182 | } | |
183 | ) | |
184 | ||
185 | staticRouter.use('/.well-known/host-meta', | |
186 | (_, res: express.Response) => { | |
187 | res.type('application/xml') | |
188 | ||
189 | const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' + | |
190 | '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' + | |
191 | ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` + | |
192 | '</XRD>' | |
193 | ||
194 | res.send(xml).end() | |
195 | } | |
196 | ) | |
197 | ||
198 | // --------------------------------------------------------------------------- | |
199 | ||
200 | export { | |
201 | staticRouter | |
202 | } | |
203 | ||
204 | // --------------------------------------------------------------------------- | |
205 | ||
206 | async function generateNodeinfo (req: express.Request, res: express.Response) { | |
207 | const { totalVideos } = await VideoModel.getStats() | |
208 | const { totalLocalVideoComments } = await VideoCommentModel.getStats() | |
209 | const { totalUsers } = await UserModel.getStats() | |
210 | let json = {} | |
211 | ||
212 | if (req.params.version && (req.params.version === '2.0')) { | |
213 | json = { | |
214 | version: '2.0', | |
215 | software: { | |
216 | name: 'peertube', | |
217 | version: PEERTUBE_VERSION | |
218 | }, | |
219 | protocols: [ | |
220 | 'activitypub' | |
221 | ], | |
222 | services: { | |
223 | inbound: [], | |
224 | outbound: [ | |
225 | 'atom1.0', | |
226 | 'rss2.0' | |
227 | ] | |
228 | }, | |
229 | openRegistrations: CONFIG.SIGNUP.ENABLED, | |
230 | usage: { | |
231 | users: { | |
232 | total: totalUsers | |
233 | }, | |
234 | localPosts: totalVideos, | |
235 | localComments: totalLocalVideoComments | |
236 | }, | |
237 | metadata: { | |
238 | taxonomy: { | |
239 | postsName: 'Videos' | |
240 | }, | |
241 | nodeName: CONFIG.INSTANCE.NAME, | |
242 | nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, | |
243 | nodeConfig: { | |
244 | search: { | |
245 | remoteUri: { | |
246 | users: CONFIG.SEARCH.REMOTE_URI.USERS, | |
247 | anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS | |
248 | } | |
249 | }, | |
250 | plugin: { | |
251 | registered: getRegisteredPlugins() | |
252 | }, | |
253 | theme: { | |
254 | registered: getRegisteredThemes(), | |
255 | default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) | |
256 | }, | |
257 | email: { | |
258 | enabled: isEmailEnabled() | |
259 | }, | |
260 | contactForm: { | |
261 | enabled: CONFIG.CONTACT_FORM.ENABLED | |
262 | }, | |
263 | transcoding: { | |
264 | hls: { | |
265 | enabled: CONFIG.TRANSCODING.HLS.ENABLED | |
266 | }, | |
267 | webtorrent: { | |
268 | enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED | |
269 | }, | |
270 | enabledResolutions: getEnabledResolutions('vod') | |
271 | }, | |
272 | live: { | |
273 | enabled: CONFIG.LIVE.ENABLED, | |
274 | transcoding: { | |
275 | enabled: CONFIG.LIVE.TRANSCODING.ENABLED, | |
276 | enabledResolutions: getEnabledResolutions('live') | |
277 | } | |
278 | }, | |
279 | import: { | |
280 | videos: { | |
281 | http: { | |
282 | enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED | |
283 | }, | |
284 | torrent: { | |
285 | enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED | |
286 | } | |
287 | } | |
288 | }, | |
289 | autoBlacklist: { | |
290 | videos: { | |
291 | ofUsers: { | |
292 | enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED | |
293 | } | |
294 | } | |
295 | }, | |
296 | avatar: { | |
297 | file: { | |
298 | size: { | |
299 | max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max | |
300 | }, | |
301 | extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME | |
302 | } | |
303 | }, | |
304 | video: { | |
305 | image: { | |
306 | extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME, | |
307 | size: { | |
308 | max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max | |
309 | } | |
310 | }, | |
311 | file: { | |
312 | extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME | |
313 | } | |
314 | }, | |
315 | videoCaption: { | |
316 | file: { | |
317 | size: { | |
318 | max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max | |
319 | }, | |
320 | extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME | |
321 | } | |
322 | }, | |
323 | user: { | |
324 | videoQuota: CONFIG.USER.VIDEO_QUOTA, | |
325 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY | |
326 | }, | |
327 | trending: { | |
328 | videos: { | |
329 | intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS | |
330 | } | |
331 | }, | |
332 | tracker: { | |
333 | enabled: CONFIG.TRACKER.ENABLED | |
334 | } | |
335 | } | |
336 | } | |
337 | } as HttpNodeinfoDiasporaSoftwareNsSchema20 | |
338 | res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"') | |
339 | } else { | |
340 | json = { error: 'Nodeinfo schema version not handled' } | |
341 | res.status(HttpStatusCode.NOT_FOUND_404) | |
342 | } | |
343 | ||
344 | return res.send(json).end() | |
345 | } | |
346 | ||
347 | function downloadTorrent (req: express.Request, res: express.Response) { | |
348 | const video = res.locals.videoAll | |
349 | ||
350 | const videoFile = getVideoFile(req, video.VideoFiles) | |
351 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() | |
352 | ||
353 | return res.download(getTorrentFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p.torrent`) | |
354 | } | |
355 | ||
356 | function downloadHLSVideoFileTorrent (req: express.Request, res: express.Response) { | |
357 | const video = res.locals.videoAll | |
358 | ||
359 | const playlist = getHLSPlaylist(video) | |
360 | if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end | |
361 | ||
362 | const videoFile = getVideoFile(req, playlist.VideoFiles) | |
363 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() | |
364 | ||
365 | return res.download(getTorrentFilePath(playlist, videoFile), `${video.name}-${videoFile.resolution}p-hls.torrent`) | |
366 | } | |
367 | ||
368 | function downloadVideoFile (req: express.Request, res: express.Response) { | |
369 | const video = res.locals.videoAll | |
370 | ||
371 | const videoFile = getVideoFile(req, video.VideoFiles) | |
372 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() | |
373 | ||
374 | return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`) | |
375 | } | |
376 | ||
377 | function downloadHLSVideoFile (req: express.Request, res: express.Response) { | |
378 | const video = res.locals.videoAll | |
379 | const playlist = getHLSPlaylist(video) | |
380 | if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end | |
381 | ||
382 | const videoFile = getVideoFile(req, playlist.VideoFiles) | |
383 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() | |
384 | ||
385 | const filename = `${video.name}-${videoFile.resolution}p-${playlist.getStringType()}${videoFile.extname}` | |
386 | return res.download(getVideoFilePath(playlist, videoFile), filename) | |
387 | } | |
388 | ||
389 | function getVideoFile (req: express.Request, files: MVideoFile[]) { | |
390 | const resolution = parseInt(req.params.resolution, 10) | |
391 | return files.find(f => f.resolution === resolution) | |
392 | } | |
393 | ||
394 | function getHLSPlaylist (video: MVideoFullLight) { | |
395 | const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) | |
396 | if (!playlist) return undefined | |
397 | ||
398 | return Object.assign(playlist, { Video: video }) | |
399 | } | |
400 | ||
401 | function getCup (req: express.Request, res: express.Response, next: express.NextFunction) { | |
402 | res.status(HttpStatusCode.I_AM_A_TEAPOT_418) | |
403 | res.setHeader('Accept-Additions', 'Non-Dairy;1,Sugar;1') | |
404 | res.setHeader('Safe', 'if-sepia-awake') | |
405 | ||
406 | return next() | |
407 | } |