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