]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - controllers/static.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / controllers / static.ts
... / ...
CommitLineData
1import * as cors from 'cors'
2import * as express from 'express'
3import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS, ROUTE_CACHE_LIFETIME } from '../initializers'
4import { VideosPreviewCache } from '../lib/cache'
5import { cacheRoute } from '../middlewares/cache'
6import { asyncMiddleware, videosGetValidator } from '../middlewares'
7import { VideoModel } from '../models/video/video'
8import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
9import { UserModel } from '../models/account/user'
10import { VideoCommentModel } from '../models/video/video-comment'
11import { HttpNodeinfoDiasporaSoftwareNsSchema20 } from '../models/nodeinfo'
12
13const packageJSON = require('../../../package.json')
14const staticRouter = express.Router()
15
16staticRouter.use(cors())
17
18/*
19 Cors is very important to let other servers access torrent and video files
20*/
21
22const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
23staticRouter.use(
24 STATIC_PATHS.TORRENTS,
25 cors(),
26 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
27)
28staticRouter.use(
29 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
30 asyncMiddleware(videosGetValidator),
31 asyncMiddleware(downloadTorrent)
32)
33
34// Videos path for webseeding
35const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
36staticRouter.use(
37 STATIC_PATHS.WEBSEED,
38 cors(),
39 express.static(videosPhysicalPath)
40)
41staticRouter.use(
42 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
43 asyncMiddleware(videosGetValidator),
44 asyncMiddleware(downloadVideoFile)
45)
46
47// Thumbnails path for express
48const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
49staticRouter.use(
50 STATIC_PATHS.THUMBNAILS,
51 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
52)
53
54const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
55staticRouter.use(
56 STATIC_PATHS.AVATARS,
57 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
58)
59
60// We don't have video previews, fetch them from the origin instance
61staticRouter.use(
62 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
63 asyncMiddleware(getPreview)
64)
65
66// We don't have video captions, fetch them from the origin instance
67staticRouter.use(
68 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
69 asyncMiddleware(getVideoCaption)
70)
71
72// robots.txt service
73staticRouter.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)
78 }
79)
80
81// nodeinfo service
82staticRouter.use('/.well-known/nodeinfo',
83 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
84 (_, res: express.Response) => {
85 return res.json({
86 links: [
87 {
88 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
89 href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json'
90 }
91 ]
92 })
93 }
94)
95staticRouter.use('/nodeinfo/:version.json',
96 // asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
97 asyncMiddleware(generateNodeinfo)
98)
99
100// ---------------------------------------------------------------------------
101
102export {
103 staticRouter
104}
105
106// ---------------------------------------------------------------------------
107
108async 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)
111
112 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
113}
114
115async 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
119 })
120 if (!path) return res.sendStatus(404)
121
122 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
123}
124
125async 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()
129 let json = {}
130
131 if (req.params.version && (req.params.version === '2.0')) {
132 json = {
133 version: '2.0',
134 software: {
135 name: 'peertube',
136 version: packageJSON.version
137 },
138 protocols: [
139 'activitypub'
140 ],
141 services: {
142 inbound: [],
143 outbound: [
144 'atom1.0',
145 'rss2.0'
146 ]
147 },
148 openRegistrations: CONFIG.SIGNUP.ENABLED,
149 usage: {
150 users: {
151 total: totalUsers
152 },
153 localPosts: totalVideos,
154 localComments: totalLocalVideoComments
155 },
156 metadata: {
157 taxonomy: {
158 postsName: 'Videos'
159 },
160 nodeName: CONFIG.INSTANCE.NAME,
161 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
162 }
163 } as HttpNodeinfoDiasporaSoftwareNsSchema20
164 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
165 } else {
166 json = { error: 'Nodeinfo schema version not handled' }
167 res.status(404)
168 }
169
170 return res.send(json).end()
171}
172
173async 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()
176
177 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
178}
179
180async 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()
183
184 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
185}
186
187function getVideoAndFile (req: express.Request, res: express.Response) {
188 const resolution = parseInt(req.params.resolution, 10)
189 const video: VideoModel = res.locals.video
190
191 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
192
193 return { video, videoFile }
194}