1 import * as cors from 'cors'
2 import { createReadStream } from 'fs-extra'
3 import * as express from 'express'
4 import { CONFIG, ROUTE_CACHE_LIFETIME, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
5 import { VideosPreviewCache } from '../lib/cache'
6 import { cacheRoute } from '../middlewares/cache'
7 import { asyncMiddleware, videosGetValidator } from '../middlewares'
8 import { VideoModel } from '../models/video/video'
9 import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
10 import { UserModel } from '../models/account/user'
11 import { VideoCommentModel } from '../models/video/video-comment'
12 import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
14 const packageJSON = require('../../../package.json')
15 const staticRouter = express.Router()
17 staticRouter.use(cors())
20 Cors is very important to let other servers access torrent and video files
23 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
25 STATIC_PATHS.TORRENTS,
27 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
30 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
31 asyncMiddleware(videosGetValidator),
32 asyncMiddleware(downloadTorrent)
35 // Videos path for webseeding
36 const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
40 express.static(videosPhysicalPath)
43 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
44 asyncMiddleware(videosGetValidator),
45 asyncMiddleware(downloadVideoFile)
48 // Thumbnails path for express
49 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
51 STATIC_PATHS.THUMBNAILS,
52 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
55 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
58 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
61 // We don't have video previews, fetch them from the origin instance
63 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
64 asyncMiddleware(getPreview)
67 // We don't have video captions, fetch them from the origin instance
69 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
70 asyncMiddleware(getVideoCaption)
74 staticRouter.get('/robots.txt',
75 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
76 (_, res: express.Response) => {
77 res.type('text/plain')
78 return res.send(CONFIG.INSTANCE.ROBOTS)
82 // security.txt service
83 staticRouter.get('/security.txt',
84 (_, res: express.Response) => {
85 return res.redirect(301, '/.well-known/security.txt')
89 staticRouter.get('/.well-known/security.txt',
90 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
91 (_, res: express.Response) => {
92 res.type('text/plain')
93 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
98 staticRouter.use('/.well-known/nodeinfo',
99 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
100 (_, res: express.Response) => {
104 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
105 href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json'
111 staticRouter.use('/nodeinfo/:version.json',
112 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
113 asyncMiddleware(generateNodeinfo)
116 // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
117 staticRouter.use('/.well-known/dnt-policy.txt',
118 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
119 (_, res: express.Response) => {
120 res.type('text/plain')
121 createReadStream('./server/static/dnt-policy/dnt-policy-1.0.txt').pipe(res)
125 // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
126 staticRouter.use('/.well-known/dnt/',
127 (_, res: express.Response) => {
128 res.json({ tracking: 'N' })
132 // ---------------------------------------------------------------------------
138 // ---------------------------------------------------------------------------
140 async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
141 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
142 if (!path) return res.sendStatus(404)
144 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
147 async function getVideoCaption (req: express.Request, res: express.Response) {
148 const path = await VideosCaptionCache.Instance.getFilePath({
149 videoId: req.params.videoId,
150 language: req.params.captionLanguage
152 if (!path) return res.sendStatus(404)
154 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
157 async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) {
158 const { totalVideos } = await VideoModel.getStats()
159 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
160 const { totalUsers } = await UserModel.getStats()
163 if (req.params.version && (req.params.version === '2.0')) {
168 version: packageJSON.version
180 openRegistrations: CONFIG.SIGNUP.ENABLED,
185 localPosts: totalVideos,
186 localComments: totalLocalVideoComments
192 nodeName: CONFIG.INSTANCE.NAME,
193 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
195 } as HttpNodeinfoDiasporaSoftwareNsSchema20
196 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
198 json = { error: 'Nodeinfo schema version not handled' }
202 return res.send(json).end()
205 async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
206 const { video, videoFile } = getVideoAndFile(req, res)
207 if (!videoFile) return res.status(404).end()
209 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
212 async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
213 const { video, videoFile } = getVideoAndFile(req, res)
214 if (!videoFile) return res.status(404).end()
216 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
219 function getVideoAndFile (req: express.Request, res: express.Response) {
220 const resolution = parseInt(req.params.resolution, 10)
221 const video: VideoModel = res.locals.video
223 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
225 return { video, videoFile }