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