1 import * as express from 'express'
2 import * as cors from 'cors'
8 } from '../initializers'
9 import { VideosPreviewCache } from '../lib'
10 import { asyncMiddleware } from '../middlewares'
12 const staticRouter = express.Router()
15 Cors is very important to let other servers access torrent and video files
18 const torrentsPhysicalPath = CONFIG.STORAGE.TORRENTS_DIR
20 STATIC_PATHS.TORRENTS,
22 express.static(torrentsPhysicalPath, { maxAge: 0 }) // Don't cache because we could regenerate the torrent file
25 // Videos path for webseeding
26 const videosPhysicalPath = CONFIG.STORAGE.VIDEOS_DIR
30 express.static(videosPhysicalPath, { maxAge: STATIC_MAX_AGE })
33 // Thumbnails path for express
34 const thumbnailsPhysicalPath = CONFIG.STORAGE.THUMBNAILS_DIR
36 STATIC_PATHS.THUMBNAILS,
37 express.static(thumbnailsPhysicalPath, { maxAge: STATIC_MAX_AGE })
40 // Video previews path for express
42 STATIC_PATHS.PREVIEWS + ':uuid.jpg',
43 asyncMiddleware(getPreview)
46 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 async function getPreview (req: express.Request, res: express.Response, next: express.NextFunction) {
55 const path = await VideosPreviewCache.Instance.getPreviewPath(req.params.uuid)
56 if (!path) return res.sendStatus(404)
58 return res.sendFile(path, { maxAge: STATIC_MAX_AGE })