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