2021-08-27 08:32:44 -04:00
|
|
|
import cors from 'cors'
|
|
|
|
import express from 'express'
|
2023-07-31 08:34:36 -04:00
|
|
|
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,
|
2023-06-07 02:53:14 -04:00
|
|
|
VideoMiniaturePermanentFileCache,
|
2023-06-06 09:59:51 -04:00
|
|
|
VideoPreviewsSimpleFileCache,
|
|
|
|
VideoStoryboardsSimpleFileCache,
|
|
|
|
VideoTorrentsSimpleFileCache
|
2023-07-31 08:34:36 -04:00
|
|
|
} 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',
|
2022-07-27 08:38:07 -04:00
|
|
|
asyncMiddleware(getActorImage),
|
|
|
|
handleStaticError
|
2021-04-06 05:35:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
lazyStaticRouter.use(
|
|
|
|
LAZY_STATIC_PATHS.BANNERS + ':filename',
|
2022-07-27 08:38:07 -04:00
|
|
|
asyncMiddleware(getActorImage),
|
|
|
|
handleStaticError
|
2019-08-09 05:32:40 -04:00
|
|
|
)
|
|
|
|
|
2023-06-07 02:53:14 -04:00
|
|
|
lazyStaticRouter.use(
|
|
|
|
LAZY_STATIC_PATHS.THUMBNAILS + ':filename',
|
|
|
|
asyncMiddleware(getThumbnail),
|
|
|
|
handleStaticError
|
|
|
|
)
|
|
|
|
|
2019-08-09 05:32:40 -04:00
|
|
|
lazyStaticRouter.use(
|
2021-02-12 10:23:19 -05:00
|
|
|
LAZY_STATIC_PATHS.PREVIEWS + ':filename',
|
2022-07-27 08:38:07 -04:00
|
|
|
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',
|
2022-07-27 08:38:07 -04:00
|
|
|
asyncMiddleware(getVideoCaption),
|
|
|
|
handleStaticError
|
2019-08-09 05:32:40 -04:00
|
|
|
)
|
|
|
|
|
2021-02-16 10:25:53 -05:00
|
|
|
lazyStaticRouter.use(
|
|
|
|
LAZY_STATIC_PATHS.TORRENTS + ':filename',
|
2022-07-27 08:38:07 -04:00
|
|
|
asyncMiddleware(getTorrent),
|
|
|
|
handleStaticError
|
2021-02-16 10:25:53 -05:00
|
|
|
)
|
|
|
|
|
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
|
2022-06-20 05:48:40 -04:00
|
|
|
|
2023-06-06 09:59:51 -04:00
|
|
|
return avatarPermanentFileCache.lazyServe({ filename, res, next })
|
2022-06-20 05:48:40 -04:00
|
|
|
}
|
|
|
|
|
2023-06-07 02:53:14 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
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)
|
2021-05-31 19:36:53 -04:00
|
|
|
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
|
2019-08-09 05:32:40 -04:00
|
|
|
|
2021-02-16 10:25:53 -05: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)
|
2021-05-31 19:36:53 -04:00
|
|
|
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
|
2019-08-09 05:32:40 -04:00
|
|
|
|
2021-02-16 10:25:53 -05: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)
|
2021-05-31 19:36:53 -04:00
|
|
|
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
|
2021-02-16 10:25:53 -05:00
|
|
|
|
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 })
|
|
|
|
}
|