]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/static.ts
Fix static avatars/thumbnails cache
[github/Chocobozzz/PeerTube.git] / server / controllers / static.ts
1 import * as cors from 'cors'
2 import * as express from 'express'
3 import { CONFIG, STATIC_DOWNLOAD_PATHS, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
4 import { VideosPreviewCache } from '../lib/cache'
5 import { asyncMiddleware, videosGetValidator } from '../middlewares'
6 import { VideoModel } from '../models/video/video'
7 import { VideosCaptionCache } from '../lib/cache/videos-caption-cache'
8
9 const staticRouter = express.Router()
10
11 staticRouter.use(cors())
12
13 /*
14 Cors is very important to let other servers access torrent and video files
15 */
16
17 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
18 staticRouter.use(
19 STATIC_PATHS.TORRENTS,
20 cors(),
21 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
22 )
23 staticRouter.use(
24 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
25 asyncMiddleware(videosGetValidator),
26 asyncMiddleware(downloadTorrent)
27 )
28
29 // Videos path for webseeding
30 const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
31 staticRouter.use(
32 STATIC_PATHS.WEBSEED,
33 cors(),
34 express.static(videosPhysicalPath)
35 )
36 staticRouter.use(
37 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
38 asyncMiddleware(videosGetValidator),
39 asyncMiddleware(downloadVideoFile)
40 )
41
42 // Thumbnails path for express
43 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
44 staticRouter.use(
45 STATIC_PATHS.THUMBNAILS,
46 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
47 )
48
49 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
50 staticRouter.use(
51 STATIC_PATHS.AVATARS,
52 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE })
53 )
54
55 // We don't have video previews, fetch them from the origin instance
56 staticRouter.use(
57 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
58 asyncMiddleware(getPreview)
59 )
60
61 // We don't have video captions, fetch them from the origin instance
62 staticRouter.use(
63 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
64 asyncMiddleware(getVideoCaption)
65 )
66
67 // robots.txt service
68 staticRouter.get('/robots.txt', (req: express.Request, res: express.Response) => {
69 res.type('text/plain')
70 return res.send(CONFIG.INSTANCE.ROBOTS)
71 })
72
73 // ---------------------------------------------------------------------------
74
75 export {
76 staticRouter
77 }
78
79 // ---------------------------------------------------------------------------
80
81 async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
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
88 async 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 })
93 if (!path) return res.sendStatus(404)
94
95 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
96 }
97
98 async function downloadTorrent (req: express.Request, res: express.Response, next: express.NextFunction) {
99 const { video, videoFile } = getVideoAndFile(req, res)
100 if (!videoFile) return res.status(404).end()
101
102 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
103 }
104
105 async function downloadVideoFile (req: express.Request, res: express.Response, next: express.NextFunction) {
106 const { video, videoFile } = getVideoAndFile(req, res)
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
112 function getVideoAndFile (req: express.Request, res: express.Response) {
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 }