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