2021-08-27 07:32:44 -05:00
|
|
|
import cors from 'cors'
|
|
|
|
import express from 'express'
|
2023-06-06 08:59:51 -05:00
|
|
|
import { CONFIG } from '@server/initializers/config'
|
2021-07-16 03:42:24 -05:00
|
|
|
import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
|
2023-06-06 08:59:51 -05:00
|
|
|
import { FILES_CACHE, LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
|
|
|
|
import {
|
|
|
|
AvatarPermanentFileCache,
|
|
|
|
VideoCaptionsSimpleFileCache,
|
2023-06-07 01:53:14 -05:00
|
|
|
VideoMiniaturePermanentFileCache,
|
2023-06-06 08:59:51 -05:00
|
|
|
VideoPreviewsSimpleFileCache,
|
|
|
|
VideoStoryboardsSimpleFileCache,
|
|
|
|
VideoTorrentsSimpleFileCache
|
|
|
|
} from '../lib/files-cache'
|
2022-07-27 07:38:07 -05:00
|
|
|
import { asyncMiddleware, handleStaticError } from '../middlewares'
|
2023-06-06 08:59:51 -05: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 04:32:40 -05:00
|
|
|
|
|
|
|
const lazyStaticRouter = express.Router()
|
|
|
|
|
|
|
|
lazyStaticRouter.use(cors())
|
|
|
|
|
|
|
|
lazyStaticRouter.use(
|
|
|
|
LAZY_STATIC_PATHS.AVATARS + ':filename',
|
2022-07-27 07:38:07 -05:00
|
|
|
asyncMiddleware(getActorImage),
|
|
|
|
handleStaticError
|
2021-04-06 04:35:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
lazyStaticRouter.use(
|
|
|
|
LAZY_STATIC_PATHS.BANNERS + ':filename',
|
2022-07-27 07:38:07 -05:00
|
|
|
asyncMiddleware(getActorImage),
|
|
|
|
handleStaticError
|
2019-08-09 04:32:40 -05:00
|
|
|
)
|
|
|
|
|
2023-06-07 01:53:14 -05:00
|
|
|
lazyStaticRouter.use(
|
|
|
|
LAZY_STATIC_PATHS.THUMBNAILS + ':filename',
|
|
|
|
asyncMiddleware(getThumbnail),
|
|
|
|
handleStaticError
|
|
|
|
)
|
|
|
|
|
2019-08-09 04:32:40 -05:00
|
|
|
lazyStaticRouter.use(
|
2021-02-12 09:23:19 -06:00
|
|
|
LAZY_STATIC_PATHS.PREVIEWS + ':filename',
|
2022-07-27 07:38:07 -05:00
|
|
|
asyncMiddleware(getPreview),
|
|
|
|
handleStaticError
|
2019-08-09 04:32:40 -05:00
|
|
|
)
|
|
|
|
|
2023-06-01 07:51:16 -05:00
|
|
|
lazyStaticRouter.use(
|
|
|
|
LAZY_STATIC_PATHS.STORYBOARDS + ':filename',
|
|
|
|
asyncMiddleware(getStoryboard),
|
|
|
|
handleStaticError
|
|
|
|
)
|
|
|
|
|
2019-08-09 04:32:40 -05:00
|
|
|
lazyStaticRouter.use(
|
2021-02-15 07:08:16 -06:00
|
|
|
LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
|
2022-07-27 07:38:07 -05:00
|
|
|
asyncMiddleware(getVideoCaption),
|
|
|
|
handleStaticError
|
2019-08-09 04:32:40 -05:00
|
|
|
)
|
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
lazyStaticRouter.use(
|
|
|
|
LAZY_STATIC_PATHS.TORRENTS + ':filename',
|
2022-07-27 07:38:07 -05:00
|
|
|
asyncMiddleware(getTorrent),
|
|
|
|
handleStaticError
|
2021-02-16 09:25:53 -06:00
|
|
|
)
|
|
|
|
|
2019-08-09 04:32:40 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
lazyStaticRouter,
|
|
|
|
getPreview,
|
|
|
|
getVideoCaption
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2023-06-06 08:59:51 -05:00
|
|
|
const avatarPermanentFileCache = new AvatarPermanentFileCache()
|
2019-08-09 04:32:40 -05:00
|
|
|
|
2023-06-06 08:59:51 -05:00
|
|
|
function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const filename = req.params.filename
|
2022-06-20 04:48:40 -05:00
|
|
|
|
2023-06-06 08:59:51 -05:00
|
|
|
return avatarPermanentFileCache.lazyServe({ filename, res, next })
|
2022-06-20 04:48:40 -05:00
|
|
|
}
|
|
|
|
|
2023-06-07 01:53:14 -05: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 04:32:40 -05:00
|
|
|
async function getPreview (req: express.Request, res: express.Response) {
|
2023-06-06 08:59:51 -05:00
|
|
|
const result = await VideoPreviewsSimpleFileCache.Instance.getFilePath(req.params.filename)
|
2021-05-31 18:36:53 -05:00
|
|
|
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
|
2019-08-09 04:32:40 -05:00
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
|
2019-08-09 04:32:40 -05:00
|
|
|
}
|
|
|
|
|
2023-06-01 07:51:16 -05:00
|
|
|
async function getStoryboard (req: express.Request, res: express.Response) {
|
2023-06-06 08:59:51 -05:00
|
|
|
const result = await VideoStoryboardsSimpleFileCache.Instance.getFilePath(req.params.filename)
|
2023-06-01 07:51:16 -05: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 04:32:40 -05:00
|
|
|
async function getVideoCaption (req: express.Request, res: express.Response) {
|
2023-06-06 08:59:51 -05:00
|
|
|
const result = await VideoCaptionsSimpleFileCache.Instance.getFilePath(req.params.filename)
|
2021-05-31 18:36:53 -05:00
|
|
|
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
|
2019-08-09 04:32:40 -05:00
|
|
|
|
2021-02-16 09:25:53 -06:00
|
|
|
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getTorrent (req: express.Request, res: express.Response) {
|
2023-06-06 08:59:51 -05:00
|
|
|
const result = await VideoTorrentsSimpleFileCache.Instance.getFilePath(req.params.filename)
|
2021-05-31 18:36:53 -05:00
|
|
|
if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
|
2021-02-16 09:25:53 -06:00
|
|
|
|
2021-02-18 04:28:00 -06:00
|
|
|
// Torrents still use the old naming convention (video uuid + .torrent)
|
2019-08-09 04:32:40 -05:00
|
|
|
return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
|
|
|
|
}
|