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