ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lazy-static.ts 4.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import cors from 'cors'
  2. import express from 'express'
  3. import { HttpStatusCode } from '@peertube/peertube-models'
  4. import { CONFIG } from '@server/initializers/config.js'
  5. import { FILES_CACHE, LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants.js'
  6. import {
  7. AvatarPermanentFileCache,
  8. VideoCaptionsSimpleFileCache,
  9. VideoMiniaturePermanentFileCache,
  10. VideoPreviewsSimpleFileCache,
  11. VideoStoryboardsSimpleFileCache,
  12. VideoTorrentsSimpleFileCache
  13. } from '../lib/files-cache/index.js'
  14. import { asyncMiddleware, handleStaticError } from '../middlewares/index.js'
  15. // ---------------------------------------------------------------------------
  16. // Cache initializations
  17. // ---------------------------------------------------------------------------
  18. VideoPreviewsSimpleFileCache.Instance.init(CONFIG.CACHE.PREVIEWS.SIZE, FILES_CACHE.PREVIEWS.MAX_AGE)
  19. VideoCaptionsSimpleFileCache.Instance.init(CONFIG.CACHE.VIDEO_CAPTIONS.SIZE, FILES_CACHE.VIDEO_CAPTIONS.MAX_AGE)
  20. VideoTorrentsSimpleFileCache.Instance.init(CONFIG.CACHE.TORRENTS.SIZE, FILES_CACHE.TORRENTS.MAX_AGE)
  21. VideoStoryboardsSimpleFileCache.Instance.init(CONFIG.CACHE.STORYBOARDS.SIZE, FILES_CACHE.STORYBOARDS.MAX_AGE)
  22. // ---------------------------------------------------------------------------
  23. const lazyStaticRouter = express.Router()
  24. lazyStaticRouter.use(cors())
  25. lazyStaticRouter.use(
  26. LAZY_STATIC_PATHS.AVATARS + ':filename',
  27. asyncMiddleware(getActorImage),
  28. handleStaticError
  29. )
  30. lazyStaticRouter.use(
  31. LAZY_STATIC_PATHS.BANNERS + ':filename',
  32. asyncMiddleware(getActorImage),
  33. handleStaticError
  34. )
  35. lazyStaticRouter.use(
  36. LAZY_STATIC_PATHS.THUMBNAILS + ':filename',
  37. asyncMiddleware(getThumbnail),
  38. handleStaticError
  39. )
  40. lazyStaticRouter.use(
  41. LAZY_STATIC_PATHS.PREVIEWS + ':filename',
  42. asyncMiddleware(getPreview),
  43. handleStaticError
  44. )
  45. lazyStaticRouter.use(
  46. LAZY_STATIC_PATHS.STORYBOARDS + ':filename',
  47. asyncMiddleware(getStoryboard),
  48. handleStaticError
  49. )
  50. lazyStaticRouter.use(
  51. LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
  52. asyncMiddleware(getVideoCaption),
  53. handleStaticError
  54. )
  55. lazyStaticRouter.use(
  56. LAZY_STATIC_PATHS.TORRENTS + ':filename',
  57. asyncMiddleware(getTorrent),
  58. handleStaticError
  59. )
  60. // ---------------------------------------------------------------------------
  61. export {
  62. lazyStaticRouter,
  63. getPreview,
  64. getVideoCaption
  65. }
  66. // ---------------------------------------------------------------------------
  67. const avatarPermanentFileCache = new AvatarPermanentFileCache()
  68. function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
  69. const filename = req.params.filename
  70. return avatarPermanentFileCache.lazyServe({ filename, res, next })
  71. }
  72. // ---------------------------------------------------------------------------
  73. const videoMiniaturePermanentFileCache = new VideoMiniaturePermanentFileCache()
  74. function getThumbnail (req: express.Request, res: express.Response, next: express.NextFunction) {
  75. const filename = req.params.filename
  76. return videoMiniaturePermanentFileCache.lazyServe({ filename, res, next })
  77. }
  78. // ---------------------------------------------------------------------------
  79. async function getPreview (req: express.Request, res: express.Response) {
  80. const result = await VideoPreviewsSimpleFileCache.Instance.getFilePath(req.params.filename)
  81. if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
  82. return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
  83. }
  84. async function getStoryboard (req: express.Request, res: express.Response) {
  85. const result = await VideoStoryboardsSimpleFileCache.Instance.getFilePath(req.params.filename)
  86. if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
  87. return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
  88. }
  89. async function getVideoCaption (req: express.Request, res: express.Response) {
  90. const result = await VideoCaptionsSimpleFileCache.Instance.getFilePath(req.params.filename)
  91. if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
  92. return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
  93. }
  94. async function getTorrent (req: express.Request, res: express.Response) {
  95. const result = await VideoTorrentsSimpleFileCache.Instance.getFilePath(req.params.filename)
  96. if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
  97. return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
  98. }