1 import * as cors from 'cors'
2 import * as express from 'express'
3 import { CONFIG, HLS_PLAYLIST_DIRECTORY, ROUTE_CACHE_LIFETIME, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
4 import { VideosPreviewCache } from '../lib/cache'
5 import { cacheRoute } from '../middlewares/cache'
6 import { asyncMiddleware, videosGetValidator } from '../middlewares'
7 import { VideoModel } from '../models/video/video'
8 import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
9 import { UserModel } from '../models/account/user'
10 import { VideoCommentModel } from '../models/video/video-comment'
11 import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
12 import { join } from 'path'
13 import { root } from '../helpers/core-utils'
15 const packageJSON = require('../../../package.json')
16 const staticRouter = express.Router()
18 staticRouter.use(cors())
21 Cors is very important to let other servers access torrent and video files
24 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
26 STATIC_PATHS.TORRENTS,
28 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
31 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
32 asyncMiddleware(videosGetValidator),
33 asyncMiddleware(downloadTorrent)
36 // Videos path for webseeding
40 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
43 STATIC_PATHS.REDUNDANCY,
45 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
49 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
50 asyncMiddleware(videosGetValidator),
51 asyncMiddleware(downloadVideoFile)
56 STATIC_PATHS.PLAYLISTS.HLS,
58 express.static(HLS_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
61 // Thumbnails path for express
62 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
64 STATIC_PATHS.THUMBNAILS,
65 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
68 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
71 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
74 // We don't have video previews, fetch them from the origin instance
76 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
77 asyncMiddleware(getPreview)
80 // We don't have video captions, fetch them from the origin instance
82 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
83 asyncMiddleware(getVideoCaption)
87 staticRouter.get('/robots.txt',
88 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
89 (_, res: express.Response) => {
90 res.type('text/plain')
91 return res.send(CONFIG.INSTANCE.ROBOTS)
95 // security.txt service
96 staticRouter.get('/security.txt',
97 (_, res: express.Response) => {
98 return res.redirect(301, '/.well-known/security.txt')
102 staticRouter.get('/.well-known/security.txt',
103 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
104 (_, res: express.Response) => {
105 res.type('text/plain')
106 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
111 staticRouter.use('/.well-known/nodeinfo',
112 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
113 (_, res: express.Response) => {
117 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
118 href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json'
124 staticRouter.use('/nodeinfo/:version.json',
125 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
126 asyncMiddleware(generateNodeinfo)
129 // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
130 staticRouter.use('/.well-known/dnt-policy.txt',
131 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
132 (_, res: express.Response) => {
133 res.type('text/plain')
135 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
139 // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
140 staticRouter.use('/.well-known/dnt/',
141 (_, res: express.Response) => {
142 res.json({ tracking: 'N' })
146 staticRouter.use('/.well-known/change-password',
147 (_, res: express.Response) => {
148 res.redirect('/my-account/settings')
152 // ---------------------------------------------------------------------------
158 // ---------------------------------------------------------------------------
160 async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
161 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
162 if (!path) return res.sendStatus(404)
164 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
167 async function getVideoCaption (req: express.Request, res: express.Response) {
168 const path = await VideosCaptionCache.Instance.getFilePath({
169 videoId: req.params.videoId,
170 language: req.params.captionLanguage
172 if (!path) return res.sendStatus(404)
174 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
177 async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) {
178 const { totalVideos } = await VideoModel.getStats()
179 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
180 const { totalUsers } = await UserModel.getStats()
183 if (req.params.version && (req.params.version === '2.0')) {
188 version: packageJSON.version
200 openRegistrations: CONFIG.SIGNUP.ENABLED,
205 localPosts: totalVideos,
206 localComments: totalLocalVideoComments
212 nodeName: CONFIG.INSTANCE.NAME,
213 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
215 } as HttpNodeinfoDiasporaSoftwareNsSchema20
216 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
218 json = { error: 'Nodeinfo schema version not handled' }
222 return res.send(json).end()
225 async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
226 const { video, videoFile } = getVideoAndFile(req, res)
227 if (!videoFile) return res.status(404).end()
229 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
232 async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
233 const { video, videoFile } = getVideoAndFile(req, res)
234 if (!videoFile) return res.status(404).end()
236 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
239 function getVideoAndFile (req: express.Request, res: express.Response) {
240 const resolution = parseInt(req.params.resolution, 10)
241 const video: VideoModel = res.locals.video
243 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
245 return { video, videoFile }