1 import * as cors from 'cors'
2 import * as express from 'express'
4 HLS_STREAMING_PLAYLIST_DIRECTORY,
13 } from '../initializers/constants'
14 import { cacheRoute } from '../middlewares/cache'
15 import { asyncMiddleware, videosDownloadValidator } from '../middlewares'
16 import { VideoModel } from '../models/video/video'
17 import { UserModel } from '../models/account/user'
18 import { VideoCommentModel } from '../models/video/video-comment'
19 import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../../shared/models/nodeinfo'
20 import { join } from 'path'
21 import { root } from '../helpers/core-utils'
22 import { CONFIG } from '../initializers/config'
23 import { Emailer } from '../lib/emailer'
24 import { getPreview, getVideoCaption } from './lazy-static'
25 import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type'
26 import { MVideoFile, MVideoFullLight } from '@server/typings/models'
27 import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths'
28 import { getThemeOrDefault } from '../lib/plugins/theme-utils'
29 import { getEnabledResolutions, getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config'
31 const staticRouter = express.Router()
33 staticRouter.use(cors())
36 Cors is very important to let other servers access torrent and video files
39 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
41 STATIC_PATHS.TORRENTS,
43 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
46 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
47 asyncMiddleware(videosDownloadValidator),
48 asyncMiddleware(downloadTorrent)
51 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+)-hls.torrent',
52 asyncMiddleware(videosDownloadValidator),
53 asyncMiddleware(downloadHLSVideoFileTorrent)
56 // Videos path for webseeding
60 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
63 STATIC_PATHS.REDUNDANCY,
65 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
69 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
70 asyncMiddleware(videosDownloadValidator),
71 asyncMiddleware(downloadVideoFile)
75 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
76 asyncMiddleware(videosDownloadValidator),
77 asyncMiddleware(downloadHLSVideoFile)
82 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
84 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
87 // Thumbnails path for express
88 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
90 STATIC_PATHS.THUMBNAILS,
91 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
94 // DEPRECATED: use lazy-static route instead
95 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
98 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
101 // DEPRECATED: use lazy-static route instead
103 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
104 asyncMiddleware(getPreview)
107 // DEPRECATED: use lazy-static route instead
109 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
110 asyncMiddleware(getVideoCaption)
113 // robots.txt service
114 staticRouter.get('/robots.txt',
115 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
116 (_, res: express.Response) => {
117 res.type('text/plain')
118 return res.send(CONFIG.INSTANCE.ROBOTS)
122 // security.txt service
123 staticRouter.get('/security.txt',
124 (_, res: express.Response) => {
125 return res.redirect(301, '/.well-known/security.txt')
129 staticRouter.get('/.well-known/security.txt',
130 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
131 (_, res: express.Response) => {
132 res.type('text/plain')
133 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
138 staticRouter.use('/.well-known/nodeinfo',
139 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
140 (_, res: express.Response) => {
144 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
145 href: WEBSERVER.URL + '/nodeinfo/2.0.json'
151 staticRouter.use('/nodeinfo/:version.json',
152 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
153 asyncMiddleware(generateNodeinfo)
156 // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
157 staticRouter.use('/.well-known/dnt-policy.txt',
158 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
159 (_, res: express.Response) => {
160 res.type('text/plain')
162 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
166 // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
167 staticRouter.use('/.well-known/dnt/',
168 (_, res: express.Response) => {
169 res.json({ tracking: 'N' })
173 staticRouter.use('/.well-known/change-password',
174 (_, res: express.Response) => {
175 res.redirect('/my-account/settings')
179 staticRouter.use('/.well-known/host-meta',
180 (_, res: express.Response) => {
181 res.type('application/xml')
183 const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
184 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
185 ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
192 // ---------------------------------------------------------------------------
198 // ---------------------------------------------------------------------------
200 async function generateNodeinfo (req: express.Request, res: express.Response) {
201 const { totalVideos } = await VideoModel.getStats()
202 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
203 const { totalUsers } = await UserModel.getStats()
206 if (req.params.version && (req.params.version === '2.0')) {
211 version: PEERTUBE_VERSION
223 openRegistrations: CONFIG.SIGNUP.ENABLED,
228 localPosts: totalVideos,
229 localComments: totalLocalVideoComments
235 nodeName: CONFIG.INSTANCE.NAME,
236 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
239 registered: getRegisteredPlugins()
242 registered: getRegisteredThemes(),
243 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
246 enabled: Emailer.isEnabled()
249 enabled: CONFIG.CONTACT_FORM.ENABLED
253 enabled: CONFIG.TRANSCODING.HLS.ENABLED
256 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
258 enabledResolutions: getEnabledResolutions()
263 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
266 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
273 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
280 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
282 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
287 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
289 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
293 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
299 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
301 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
305 videoQuota: CONFIG.USER.VIDEO_QUOTA,
306 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
310 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
314 enabled: CONFIG.TRACKER.ENABLED
318 } as HttpNodeinfoDiasporaSoftwareNsSchema20
319 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
321 json = { error: 'Nodeinfo schema version not handled' }
325 return res.send(json).end()
328 async function downloadTorrent (req: express.Request, res: express.Response) {
329 const video = res.locals.videoAll
331 const videoFile = getVideoFile(req, video.VideoFiles)
332 if (!videoFile) return res.status(404).end()
334 return res.download(getTorrentFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
337 async function downloadHLSVideoFileTorrent (req: express.Request, res: express.Response) {
338 const video = res.locals.videoAll
340 const playlist = getHLSPlaylist(video)
341 if (!playlist) return res.status(404).end
343 const videoFile = getVideoFile(req, playlist.VideoFiles)
344 if (!videoFile) return res.status(404).end()
346 return res.download(getTorrentFilePath(playlist, videoFile), `${video.name}-${videoFile.resolution}p-hls.torrent`)
349 async function downloadVideoFile (req: express.Request, res: express.Response) {
350 const video = res.locals.videoAll
352 const videoFile = getVideoFile(req, video.VideoFiles)
353 if (!videoFile) return res.status(404).end()
355 return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
358 async function downloadHLSVideoFile (req: express.Request, res: express.Response) {
359 const video = res.locals.videoAll
360 const playlist = getHLSPlaylist(video)
361 if (!playlist) return res.status(404).end
363 const videoFile = getVideoFile(req, playlist.VideoFiles)
364 if (!videoFile) return res.status(404).end()
366 const filename = `${video.name}-${videoFile.resolution}p-${playlist.getStringType()}${videoFile.extname}`
367 return res.download(getVideoFilePath(playlist, videoFile), filename)
370 function getVideoFile (req: express.Request, files: MVideoFile[]) {
371 const resolution = parseInt(req.params.resolution, 10)
372 return files.find(f => f.resolution === resolution)
375 function getHLSPlaylist (video: MVideoFullLight) {
376 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
377 if (!playlist) return undefined
379 return Object.assign(playlist, { Video: video })