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