1 import * as cors from 'cors'
2 import * as express from 'express'
4 HLS_STREAMING_PLAYLIST_DIRECTORY, PEERTUBE_VERSION,
10 } from '../initializers/constants'
11 import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
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'
22 const staticRouter = express.Router()
24 staticRouter.use(cors())
27 Cors is very important to let other servers access torrent and video files
30 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
32 STATIC_PATHS.TORRENTS,
34 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
37 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
38 asyncMiddleware(videosGetValidator),
39 asyncMiddleware(downloadTorrent)
42 // Videos path for webseeding
46 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
49 STATIC_PATHS.REDUNDANCY,
51 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
55 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
56 asyncMiddleware(videosGetValidator),
57 asyncMiddleware(downloadVideoFile)
62 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
64 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
67 // Thumbnails path for express
68 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
70 STATIC_PATHS.THUMBNAILS,
71 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
74 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
77 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
80 // We don't have video previews, fetch them from the origin instance
82 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
83 asyncMiddleware(getPreview)
86 // We don't have video captions, fetch them from the origin instance
88 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
89 asyncMiddleware(getVideoCaption)
93 staticRouter.get('/robots.txt',
94 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
95 (_, res: express.Response) => {
96 res.type('text/plain')
97 return res.send(CONFIG.INSTANCE.ROBOTS)
101 // security.txt service
102 staticRouter.get('/security.txt',
103 (_, res: express.Response) => {
104 return res.redirect(301, '/.well-known/security.txt')
108 staticRouter.get('/.well-known/security.txt',
109 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
110 (_, res: express.Response) => {
111 res.type('text/plain')
112 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
117 staticRouter.use('/.well-known/nodeinfo',
118 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
119 (_, res: express.Response) => {
123 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
124 href: WEBSERVER.URL + '/nodeinfo/2.0.json'
130 staticRouter.use('/nodeinfo/:version.json',
131 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
132 asyncMiddleware(generateNodeinfo)
135 // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
136 staticRouter.use('/.well-known/dnt-policy.txt',
137 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
138 (_, res: express.Response) => {
139 res.type('text/plain')
141 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
145 // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
146 staticRouter.use('/.well-known/dnt/',
147 (_, res: express.Response) => {
148 res.json({ tracking: 'N' })
152 staticRouter.use('/.well-known/change-password',
153 (_, res: express.Response) => {
154 res.redirect('/my-account/settings')
158 staticRouter.use('/.well-known/host-meta',
159 (_, res: express.Response) => {
160 res.type('application/xml')
162 const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
163 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
164 ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
171 // ---------------------------------------------------------------------------
177 // ---------------------------------------------------------------------------
179 async function getPreview (req: express.Request, res: express.Response) {
180 const result = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
181 if (!result) return res.sendStatus(404)
183 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE })
186 async function getVideoCaption (req: express.Request, res: express.Response) {
187 const result = await VideosCaptionCache.Instance.getFilePath({
188 videoId: req.params.videoId,
189 language: req.params.captionLanguage
191 if (!result) return res.sendStatus(404)
193 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE })
196 async function generateNodeinfo (req: express.Request, res: express.Response) {
197 const { totalVideos } = await VideoModel.getStats()
198 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
199 const { totalUsers } = await UserModel.getStats()
202 if (req.params.version && (req.params.version === '2.0')) {
207 version: PEERTUBE_VERSION
219 openRegistrations: CONFIG.SIGNUP.ENABLED,
224 localPosts: totalVideos,
225 localComments: totalLocalVideoComments
231 nodeName: CONFIG.INSTANCE.NAME,
232 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
234 } as HttpNodeinfoDiasporaSoftwareNsSchema20
235 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
237 json = { error: 'Nodeinfo schema version not handled' }
241 return res.send(json).end()
244 async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
245 const { video, videoFile } = getVideoAndFile(req, res)
246 if (!videoFile) return res.status(404).end()
248 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
251 async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
252 const { video, videoFile } = getVideoAndFile(req, res)
253 if (!videoFile) return res.status(404).end()
255 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
258 function getVideoAndFile (req: express.Request, res: express.Response) {
259 const resolution = parseInt(req.params.resolution, 10)
260 const video = res.locals.video
262 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
264 return { video, videoFile }