1 import * as cors from 'cors'
2 import * as express from 'express'
4 HLS_STREAMING_PLAYLIST_DIRECTORY,
11 } from '../initializers/constants'
12 import { cacheRoute } from '../middlewares/cache'
13 import { asyncMiddleware, videosGetValidator } from '../middlewares'
14 import { VideoModel } from '../models/video/video'
15 import { UserModel } from '../models/account/user'
16 import { VideoCommentModel } from '../models/video/video-comment'
17 import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
18 import { join } from 'path'
19 import { root } from '../helpers/core-utils'
20 import { CONFIG } from '../initializers/config'
21 import { getPreview, getVideoCaption } from './lazy-static'
23 const staticRouter = express.Router()
25 staticRouter.use(cors())
28 Cors is very important to let other servers access torrent and video files
31 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
33 STATIC_PATHS.TORRENTS,
35 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
38 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
39 asyncMiddleware(videosGetValidator),
40 asyncMiddleware(downloadTorrent)
43 // Videos path for webseeding
47 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
50 STATIC_PATHS.REDUNDANCY,
52 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
56 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
57 asyncMiddleware(videosGetValidator),
58 asyncMiddleware(downloadVideoFile)
63 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
65 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
68 // Thumbnails path for express
69 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
71 STATIC_PATHS.THUMBNAILS,
72 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
75 // DEPRECATED: use lazy-static route instead
76 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
79 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
82 // DEPRECATED: use lazy-static route instead
84 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
85 asyncMiddleware(getPreview)
88 // DEPRECATED: use lazy-static route instead
90 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
91 asyncMiddleware(getVideoCaption)
95 staticRouter.get('/robots.txt',
96 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
97 (_, res: express.Response) => {
98 res.type('text/plain')
99 return res.send(CONFIG.INSTANCE.ROBOTS)
103 // security.txt service
104 staticRouter.get('/security.txt',
105 (_, res: express.Response) => {
106 return res.redirect(301, '/.well-known/security.txt')
110 staticRouter.get('/.well-known/security.txt',
111 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
112 (_, res: express.Response) => {
113 res.type('text/plain')
114 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
119 staticRouter.use('/.well-known/nodeinfo',
120 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
121 (_, res: express.Response) => {
125 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
126 href: WEBSERVER.URL + '/nodeinfo/2.0.json'
132 staticRouter.use('/nodeinfo/:version.json',
133 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
134 asyncMiddleware(generateNodeinfo)
137 // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
138 staticRouter.use('/.well-known/dnt-policy.txt',
139 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
140 (_, res: express.Response) => {
141 res.type('text/plain')
143 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
147 // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
148 staticRouter.use('/.well-known/dnt/',
149 (_, res: express.Response) => {
150 res.json({ tracking: 'N' })
154 staticRouter.use('/.well-known/change-password',
155 (_, res: express.Response) => {
156 res.redirect('/my-account/settings')
160 staticRouter.use('/.well-known/host-meta',
161 (_, res: express.Response) => {
162 res.type('application/xml')
164 const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
165 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
166 ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
173 // ---------------------------------------------------------------------------
179 // ---------------------------------------------------------------------------
181 async function generateNodeinfo (req: express.Request, res: express.Response) {
182 const { totalVideos } = await VideoModel.getStats()
183 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
184 const { totalUsers } = await UserModel.getStats()
187 if (req.params.version && (req.params.version === '2.0')) {
192 version: PEERTUBE_VERSION
204 openRegistrations: CONFIG.SIGNUP.ENABLED,
209 localPosts: totalVideos,
210 localComments: totalLocalVideoComments
216 nodeName: CONFIG.INSTANCE.NAME,
217 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
219 } as HttpNodeinfoDiasporaSoftwareNsSchema20
220 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
222 json = { error: 'Nodeinfo schema version not handled' }
226 return res.send(json).end()
229 async function downloadTorrent (req: express.Request, res: express.Response) {
230 const { video, videoFile } = getVideoAndFile(req, res)
231 if (!videoFile) return res.status(404).end()
233 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
236 async function downloadVideoFile (req: express.Request, res: express.Response) {
237 const { video, videoFile } = getVideoAndFile(req, res)
238 if (!videoFile) return res.status(404).end()
240 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
243 function getVideoAndFile (req: express.Request, res: express.Response) {
244 const resolution = parseInt(req.params.resolution, 10)
245 const video = res.locals.videoAll
247 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
249 return { video, videoFile }