1 import * as cors from 'cors'
2 import * as express from 'express'
3 import { CONFIG, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers'
4 import { VideosPreviewCache } from '../lib/cache'
5 import { asyncMiddleware } from '../middlewares'
7 const staticRouter = express.Router()
10 Cors is very important to let other servers access torrent and video files
13 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
15 STATIC_PATHS.TORRENTS,
17 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
20 // Videos path for webseeding
21 const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
25 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE })
28 // Thumbnails path for express
29 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
31 STATIC_PATHS.THUMBNAILS,
32 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
35 const avatarsPhysicalPath = CONFIG.STORAGE.AVATARS_DIR
38 express.static(avatarsPhysicalPath, { maxAge: STATIC_MAX_AGE })
41 // Video previews path for express
43 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
44 asyncMiddleware(getPreview)
47 // ---------------------------------------------------------------------------
53 // ---------------------------------------------------------------------------
55 async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
56 const path = await VideosPreviewCache.Instance.getPreviewPath(req.params.uuid)
57 if (!path) return res.sendStatus(404)
59 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })