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