1 import * as cors from 'cors'
2 import * as express from 'express'
5 HLS_STREAMING_PLAYLIST_DIRECTORY,
10 } from '../initializers'
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'
21 const packageJSON = require('../../../package.json')
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: CONFIG.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 // ---------------------------------------------------------------------------
164 // ---------------------------------------------------------------------------
166 async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
167 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
168 if (!path) return res.sendStatus(404)
170 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
173 async function getVideoCaption (req: express.Request, res: express.Response) {
174 const path = await VideosCaptionCache.Instance.getFilePath({
175 videoId: req.params.videoId,
176 language: req.params.captionLanguage
178 if (!path) return res.sendStatus(404)
180 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
183 async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) {
184 const { totalVideos } = await VideoModel.getStats()
185 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
186 const { totalUsers } = await UserModel.getStats()
189 if (req.params.version && (req.params.version === '2.0')) {
194 version: packageJSON.version
206 openRegistrations: CONFIG.SIGNUP.ENABLED,
211 localPosts: totalVideos,
212 localComments: totalLocalVideoComments
218 nodeName: CONFIG.INSTANCE.NAME,
219 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
221 } as HttpNodeinfoDiasporaSoftwareNsSchema20
222 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
224 json = { error: 'Nodeinfo schema version not handled' }
228 return res.send(json).end()
231 async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
232 const { video, videoFile } = getVideoAndFile(req, res)
233 if (!videoFile) return res.status(404).end()
235 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
238 async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
239 const { video, videoFile } = getVideoAndFile(req, res)
240 if (!videoFile) return res.status(404).end()
242 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
245 function getVideoAndFile (req: express.Request, res: express.Response) {
246 const resolution = parseInt(req.params.resolution, 10)
247 const video = res.locals.video
249 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
251 return { video, videoFile }