1
0
Fork 0
peertube/server/core/controllers/lazy-static.ts

129 lines
4.4 KiB
TypeScript
Raw Normal View History

2021-08-27 08:32:44 -04: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 09:59:51 -04:00
import {
AvatarPermanentFileCache,
VideoCaptionsSimpleFileCache,
VideoMiniaturePermanentFileCache,
2023-06-06 09:59:51 -04:00
VideoPreviewsSimpleFileCache,
VideoStoryboardsSimpleFileCache,
VideoTorrentsSimpleFileCache
} from '../lib/files-cache/index.js'
import { asyncMiddleware, handleStaticError } from '../middlewares/index.js'
2023-06-06 09:59:51 -04: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 05:32:40 -04:00
const lazyStaticRouter = express.Router()
lazyStaticRouter.use(cors())
lazyStaticRouter.use(
LAZY_STATIC_PATHS.AVATARS + ':filename',
asyncMiddleware(getActorImage),
handleStaticError
2021-04-06 05:35:56 -04:00
)
lazyStaticRouter.use(
LAZY_STATIC_PATHS.BANNERS + ':filename',
asyncMiddleware(getActorImage),
handleStaticError
2019-08-09 05:32:40 -04:00
)
lazyStaticRouter.use(
LAZY_STATIC_PATHS.THUMBNAILS + ':filename',
asyncMiddleware(getThumbnail),
handleStaticError
)
2019-08-09 05:32:40 -04:00
lazyStaticRouter.use(
LAZY_STATIC_PATHS.PREVIEWS + ':filename',
asyncMiddleware(getPreview),
handleStaticError
2019-08-09 05:32:40 -04:00
)
2023-06-01 08:51:16 -04:00
lazyStaticRouter.use(
LAZY_STATIC_PATHS.STORYBOARDS + ':filename',
asyncMiddleware(getStoryboard),
handleStaticError
)
2019-08-09 05:32:40 -04:00
lazyStaticRouter.use(
2021-02-15 08:08:16 -05:00
LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
asyncMiddleware(getVideoCaption),
handleStaticError
2019-08-09 05:32:40 -04:00
)
lazyStaticRouter.use(
LAZY_STATIC_PATHS.TORRENTS + ':filename',
asyncMiddleware(getTorrent),
handleStaticError
)
2019-08-09 05:32:40 -04:00
// ---------------------------------------------------------------------------
export {
lazyStaticRouter,
getPreview,
getVideoCaption
}
// ---------------------------------------------------------------------------
2023-06-06 09:59:51 -04:00
const avatarPermanentFileCache = new AvatarPermanentFileCache()
2019-08-09 05:32:40 -04:00
2023-06-06 09:59:51 -04:00
function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
const filename = req.params.filename
2023-06-06 09:59:51 -04: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 05:32:40 -04:00
async function getPreview (req: express.Request, res: express.Response) {
2023-06-06 09:59:51 -04:00
const result = await VideoPreviewsSimpleFileCache.Instance.getFilePath(req.params.filename)
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
2019-08-09 05:32:40 -04:00
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
2019-08-09 05:32:40 -04:00
}
2023-06-01 08:51:16 -04:00
async function getStoryboard (req: express.Request, res: express.Response) {
2023-06-06 09:59:51 -04:00
const result = await VideoStoryboardsSimpleFileCache.Instance.getFilePath(req.params.filename)
2023-06-01 08:51:16 -04: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 05:32:40 -04:00
async function getVideoCaption (req: express.Request, res: express.Response) {
2023-06-06 09:59:51 -04:00
const result = await VideoCaptionsSimpleFileCache.Instance.getFilePath(req.params.filename)
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
2019-08-09 05:32:40 -04:00
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
}
async function getTorrent (req: express.Request, res: express.Response) {
2023-06-06 09:59:51 -04:00
const result = await VideoTorrentsSimpleFileCache.Instance.getFilePath(req.params.filename)
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
2021-02-18 05:28:00 -05:00
// Torrents still use the old naming convention (video uuid + .torrent)
2019-08-09 05:32:40 -04:00
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
}