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'
9 const staticRouter = express.Router()
11 staticRouter.use(cors())
14 Cors is very important to let other servers access torrent and video files
17 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
19 STATIC_PATHS.TORRENTS,
21 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
24 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
25 asyncMiddleware(videosGetValidator),
26 asyncMiddleware(downloadTorrent)
29 // Videos path for webseeding
30 const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
34 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE })
37 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
38 asyncMiddleware(videosGetValidator),
39 asyncMiddleware(downloadVideoFile)
42 // Thumbnails path for express
43 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
45 STATIC_PATHS.THUMBNAILS,
46 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
49 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
52 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE })
55 // We don't have video previews, fetch them from the origin instance
57 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
58 asyncMiddleware(getPreview)
61 // We don't have video captions, fetch them from the origin instance
63 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
64 asyncMiddleware(getVideoCaption)
68 staticRouter.get('/robots.txt', (req: express.Request, res: express.Response) => {
69 res.type('text/plain')
70 return res.send(CONFIG.INSTANCE.ROBOTS)
73 // ---------------------------------------------------------------------------
79 // ---------------------------------------------------------------------------
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)
85 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
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
93 if (!path) return res.sendStatus(404)
95 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
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()
102 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
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()
109 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
112 function getVideoAndFile (req: express.Request, res: express.Response) {
113 const resolution = parseInt(req.params.resolution, 10)
114 const video: VideoModel = res.locals.video
116 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
118 return { video, videoFile }