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()
12 Cors is very important to let other servers access torrent and video files
15 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
17 STATIC_PATHS.TORRENTS,
19 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
22 STATIC_DOWNLOAD_PATHS.TORRENTS + ':id-:resolution([0-9]+).torrent',
23 asyncMiddleware(videosGetValidator),
24 asyncMiddleware(downloadTorrent)
27 // Videos path for webseeding
28 const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
32 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE })
35 STATIC_DOWNLOAD_PATHS.VIDEOS + ':id-:resolution([0-9]+).:extension',
36 asyncMiddleware(videosGetValidator),
37 asyncMiddleware(downloadVideoFile)
40 // Thumbnails path for express
41 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
43 STATIC_PATHS.THUMBNAILS,
44 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
47 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
50 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE })
53 // We don't have video previews, fetch them from the origin instance
55 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
56 asyncMiddleware(getPreview)
59 // We don't have video captions, fetch them from the origin instance
61 STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
62 asyncMiddleware(getVideoCaption)
66 staticRouter.get('/robots.txt', (req: express.Request, res: express.Response) => {
67 res.type('text/plain')
68 return res.send(CONFIG.INSTANCE.ROBOTS)
71 // ---------------------------------------------------------------------------
77 // ---------------------------------------------------------------------------
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)
83 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
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
91 if (!path) return res.sendStatus(404)
93 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })
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()
100 return res.download(video.getTorrentFilePath(videoFile), `${video.name}-${videoFile.resolution}p.torrent`)
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()
107 return res.download(video.getVideoFilePath(videoFile), `${video.name}-${videoFile.resolution}p${videoFile.extname}`)
110 function getVideoAndFile (req: express.Request, res: express.Response) {
111 const resolution = parseInt(req.params.resolution, 10)
112 const video: VideoModel = res.locals.video
114 const videoFile = video.VideoFiles.find(f => f.resolution === resolution)
116 return { video, videoFile }