]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/static.ts
selective route permission to use embeds with x-frame-deny
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
CommitLineData
4d4e5cd4 1import * as cors from 'cors'
50d6de9c 2import * as express from 'express'
02756fbd 3import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
50d6de9c 4import { VideosPreviewCache } from '../lib/cache'
02756fbd
C
5import { asyncMiddleware, videosGetValidator } from '../middlewares'
6import { VideoModel } from '../models/video/video'
40e87e9e 7import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
65fcc311
C
8
9const staticRouter = express.Router()
10
11/*
60862425 12 Cors is very important to let other servers access torrent and video files
65fcc311
C
13*/
14
15const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
16staticRouter.use(
17 STATIC_PATHS.TORRENTS,
18 cors(),
49379960 19 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
65fcc311 20)
02756fbd
C
21staticRouter.use(
22 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
23 asyncMiddleware(videosGetValidator),
24 asyncMiddleware(downloadTorrent)
25)
65fcc311
C
26
27// Videos path for webseeding
28const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
29staticRouter.use(
30 STATIC_PATHS.WEBSEED,
31 cors(),
32 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE })
33)
02756fbd
C
34staticRouter.use(
35 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
36 asyncMiddleware(videosGetValidator),
37 asyncMiddleware(downloadVideoFile)
38)
65fcc311
C
39
40// Thumbnails path for express
41const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
42staticRouter.use(
43 STATIC_PATHS.THUMBNAILS,
44 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
45)
46
c5911fd3
C
47const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
48staticRouter.use(
49 STATIC_PATHS.AVATARS,
50 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE })
51)
52
40e87e9e 53// We don't have video previews, fetch them from the origin instance
65fcc311 54staticRouter.use(
f981dae8 55 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
eb080476 56 asyncMiddleware(getPreview)
65fcc311
C
57)
58
40e87e9e
C
59// We don't have video captions, fetch them from the origin instance
60staticRouter.use(
61 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
62 asyncMiddleware(getVideoCaption)
63)
64
ac235c37
RK
65// robots.txt service
66staticRouter.get('/robots.txt', (req: express.Request, res: express.Response) => {
67 res.type('text/plain')
68 return res.send(CONFIG.INSTANCE.ROBOTS)
69})
70
65fcc311
C
71// ---------------------------------------------------------------------------
72
73export {
74 staticRouter
75}
f981dae8
C
76
77// ---------------------------------------------------------------------------
78
eb080476 79async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
40e87e9e
C
80 const path = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
81 if (!path) return res.sendStatus(404)
82
83 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
84}
85
86async function getVideoCaption (req: express.Request, res: express.Response) {
87 const path = await VideosCaptionCache.Instance.getFilePath({
88 videoId: req.params.videoId,
89 language: req.params.captionLanguage
90 })
eb080476 91 if (!path) return res.sendStatus(404)
f981dae8 92
eb080476 93 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
f981dae8 94}
02756fbd
C
95
96async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 97 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
98 if (!videoFile) return res.status(404).end()
99
100 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
101}
102
103async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
9118bca3 104 const { video, videoFile } = getVideoAndFile(req, res)
02756fbd
C
105 if (!videoFile) return res.status(404).end()
106
107 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
108}
109
9118bca3 110function getVideoAndFile (req: express.Request, res: express.Response) {
02756fbd
C
111 const resolution = parseInt(req.params.resolution, 10)
112 const video: VideoModel = res.locals.video
113
114 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
115
116 return { video, videoFile }
117}