]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/static.ts
Fix thumbnail processing
[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
37const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
38staticRouter.use(
39 STATIC_PATHS.WEBSEED,
40 cors(),
57a81ff6 41 express.static(videosPhysicalPath)
65fcc311 42)
02756fbd
C
43staticRouter.use(
44 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
45 asyncMiddleware(videosGetValidator),
46 asyncMiddleware(downloadVideoFile)
47)
65fcc311
C
48
49// Thumbnails path for express
50const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
51staticRouter.use(
52 STATIC_PATHS.THUMBNAILS,
a8bf1d82 53 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
65fcc311
C
54)
55
c5911fd3
C
56const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
57staticRouter.use(
58 STATIC_PATHS.AVATARS,
a8bf1d82 59 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE, fallthrough: false }) // 404 if the file does not exist
c5911fd3
C
60)
61
40e87e9e 62// We don't have video previews, fetch them from the origin instance
65fcc311 63staticRouter.use(
f981dae8 64 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
eb080476 65 asyncMiddleware(getPreview)
65fcc311
C
66)
67
40e87e9e
C
68// We don't have video captions, fetch them from the origin instance
69staticRouter.use(
70 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
71 asyncMiddleware(getVideoCaption)
72)
73
ac235c37 74// robots.txt service
3f6d68d9 75staticRouter.get('/robots.txt',
98d3324d 76 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS)),
3f6d68d9
RK
77 (_, res: express.Response) => {
78 res.type('text/plain')
79 return res.send(CONFIG.INSTANCE.ROBOTS)
80 }
81)
82
5447516b
AH
83// security.txt service
84staticRouter.get('/security.txt',
85 (_, res: express.Response) => {
86 return res.redirect(301, '/.well-known/security.txt')
87 }
88)
89
90staticRouter.get('/.well-known/security.txt',
91 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.SECURITYTXT)),
92 (_, res: express.Response) => {
93 res.type('text/plain')
94 return res.send(CONFIG.INSTANCE.SECURITYTXT + CONFIG.INSTANCE.SECURITYTXT_CONTACT)
95 }
96)
97
3f6d68d9
RK
98// nodeinfo service
99staticRouter.use('/.well-known/nodeinfo',
98d3324d 100 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
3f6d68d9
RK
101 (_, res: express.Response) => {
102 return res.json({
103 links: [
104 {
105 rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
106 href: CONFIG.WEBSERVER.URL + '/nodeinfo/2.0.json'
107 }
108 ]
109 })
110 }
111)
112staticRouter.use('/nodeinfo/:version.json',
aad0ec24 113 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.NODEINFO)),
3f6d68d9
RK
114 asyncMiddleware(generateNodeinfo)
115)
ac235c37 116
aad0ec24
RK
117// dnt-policy.txt service (see https://www.eff.org/dnt-policy)
118staticRouter.use('/.well-known/dnt-policy.txt',
119 asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.DNT_POLICY)),
120 (_, res: express.Response) => {
121 res.type('text/plain')
aac0118d 122
d1105b97 123 return res.sendFile(join(root(), 'dist/server/static/dnt-policy/dnt-policy-1.0.txt'))
aad0ec24
RK
124 }
125)
126
127// dnt service (see https://www.w3.org/TR/tracking-dnt/#status-resource)
128staticRouter.use('/.well-known/dnt/',
129 (_, res: express.Response) => {
130 res.json({ tracking: 'N' })
131 }
132)
133
65fcc311
C
134// ---------------------------------------------------------------------------
135
136export {
137 staticRouter
138}
f981dae8
C
139
140// ---------------------------------------------------------------------------
141
eb080476 142async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
40e87e9e
C
143 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
144 if (!path) return res.sendStatus(404)
145
146 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
147}
148
149async function getVideoCaption (req: express.Request, res: express.Response) {
150 const path = await VideosCaptionCache.Instance.getFilePath({
151 videoId: req.params.videoId,
152 language: req.params.captionLanguage
153 })
eb080476 154 if (!path) return res.sendStatus(404)
f981dae8 155
eb080476 156 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
f981dae8 157}
02756fbd 158
3f6d68d9
RK
159async function generateNodeinfo (req: express.Request, res: express.Response, next: express.NextFunction) {
160 const { totalVideos } = await VideoModel.getStats()
161 const { totalLocalVideoComments } = await VideoCommentModel.getStats()
162 const { totalUsers } = await UserModel.getStats()
163 let json = {}
164
165 if (req.params.version && (req.params.version === '2.0')) {
166 json = {
167 version: '2.0',
168 software: {
169 name: 'peertube',
170 version: packageJSON.version
171 },
172 protocols: [
173 'activitypub'
174 ],
175 services: {
176 inbound: [],
177 outbound: [
178 'atom1.0',
179 'rss2.0'
180 ]
181 },
182 openRegistrations: CONFIG.SIGNUP.ENABLED,
183 usage: {
184 users: {
185 total: totalUsers
186 },
187 localPosts: totalVideos,
188 localComments: totalLocalVideoComments
189 },
190 metadata: {
191 taxonomy: {
192 postsName: 'Videos'
193 },
194 nodeName: CONFIG.INSTANCE.NAME,
195 nodeDescription: CONFIG.INSTANCE.SHORT_DESCRIPTION
196 }
197 } as HttpNodeinfoDiasporaSoftwareNsSchema20
98d3324d 198 res.contentType('application/json; profile="http://nodeinfo.diaspora.software/ns/schema/2.0#"')
3f6d68d9
RK
199 } else {
200 json = { error: 'Nodeinfo schema version not handled' }
201 res.status(404)
202 }
203
98d3324d 204 return res.send(json).end()
3f6d68d9
RK
205}
206
02756fbd 207async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 208 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
209 if (!videoFile) return res.status(404).end()
210
211 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
212}
213
214async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 215 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
216 if (!videoFile) return res.status(404).end()
217
218 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
219}
220
9118bca3 221function getVideoAndFile (req: express.Request, res: express.Response) {
02756fbd
C
222 const resolution = parseInt(req.params.resolution, 10)
223 const video: VideoModel = res.locals.video
224
225 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
226
227 return { video, videoFile }
228}