]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as cors from 'cors' |
50d6de9c | 2 | import * as express from 'express' |
9c6ca37f | 3 | import { |
4c1c1709 C |
4 | CONSTRAINTS_FIELDS, |
5 | DEFAULT_THEME_NAME, | |
34dd7cb4 C |
6 | HLS_STREAMING_PLAYLIST_DIRECTORY, |
7 | PEERTUBE_VERSION, | |
9c6ca37f C |
8 | ROUTE_CACHE_LIFETIME, |
9 | STATIC_DOWNLOAD_PATHS, | |
10 | STATIC_MAX_AGE, | |
6dd9de95 | 11 | STATIC_PATHS, |
4c1c1709 | 12 | WEBSERVER |
74dc3bca | 13 | } from '../initializers/constants' |
98d3324d | 14 | import { cacheRoute } from '../middlewares/cache' |
eccf70f0 | 15 | import { asyncMiddleware, videosDownloadValidator } from '../middlewares' |
02756fbd | 16 | import { VideoModel } from '../models/video/video' |
3f6d68d9 RK |
17 | import { UserModel } from '../models/account/user' |
18 | import { VideoCommentModel } from '../models/video/video-comment' | |
c75937d0 | 19 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' |
aac0118d C |
20 | import { join } from 'path' |
21 | import { root } from '../helpers/core-utils' | |
454c20fa | 22 | import { getEnabledResolutions } from '../lib/video-transcoding' |
4c1c1709 | 23 | import { CONFIG, isEmailEnabled } from '../initializers/config' |
557b13ae | 24 | import { getPreview, getVideoCaption } from './lazy-static' |
d7a25329 | 25 | import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type' |
26d6bf65 | 26 | import { MVideoFile, MVideoFullLight } from '@server/types/models' |
d7a25329 | 27 | import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths' |
174e0855 | 28 | import { getThemeOrDefault } from '../lib/plugins/theme-utils' |
454c20fa | 29 | import { getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config' |
2d53be02 | 30 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
f2eb23cd | 31 | import { serveIndexHTML } from '@server/lib/client-html' |
65fcc311 C |
32 | |
33 | const staticRouter = express.Router() | |
34 | ||
62945f06 C |
35 | staticRouter.use(cors()) |
36 | ||
65fcc311 | 37 | /* |
60862425 | 38 | Cors is very important to let other servers access torrent and video files |
65fcc311 C |
39 | */ |
40 | ||
41 | const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR | |
42 | staticRouter.use( | |
43 | STATIC_PATHS.TORRENTS, | |
44 | cors(), | |
49379960 | 45 | express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file |
65fcc311 | 46 | ) |
02756fbd C |
47 | staticRouter.use( |
48 | STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent', | |
eccf70f0 | 49 | asyncMiddleware(videosDownloadValidator), |
a1587156 | 50 | downloadTorrent |
02756fbd | 51 | ) |
d7a25329 C |
52 | staticRouter.use( |
53 | STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+)-hls.torrent', | |
eccf70f0 | 54 | asyncMiddleware(videosDownloadValidator), |
a1587156 | 55 | downloadHLSVideoFileTorrent |
d7a25329 | 56 | ) |
65fcc311 C |
57 | |
58 | // Videos path for webseeding | |
65fcc311 C |
59 | staticRouter.use( |
60 | STATIC_PATHS.WEBSEED, | |
61 | cors(), | |
b9fffa29 | 62 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video |
65fcc311 | 63 | ) |
6040f87d | 64 | staticRouter.use( |
b9fffa29 | 65 | STATIC_PATHS.REDUNDANCY, |
6040f87d | 66 | cors(), |
b9fffa29 | 67 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video |
6040f87d C |
68 | ) |
69 | ||
02756fbd C |
70 | staticRouter.use( |
71 | STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', | |
eccf70f0 | 72 | asyncMiddleware(videosDownloadValidator), |
a1587156 | 73 | downloadVideoFile |
02756fbd | 74 | ) |
65fcc311 | 75 | |
d7a25329 | 76 | staticRouter.use( |
efcd6f2e | 77 | STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension', |
eccf70f0 | 78 | asyncMiddleware(videosDownloadValidator), |
a1587156 | 79 | downloadHLSVideoFile |
d7a25329 C |
80 | ) |
81 | ||
09209296 C |
82 | // HLS |
83 | staticRouter.use( | |
9c6ca37f | 84 | STATIC_PATHS.STREAMING_PLAYLISTS.HLS, |
09209296 | 85 | cors(), |
9c6ca37f | 86 | express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist |
09209296 C |
87 | ) |
88 | ||
65fcc311 C |
89 | // Thumbnails path for express |
90 | const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR | |
91 | staticRouter.use( | |
92 | STATIC_PATHS.THUMBNAILS, | |
cd4cb177 | 93 | express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist |
65fcc311 C |
94 | ) |
95 | ||
557b13ae | 96 | // DEPRECATED: use lazy-static route instead |
c5911fd3 C |
97 | const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR |
98 | staticRouter.use( | |
99 | STATIC_PATHS.AVATARS, | |
cd4cb177 | 100 | express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist |
c5911fd3 C |
101 | ) |
102 | ||
557b13ae | 103 | // DEPRECATED: use lazy-static route instead |
65fcc311 | 104 | staticRouter.use( |
f981dae8 | 105 | STATIC_PATHS.PREVIEWS + ':uuid.jpg', |
eb080476 | 106 | asyncMiddleware(getPreview) |
65fcc311 C |
107 | ) |
108 | ||
557b13ae | 109 | // DEPRECATED: use lazy-static route instead |
40e87e9e C |
110 | staticRouter.use( |
111 | STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt', | |
112 | asyncMiddleware(getVideoCaption) | |
113 | ) | |
114 | ||
ac235c37 | 115 | // robots.txt service |
3f6d68d9 | 116 | staticRouter.get('/robots.txt', |
f2f0eda5 | 117 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ROBOTS)), |
3f6d68d9 RK |
118 | (_, res: express.Response) => { |
119 | res.type('text/plain') | |
120 | return res.send(CONFIG.INSTANCE.ROBOTS) | |
121 | } | |
122 | ) | |
123 | ||
f2eb23cd RK |
124 | staticRouter.all('/teapot', |
125 | getCup, | |
126 | asyncMiddleware(serveIndexHTML) | |
127 | ) | |
128 | ||
5447516b AH |
129 | // security.txt service |
130 | staticRouter.get('/security.txt', | |
131 | (_, res: express.Response) => { | |
2d53be02 | 132 | return res.redirect(HttpStatusCode.MOVED_PERMANENTLY_301, '/.well-known/security.txt') |
5447516b AH |
133 | } |
134 | ) | |
135 | ||
136 | staticRouter.get('/.well-known/security.txt', | |
f2f0eda5 | 137 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.SECURITYTXT)), |
5447516b AH |
138 | (_, res: express.Response) => { |
139 | res.type('text/plain') | |
140 | return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT) | |
141 | } | |
142 | ) | |
143 | ||
3f6d68d9 RK |
144 | // nodeinfo service |
145 | staticRouter.use('/.well-known/nodeinfo', | |
f2f0eda5 | 146 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)), |
3f6d68d9 RK |
147 | (_, res: express.Response) => { |
148 | return res.json({ | |
149 | links: [ | |
150 | { | |
151 | rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', | |
6dd9de95 | 152 | href: WEBSERVER.URL + '/nodeinfo/2.0.json' |
3f6d68d9 RK |
153 | } |
154 | ] | |
155 | }) | |
156 | } | |
157 | ) | |
158 | staticRouter.use('/nodeinfo/:version.json', | |
f2f0eda5 | 159 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)), |
3f6d68d9 RK |
160 | asyncMiddleware(generateNodeinfo) |
161 | ) | |
ac235c37 | 162 | |
aad0ec24 RK |
163 | // dnt-policy.txt service (see https://www.eff.org/dnt-policy) |
164 | staticRouter.use('/.well-known/dnt-policy.txt', | |
f2f0eda5 | 165 | asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.DNT_POLICY)), |
aad0ec24 RK |
166 | (_, res: express.Response) => { |
167 | res.type('text/plain') | |
aac0118d | 168 | |
d1105b97 | 169 | return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt')) |
aad0ec24 RK |
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' }) | |
31414127 RK |
177 | } |
178 | ) | |
179 | ||
180 | staticRouter.use('/.well-known/change-password', | |
181 | (_, res: express.Response) => { | |
182 | res.redirect('/my-account/settings') | |
aad0ec24 RK |
183 | } |
184 | ) | |
185 | ||
3ddb1ec5 C |
186 | staticRouter.use('/.well-known/host-meta', |
187 | (_, res: express.Response) => { | |
03371ad9 | 188 | res.type('application/xml') |
3ddb1ec5 C |
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 | ||
65fcc311 C |
199 | // --------------------------------------------------------------------------- |
200 | ||
201 | export { | |
202 | staticRouter | |
203 | } | |
f981dae8 C |
204 | |
205 | // --------------------------------------------------------------------------- | |
206 | ||
536598cf | 207 | async function generateNodeinfo (req: express.Request, res: express.Response) { |
3f6d68d9 RK |
208 | const { totalVideos } = await VideoModel.getStats() |
209 | const { totalLocalVideoComments } = await VideoCommentModel.getStats() | |
47d8e266 | 210 | const { totalUsers, totalMonthlyActiveUsers, totalHalfYearActiveUsers } = await UserModel.getStats() |
3f6d68d9 RK |
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', | |
66170ca8 | 218 | version: PEERTUBE_VERSION |
3f6d68d9 RK |
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: { | |
47d8e266 C |
233 | total: totalUsers, |
234 | activeMonth: totalMonthlyActiveUsers, | |
235 | activeHalfyear: totalHalfYearActiveUsers | |
3f6d68d9 RK |
236 | }, |
237 | localPosts: totalVideos, | |
238 | localComments: totalLocalVideoComments | |
239 | }, | |
240 | metadata: { | |
241 | taxonomy: { | |
242 | postsName: 'Videos' | |
243 | }, | |
244 | nodeName: CONFIG.INSTANCE.NAME, | |
174e0855 RK |
245 | nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION, |
246 | nodeConfig: { | |
9677fca7 RK |
247 | search: { |
248 | remoteUri: { | |
249 | users: CONFIG.SEARCH.REMOTE_URI.USERS, | |
250 | anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS | |
251 | } | |
252 | }, | |
174e0855 RK |
253 | plugin: { |
254 | registered: getRegisteredPlugins() | |
255 | }, | |
256 | theme: { | |
257 | registered: getRegisteredThemes(), | |
258 | default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME) | |
259 | }, | |
260 | email: { | |
4c1c1709 | 261 | enabled: isEmailEnabled() |
174e0855 RK |
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 | }, | |
c6c0fa6c C |
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 | } | |
174e0855 RK |
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 | } | |
3f6d68d9 RK |
339 | } |
340 | } as HttpNodeinfoDiasporaSoftwareNsSchema20 | |
98d3324d | 341 | res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"') |
3f6d68d9 RK |
342 | } else { |
343 | json = { error: 'Nodeinfo schema version not handled' } | |
2d53be02 | 344 | res.status(HttpStatusCode.NOT_FOUND_404) |
3f6d68d9 RK |
345 | } |
346 | ||
98d3324d | 347 | return res.send(json).end() |
3f6d68d9 RK |
348 | } |
349 | ||
a1587156 | 350 | function downloadTorrent (req: express.Request, res: express.Response) { |
d7a25329 C |
351 | const video = res.locals.videoAll |
352 | ||
353 | const videoFile = getVideoFile(req, video.VideoFiles) | |
2d53be02 | 354 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() |
d7a25329 C |
355 | |
356 | return res.download(getTorrentFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p.torrent`) | |
357 | } | |
358 | ||
a1587156 | 359 | function downloadHLSVideoFileTorrent (req: express.Request, res: express.Response) { |
d7a25329 C |
360 | const video = res.locals.videoAll |
361 | ||
362 | const playlist = getHLSPlaylist(video) | |
2d53be02 | 363 | if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end |
d7a25329 C |
364 | |
365 | const videoFile = getVideoFile(req, playlist.VideoFiles) | |
2d53be02 | 366 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() |
02756fbd | 367 | |
d7a25329 | 368 | return res.download(getTorrentFilePath(playlist, videoFile), `${video.name}-${videoFile.resolution}p-hls.torrent`) |
02756fbd C |
369 | } |
370 | ||
a1587156 | 371 | function downloadVideoFile (req: express.Request, res: express.Response) { |
d7a25329 C |
372 | const video = res.locals.videoAll |
373 | ||
374 | const videoFile = getVideoFile(req, video.VideoFiles) | |
2d53be02 | 375 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() |
02756fbd | 376 | |
d7a25329 | 377 | return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`) |
02756fbd C |
378 | } |
379 | ||
a1587156 | 380 | function downloadHLSVideoFile (req: express.Request, res: express.Response) { |
453e83ea | 381 | const video = res.locals.videoAll |
d7a25329 | 382 | const playlist = getHLSPlaylist(video) |
2d53be02 | 383 | if (!playlist) return res.status(HttpStatusCode.NOT_FOUND_404).end |
d7a25329 C |
384 | |
385 | const videoFile = getVideoFile(req, playlist.VideoFiles) | |
2d53be02 | 386 | if (!videoFile) return res.status(HttpStatusCode.NOT_FOUND_404).end() |
d7a25329 C |
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 | } | |
02756fbd | 396 | |
d7a25329 C |
397 | function getHLSPlaylist (video: MVideoFullLight) { |
398 | const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) | |
399 | if (!playlist) return undefined | |
02756fbd | 400 | |
d7a25329 | 401 | return Object.assign(playlist, { Video: video }) |
02756fbd | 402 | } |
f2eb23cd RK |
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 | } |