1 import * as cors from 'cors'
2 import * as express from 'express'
6 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, isEmailEnabled } from '../initializers/config'
23 import { getPreview, getVideoCaption } from './lazy-static'
24 import { VideoStreamingPlaylistType } from '@shared/models/videos/video-streaming-playlist.type'
25 import { MVideoFile, MVideoFullLight } from '@server/typings/models'
26 import { getTorrentFilePath, getVideoFilePath } from '@server/lib/video-paths'
27 import { getThemeOrDefault } from '../lib/plugins/theme-utils'
28 import { getEnabledResolutions, getRegisteredPlugins, getRegisteredThemes } from '@server/controllers/api/config'
30 const staticRouter = express.Router()
32 staticRouter.use(cors())
35 Cors is very important to let other servers access torrent and video files
38 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
40 STATIC_PATHS.TORRENTS,
42 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
45 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
46 asyncMiddleware(videosDownloadValidator),
50 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+)-hls.torrent',
51 asyncMiddleware(videosDownloadValidator),
52 downloadHLSVideoFileTorrent
55 // Videos path for webseeding
59 express.static(CONFIG.STORAGE.VIDEOS_DIR, { fallthrough: false }) // 404 because we don't have this video
62 STATIC_PATHS.REDUNDANCY,
64 express.static(CONFIG.STORAGE.REDUNDANCY_DIR, { fallthrough: false }) // 404 because we don't have this video
68 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
69 asyncMiddleware(videosDownloadValidator),
74 STATIC_DOWNLOAD_PATHS.HLS_VIDEOS + ':id-:resolution([0-9]+)-fragmented.:extension',
75 asyncMiddleware(videosDownloadValidator),
81 STATIC_PATHS.STREAMING_PLAYLISTS.HLS,
83 express.static(HLS_STREAMING_PLAYLIST_DIRECTORY, { fallthrough: false }) // 404 if the file does not exist
86 // Thumbnails path for express
87 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
89 STATIC_PATHS.THUMBNAILS,
90 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
93 // DEPRECATED: use lazy-static route instead
94 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
97 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE.SERVER, fallthrough: false }) // 404 if the file does not exist
100 // DEPRECATED: use lazy-static route instead
102 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
103 asyncMiddleware(getPreview)
106 // DEPRECATED: use lazy-static route instead
108 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
109 asyncMiddleware(getVideoCaption)
112 // robots.txt service
113 staticRouter.get('/robots.txt',
114 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.ROBOTS)),
115 (_, res: express.Response) => {
116 res.type('text/plain')
117 return res.send(CONFIG.INSTANCE.ROBOTS)
121 // security.txt service
122 staticRouter.get('/security.txt',
123 (_, res: express.Response) => {
124 return res.redirect(301, '/.well-known/security.txt')
128 staticRouter.get('/.well-known/security.txt',
129 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
130 (_, res: express.Response) => {
131 res.type('text/plain')
132 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
137 staticRouter.use('/.well-known/nodeinfo',
138 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)),
139 (_, res: express.Response) => {
143 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
144 href: WEBSERVER.URL + '/nodeinfo/2.0.json'
150 staticRouter.use('/nodeinfo/:version.json',
151 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.NODEINFO)),
152 asyncMiddleware(generateNodeinfo)
155 // dnt-policy.txt service (see https://www.eff.org/dnt-policy)
156 staticRouter.use('/.well-known/dnt-policy.txt',
157 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
158 (_, res: express.Response) => {
159 res.type('text/plain')
161 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
165 // dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
166 staticRouter.use('/.well-known/dnt/',
167 (_, res: express.Response) => {
168 res.json({ tracking: 'N' })
172 staticRouter.use('/.well-known/change-password',
173 (_, res: express.Response) => {
174 res.redirect('/my-account/settings')
178 staticRouter.use('/.well-known/host-meta',
179 (_, res: express.Response) => {
180 res.type('application/xml')
182 const xml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
183 '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n' +
184 ` <Link rel="lrdd" type="application/xrd+xml" template="${WEBSERVER.URL}/.well-known/webfinger?resource={uri}"/>\n` +
191 // ---------------------------------------------------------------------------
197 // ---------------------------------------------------------------------------
199 async function generateNodeinfo (req: express.Request, res: express.Response) {
200 const { totalVideos } = await VideoModel.getStats()
201 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
202 const { totalUsers } = await UserModel.getStats()
205 if (req.params.version && (req.params.version === '2.0')) {
210 version: PEERTUBE_VERSION
222 openRegistrations: CONFIG.SIGNUP.ENABLED,
227 localPosts: totalVideos,
228 localComments: totalLocalVideoComments
234 nodeName: CONFIG.INSTANCE.NAME,
235 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION,
239 users: CONFIG.SEARCH.REMOTE_URI.USERS,
240 anonymous: CONFIG.SEARCH.REMOTE_URI.ANONYMOUS
244 registered: getRegisteredPlugins()
247 registered: getRegisteredThemes(),
248 default: getThemeOrDefault(CONFIG.THEME.DEFAULT, DEFAULT_THEME_NAME)
251 enabled: isEmailEnabled()
254 enabled: CONFIG.CONTACT_FORM.ENABLED
258 enabled: CONFIG.TRANSCODING.HLS.ENABLED
261 enabled: CONFIG.TRANSCODING.WEBTORRENT.ENABLED
263 enabledResolutions: getEnabledResolutions()
268 enabled: CONFIG.IMPORT.VIDEOS.HTTP.ENABLED
271 enabled: CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED
278 enabled: CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED
285 max: CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max
287 extensions: CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
292 extensions: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME,
294 max: CONSTRAINTS_FIELDS.VIDEOS.IMAGE.FILE_SIZE.max
298 extensions: CONSTRAINTS_FIELDS.VIDEOS.EXTNAME
304 max: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.FILE_SIZE.max
306 extensions: CONSTRAINTS_FIELDS.VIDEO_CAPTIONS.CAPTION_FILE.EXTNAME
310 videoQuota: CONFIG.USER.VIDEO_QUOTA,
311 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY
315 intervalDays: CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS
319 enabled: CONFIG.TRACKER.ENABLED
323 } as HttpNodeinfoDiasporaSoftwareNsSchema20
324 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
326 json = { error: 'Nodeinfo schema version not handled' }
330 return res.send(json).end()
333 function downloadTorrent (req: express.Request, res: express.Response) {
334 const video = res.locals.videoAll
336 const videoFile = getVideoFile(req, video.VideoFiles)
337 if (!videoFile) return res.status(404).end()
339 return res.download(getTorrentFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
342 function downloadHLSVideoFileTorrent (req: express.Request, res: express.Response) {
343 const video = res.locals.videoAll
345 const playlist = getHLSPlaylist(video)
346 if (!playlist) return res.status(404).end
348 const videoFile = getVideoFile(req, playlist.VideoFiles)
349 if (!videoFile) return res.status(404).end()
351 return res.download(getTorrentFilePath(playlist, videoFile), `${video.name}-${videoFile.resolution}p-hls.torrent`)
354 function downloadVideoFile (req: express.Request, res: express.Response) {
355 const video = res.locals.videoAll
357 const videoFile = getVideoFile(req, video.VideoFiles)
358 if (!videoFile) return res.status(404).end()
360 return res.download(getVideoFilePath(video, videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
363 function downloadHLSVideoFile (req: express.Request, res: express.Response) {
364 const video = res.locals.videoAll
365 const playlist = getHLSPlaylist(video)
366 if (!playlist) return res.status(404).end
368 const videoFile = getVideoFile(req, playlist.VideoFiles)
369 if (!videoFile) return res.status(404).end()
371 const filename = `${video.name}-${videoFile.resolution}p-${playlist.getStringType()}${videoFile.extname}`
372 return res.download(getVideoFilePath(playlist, videoFile), filename)
375 function getVideoFile (req: express.Request, files: MVideoFile[]) {
376 const resolution = parseInt(req.params.resolution, 10)
377 return files.find(f => f.resolution === resolution)
380 function getHLSPlaylist (video: MVideoFullLight) {
381 const playlist = video.VideoStreamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
382 if (!playlist) return undefined
384 return Object.assign(playlist, { Video: video })