PeerTube/server/core/controllers/lazy-static.ts

128 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-08-27 14:32:44 +02:00
import cors from 'cors'
import express from 'express'
import { HttpStatusCode } from '@peertube/peertube-models'
import { CONFIG } from '@server/initializers/config.js'
import { FILES_CACHE, LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants.js'
2023-06-06 15:59:51 +02:00
import {
AvatarPermanentFileCache,
VideoCaptionsSimpleFileCache,
VideoMiniaturePermanentFileCache,
2023-06-06 15:59:51 +02:00
VideoPreviewsSimpleFileCache,
VideoStoryboardsSimpleFileCache,
VideoTorrentsSimpleFileCache
} from '../lib/files-cache/index.js'
import { asyncMiddleware, handleStaticError } from '../middlewares/index.js'
2023-06-06 15:59:51 +02:00
// ---------------------------------------------------------------------------
// Cache initializations
// ---------------------------------------------------------------------------
VideoPreviewsSimpleFileCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, FILES_CACHE.PREVIEWS.MAX_AGE)
VideoCaptionsSimpleFileCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE)
VideoTorrentsSimpleFileCache.Instance.init(CONFIG.CACHE.TORRENTS.SIZE, FILES_CACHE.TORRENTS.MAX_AGE)
VideoStoryboardsSimpleFileCache.Instance.init(CONFIG.CACHE.STORYBOARDS.SIZE, FILES_CACHE.STORYBOARDS.MAX_AGE)
// ---------------------------------------------------------------------------
2019-08-09 11:32:40 +02:00
const lazyStaticRouter = express.Router()
lazyStaticRouter.use(cors())
lazyStaticRouter.use(
LAZY_STATIC_PATHS.AVATARS + ':filename',
asyncMiddleware(getActorImage),
handleStaticError
2021-04-06 11:35:56 +02:00
)
lazyStaticRouter.use(
LAZY_STATIC_PATHS.BANNERS + ':filename',
asyncMiddleware(getActorImage),
handleStaticError
2019-08-09 11:32:40 +02:00
)
lazyStaticRouter.use(
LAZY_STATIC_PATHS.THUMBNAILS + ':filename',
asyncMiddleware(getThumbnail),
handleStaticError
)
2019-08-09 11:32:40 +02:00
lazyStaticRouter.use(
LAZY_STATIC_PATHS.PREVIEWS + ':filename',
asyncMiddleware(getPreview),
handleStaticError
2019-08-09 11:32:40 +02:00
)
2023-06-01 14:51:16 +02:00
lazyStaticRouter.use(
LAZY_STATIC_PATHS.STORYBOARDS + ':filename',
asyncMiddleware(getStoryboard),
handleStaticError
)
2019-08-09 11:32:40 +02:00
lazyStaticRouter.use(
2021-02-15 14:08:16 +01:00
LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
asyncMiddleware(getVideoCaption),
handleStaticError
2019-08-09 11:32:40 +02:00
)
lazyStaticRouter.use(
LAZY_STATIC_PATHS.TORRENTS + ':filename',
asyncMiddleware(getTorrent),
handleStaticError
)
2019-08-09 11:32:40 +02:00
// ---------------------------------------------------------------------------
export {
lazyStaticRouter,
getPreview,
getVideoCaption
}
// ---------------------------------------------------------------------------
2023-06-06 15:59:51 +02:00
const avatarPermanentFileCache = new AvatarPermanentFileCache()
2019-08-09 11:32:40 +02:00
2023-06-06 15:59:51 +02:00
function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
const filename = req.params.filename
2023-06-06 15:59:51 +02:00
return avatarPermanentFileCache.lazyServe({ filename, res, next })
}
// ---------------------------------------------------------------------------
const videoMiniaturePermanentFileCache = new VideoMiniaturePermanentFileCache()
function getThumbnail (req: express.Request, res: express.Response, next: express.NextFunction) {
const filename = req.params.filename
return videoMiniaturePermanentFileCache.lazyServe({ filename, res, next })
}
// ---------------------------------------------------------------------------
2019-08-09 11:32:40 +02:00
async function getPreview (req: express.Request, res: express.Response) {
2023-06-06 15:59:51 +02:00
const result = await VideoPreviewsSimpleFileCache.Instance.getFilePath(req.params.filename)
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
2019-08-09 11:32:40 +02:00
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
2019-08-09 11:32:40 +02:00
}
2023-06-01 14:51:16 +02:00
async function getStoryboard (req: express.Request, res: express.Response) {
2023-06-06 15:59:51 +02:00
const result = await VideoStoryboardsSimpleFileCache.Instance.getFilePath(req.params.filename)
2023-06-01 14:51:16 +02:00
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
}
2019-08-09 11:32:40 +02:00
async function getVideoCaption (req: express.Request, res: express.Response) {
2023-06-06 15:59:51 +02:00
const result = await VideoCaptionsSimpleFileCache.Instance.getFilePath(req.params.filename)
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
2019-08-09 11:32:40 +02:00
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
}
async function getTorrent (req: express.Request, res: express.Response) {
2023-06-06 15:59:51 +02:00
const result = await VideoTorrentsSimpleFileCache.Instance.getFilePath(req.params.filename)
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
2019-08-09 11:32:40 +02:00
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
}