1 import * as cors from 'cors'
2 import { createReadStream } from 'fs-extra'
3 import * as express from 'express'
4 import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } 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 '../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)
83 staticRouter.use('/.well-known/nodeinfo',
84 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
85 (_, res: express.Response) => {
89 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
90 href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json'
96 staticRouter.use('/nodeinfo/:version.json',
97 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
98 asyncMiddleware(generateNodeinfo)
101 // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
102 staticRouter.use('/.well-known/dnt-policy.txt',
103 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
104 (_, res: express.Response) => {
105 res.type('text/plain')
106 createReadStream('./server/static/dnt-policy/dnt-policy-1.0.txt').pipe(res)
110 // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
111 staticRouter.use('/.well-known/dnt/',
112 (_, res: express.Response) => {
113 res.json({ tracking: 'N' })
117 // ---------------------------------------------------------------------------
123 // ---------------------------------------------------------------------------
125 async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
126 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
127 if (!path) return res.sendStatus(404)
129 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
132 async function getVideoCaption (req: express.Request, res: express.Response) {
133 const path = await VideosCaptionCache.Instance.getFilePath({
134 videoId: req.params.videoId,
135 language: req.params.captionLanguage
137 if (!path) return res.sendStatus(404)
139 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
142 async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) {
143 const { totalVideos } = await VideoModel.getStats()
144 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
145 const { totalUsers } = await UserModel.getStats()
148 if (req.params.version && (req.params.version === '2.0')) {
153 version: packageJSON.version
165 openRegistrations: CONFIG.SIGNUP.ENABLED,
170 localPosts: totalVideos,
171 localComments: totalLocalVideoComments
177 nodeName: CONFIG.INSTANCE.NAME,
178 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
180 } as HttpNodeinfoDiasporaSoftwareNsSchema20
181 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
183 json = { error: 'Nodeinfo schema version not handled' }
187 return res.send(json).end()
190 async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
191 const { video, videoFile } = getVideoAndFile(req, res)
192 if (!videoFile) return res.status(404).end()
194 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
197 async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
198 const { video, videoFile } = getVideoAndFile(req, res)
199 if (!videoFile) return res.status(404).end()
201 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
204 function getVideoAndFile (req: express.Request, res: express.Response) {
205 const resolution = parseInt(req.params.resolution, 10)
206 const video: VideoModel = res.locals.video
208 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
210 return { video, videoFile }