]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as cors from 'cors' |
50d6de9c | 2 | import * as express from 'express' |
09209296 | 3 | import { CONFIG, HLS_PLAYLIST_DIRECTORY, ROUTE_CACHE_LIFETIME, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers' |
50d6de9c | 4 | import { VideosPreviewCache } from '../lib/cache' |
98d3324d | 5 | import { cacheRoute } from '../middlewares/cache' |
02756fbd C |
6 | import { asyncMiddleware, videosGetValidator } from '../middlewares' |
7 | import { VideoModel } from '../models/video/video' | |
40e87e9e | 8 | import { VideosCaptionCache } from '../lib/cache/videos-caption-cache' |
3f6d68d9 RK |
9 | import { UserModel } from '../models/account/user' |
10 | import { VideoCommentModel } from '../models/video/video-comment' | |
c75937d0 | 11 | import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo' |
aac0118d C |
12 | import { join } from 'path' |
13 | import { root } from '../helpers/core-utils' | |
65fcc311 | 14 | |
3f6d68d9 | 15 | const packageJSON = require('../../../package.json') |
65fcc311 C |
16 | const staticRouter = express.Router() |
17 | ||
62945f06 C |
18 | staticRouter.use(cors()) |
19 | ||
65fcc311 | 20 | /* |
60862425 | 21 | Cors is very important to let other servers access torrent and video files |
65fcc311 C |
22 | */ |
23 | ||
24 | const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR | |
25 | staticRouter.use( | |
26 | STATIC_PATHS.TORRENTS, | |
27 | cors(), | |
49379960 | 28 | express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file |
65fcc311 | 29 | ) |
02756fbd C |
30 | staticRouter.use( |
31 | STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent', | |
32 | asyncMiddleware(videosGetValidator), | |
33 | asyncMiddleware(downloadTorrent) | |
34 | ) | |
65fcc311 C |
35 | |
36 | // Videos path for webseeding | |
65fcc311 C |
37 | staticRouter.use( |
38 | STATIC_PATHS.WEBSEED, | |
39 | cors(), | |
b9fffa29 | 40 | express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video |
65fcc311 | 41 | ) |
6040f87d | 42 | staticRouter.use( |
b9fffa29 | 43 | STATIC_PATHS.REDUNDANCY, |
6040f87d | 44 | cors(), |
b9fffa29 | 45 | express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video |
6040f87d C |
46 | ) |
47 | ||
02756fbd C |
48 | staticRouter.use( |
49 | STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension', | |
50 | asyncMiddleware(videosGetValidator), | |
51 | asyncMiddleware(downloadVideoFile) | |
52 | ) | |
65fcc311 | 53 | |
09209296 C |
54 | // HLS |
55 | staticRouter.use( | |
56 | STATIC_PATHS.PLAYLISTS.HLS, | |
57 | cors(), | |
58 | express.static(HLS_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist | |
59 | ) | |
60 | ||
65fcc311 C |
61 | // Thumbnails path for express |
62 | const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR | |
63 | staticRouter.use( | |
64 | STATIC_PATHS.THUMBNAILS, | |
a8bf1d82 | 65 | express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist |
65fcc311 C |
66 | ) |
67 | ||
c5911fd3 C |
68 | const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR |
69 | staticRouter.use( | |
70 | STATIC_PATHS.AVATARS, | |
a8bf1d82 | 71 | express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist |
c5911fd3 C |
72 | ) |
73 | ||
40e87e9e | 74 | // We don't have video previews, fetch them from the origin instance |
65fcc311 | 75 | staticRouter.use( |
f981dae8 | 76 | STATIC_PATHS.PREVIEWS + ':uuid.jpg', |
eb080476 | 77 | asyncMiddleware(getPreview) |
65fcc311 C |
78 | ) |
79 | ||
40e87e9e C |
80 | // We don't have video captions, fetch them from the origin instance |
81 | staticRouter.use( | |
82 | STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt', | |
83 | asyncMiddleware(getVideoCaption) | |
84 | ) | |
85 | ||
ac235c37 | 86 | // robots.txt service |
3f6d68d9 | 87 | staticRouter.get('/robots.txt', |
98d3324d | 88 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)), |
3f6d68d9 RK |
89 | (_, res: express.Response) => { |
90 | res.type('text/plain') | |
91 | return res.send(CONFIG.INSTANCE.ROBOTS) | |
92 | } | |
93 | ) | |
94 | ||
5447516b AH |
95 | // security.txt service |
96 | staticRouter.get('/security.txt', | |
97 | (_, res: express.Response) => { | |
98 | return res.redirect(301, '/.well-known/security.txt') | |
99 | } | |
100 | ) | |
101 | ||
102 | staticRouter.get('/.well-known/security.txt', | |
103 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)), | |
104 | (_, res: express.Response) => { | |
105 | res.type('text/plain') | |
106 | return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT) | |
107 | } | |
108 | ) | |
109 | ||
3f6d68d9 RK |
110 | // nodeinfo service |
111 | staticRouter.use('/.well-known/nodeinfo', | |
98d3324d | 112 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)), |
3f6d68d9 RK |
113 | (_, res: express.Response) => { |
114 | return res.json({ | |
115 | links: [ | |
116 | { | |
117 | rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0', | |
118 | href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json' | |
119 | } | |
120 | ] | |
121 | }) | |
122 | } | |
123 | ) | |
124 | staticRouter.use('/nodeinfo/:version.json', | |
aad0ec24 | 125 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)), |
3f6d68d9 RK |
126 | asyncMiddleware(generateNodeinfo) |
127 | ) | |
ac235c37 | 128 | |
aad0ec24 RK |
129 | // dnt-policy.txt service (see https://www.eff.org/dnt-policy) |
130 | staticRouter.use('/.well-known/dnt-policy.txt', | |
131 | asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)), | |
132 | (_, res: express.Response) => { | |
133 | res.type('text/plain') | |
aac0118d | 134 | |
d1105b97 | 135 | return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt')) |
aad0ec24 RK |
136 | } |
137 | ) | |
138 | ||
139 | // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource) | |
140 | staticRouter.use('/.well-known/dnt/', | |
141 | (_, res: express.Response) => { | |
142 | res.json({ tracking: 'N' }) | |
31414127 RK |
143 | } |
144 | ) | |
145 | ||
146 | staticRouter.use('/.well-known/change-password', | |
147 | (_, res: express.Response) => { | |
148 | res.redirect('/my-account/settings') | |
aad0ec24 RK |
149 | } |
150 | ) | |
151 | ||
65fcc311 C |
152 | // --------------------------------------------------------------------------- |
153 | ||
154 | export { | |
155 | staticRouter | |
156 | } | |
f981dae8 C |
157 | |
158 | // --------------------------------------------------------------------------- | |
159 | ||
eb080476 | 160 | async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) { |
40e87e9e C |
161 | const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid) |
162 | if (!path) return res.sendStatus(404) | |
163 | ||
164 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE }) | |
165 | } | |
166 | ||
167 | async function getVideoCaption (req: express.Request, res: express.Response) { | |
168 | const path = await VideosCaptionCache.Instance.getFilePath({ | |
169 | videoId: req.params.videoId, | |
170 | language: req.params.captionLanguage | |
171 | }) | |
eb080476 | 172 | if (!path) return res.sendStatus(404) |
f981dae8 | 173 | |
eb080476 | 174 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE }) |
f981dae8 | 175 | } |
02756fbd | 176 | |
3f6d68d9 RK |
177 | async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) { |
178 | const { totalVideos } = await VideoModel.getStats() | |
179 | const { totalLocalVideoComments } = await VideoCommentModel.getStats() | |
180 | const { totalUsers } = await UserModel.getStats() | |
181 | let json = {} | |
182 | ||
183 | if (req.params.version && (req.params.version === '2.0')) { | |
184 | json = { | |
185 | version: '2.0', | |
186 | software: { | |
187 | name: 'peertube', | |
188 | version: packageJSON.version | |
189 | }, | |
190 | protocols: [ | |
191 | 'activitypub' | |
192 | ], | |
193 | services: { | |
194 | inbound: [], | |
195 | outbound: [ | |
196 | 'atom1.0', | |
197 | 'rss2.0' | |
198 | ] | |
199 | }, | |
200 | openRegistrations: CONFIG.SIGNUP.ENABLED, | |
201 | usage: { | |
202 | users: { | |
203 | total: totalUsers | |
204 | }, | |
205 | localPosts: totalVideos, | |
206 | localComments: totalLocalVideoComments | |
207 | }, | |
208 | metadata: { | |
209 | taxonomy: { | |
210 | postsName: 'Videos' | |
211 | }, | |
212 | nodeName: CONFIG.INSTANCE.NAME, | |
213 | nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION | |
214 | } | |
215 | } as HttpNodeinfoDiasporaSoftwareNsSchema20 | |
98d3324d | 216 | res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"') |
3f6d68d9 RK |
217 | } else { |
218 | json = { error: 'Nodeinfo schema version not handled' } | |
219 | res.status(404) | |
220 | } | |
221 | ||
98d3324d | 222 | return res.send(json).end() |
3f6d68d9 RK |
223 | } |
224 | ||
02756fbd | 225 | async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) { |
9118bca3 | 226 | const { video, videoFile } = getVideoAndFile(req, res) |
02756fbd C |
227 | if (!videoFile) return res.status(404).end() |
228 | ||
229 | return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`) | |
230 | } | |
231 | ||
232 | async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) { | |
9118bca3 | 233 | const { video, videoFile } = getVideoAndFile(req, res) |
02756fbd C |
234 | if (!videoFile) return res.status(404).end() |
235 | ||
236 | return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`) | |
237 | } | |
238 | ||
9118bca3 | 239 | function getVideoAndFile (req: express.Request, res: express.Response) { |
02756fbd C |
240 | const resolution = parseInt(req.params.resolution, 10) |
241 | const video: VideoModel = res.locals.video | |
242 | ||
243 | const videoFile = video.VideoFiles.find(f => f.resolution === resolution) | |
244 | ||
245 | return { video, videoFile } | |
246 | } |