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