1 import * as cors from 'cors'
2 import * as express from 'express'
3 import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } 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 '../models/nodeinfo'
13 const packageJSON = require('../../../package.json')
14 const staticRouter = express.Router()
16 staticRouter.use(cors())
19 Cors is very important to let other servers access torrent and video files
22 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
24 STATIC_PATHS.TORRENTS,
26 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
29 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
30 asyncMiddleware(videosGetValidator),
31 asyncMiddleware(downloadTorrent)
34 // Videos path for webseeding
35 const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
39 express.static(videosPhysicalPath)
42 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
43 asyncMiddleware(videosGetValidator),
44 asyncMiddleware(downloadVideoFile)
47 // Thumbnails path for express
48 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
50 STATIC_PATHS.THUMBNAILS,
51 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
54 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
57 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE })
60 // We don't have video previews, fetch them from the origin instance
62 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
63 asyncMiddleware(getPreview)
66 // We don't have video captions, fetch them from the origin instance
68 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
69 asyncMiddleware(getVideoCaption)
73 staticRouter.get('/robots.txt',
74 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
75 (_, res: express.Response) => {
76 res.type('text/plain')
77 return res.send(CONFIG.INSTANCE.ROBOTS)
82 staticRouter.use('/.well-known/nodeinfo',
83 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
84 (_, res: express.Response) => {
88 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
89 href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json'
95 staticRouter.use('/nodeinfo/:version.json',
96 // asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
97 asyncMiddleware(generateNodeinfo)
100 // ---------------------------------------------------------------------------
106 // ---------------------------------------------------------------------------
108 async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
109 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
110 if (!path) return res.sendStatus(404)
112 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
115 async function getVideoCaption (req: express.Request, res: express.Response) {
116 const path = await VideosCaptionCache.Instance.getFilePath({
117 videoId: req.params.videoId,
118 language: req.params.captionLanguage
120 if (!path) return res.sendStatus(404)
122 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
125 async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) {
126 const { totalVideos } = await VideoModel.getStats()
127 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
128 const { totalUsers } = await UserModel.getStats()
131 if (req.params.version && (req.params.version === '2.0')) {
136 version: packageJSON.version
148 openRegistrations: CONFIG.SIGNUP.ENABLED,
153 localPosts: totalVideos,
154 localComments: totalLocalVideoComments
160 nodeName: CONFIG.INSTANCE.NAME,
161 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
163 } as HttpNodeinfoDiasporaSoftwareNsSchema20
164 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
166 json = { error: 'Nodeinfo schema version not handled' }
170 return res.send(json).end()
173 async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
174 const { video, videoFile } = getVideoAndFile(req, res)
175 if (!videoFile) return res.status(404).end()
177 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
180 async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
181 const { video, videoFile } = getVideoAndFile(req, res)
182 if (!videoFile) return res.status(404).end()
184 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
187 function getVideoAndFile (req: express.Request, res: express.Response) {
188 const resolution = parseInt(req.params.resolution, 10)
189 const video: VideoModel = res.locals.video
191 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
193 return { video, videoFile }