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