lazy-static.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import * as cors from 'cors'
  2. import * as express from 'express'
  3. import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
  4. import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
  5. import { asyncMiddleware } from '../middlewares'
  6. import { AvatarModel } from '../models/avatar/avatar'
  7. import { logger } from '../helpers/logger'
  8. import { avatarPathUnsafeCache, pushAvatarProcessInQueue } from '../lib/avatar'
  9. import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
  10. const lazyStaticRouter = express.Router()
  11. lazyStaticRouter.use(cors())
  12. lazyStaticRouter.use(
  13. LAZY_STATIC_PATHS.AVATARS + ':filename',
  14. asyncMiddleware(getAvatar)
  15. )
  16. lazyStaticRouter.use(
  17. LAZY_STATIC_PATHS.PREVIEWS + ':uuid.jpg',
  18. asyncMiddleware(getPreview)
  19. )
  20. lazyStaticRouter.use(
  21. LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':videoId-:captionLanguage([a-z]+).vtt',
  22. asyncMiddleware(getVideoCaption)
  23. )
  24. // ---------------------------------------------------------------------------
  25. export {
  26. lazyStaticRouter,
  27. getPreview,
  28. getVideoCaption
  29. }
  30. // ---------------------------------------------------------------------------
  31. async function getAvatar (req: express.Request, res: express.Response) {
  32. const filename = req.params.filename
  33. if (avatarPathUnsafeCache.has(filename)) {
  34. return res.sendFile(avatarPathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER })
  35. }
  36. const avatar = await AvatarModel.loadByName(filename)
  37. if (!avatar) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  38. if (avatar.onDisk === false) {
  39. if (!avatar.fileUrl) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  40. logger.info('Lazy serve remote avatar image %s.', avatar.fileUrl)
  41. try {
  42. await pushAvatarProcessInQueue({ filename: avatar.filename, fileUrl: avatar.fileUrl })
  43. } catch (err) {
  44. logger.warn('Cannot process remote avatar %s.', avatar.fileUrl, { err })
  45. return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  46. }
  47. avatar.onDisk = true
  48. avatar.save()
  49. .catch(err => logger.error('Cannot save new avatar disk state.', { err }))
  50. }
  51. const path = avatar.getPath()
  52. avatarPathUnsafeCache.set(filename, path)
  53. return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
  54. }
  55. async function getPreview (req: express.Request, res: express.Response) {
  56. const result = await VideosPreviewCache.Instance.getFilePath(req.params.uuid)
  57. if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  58. return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
  59. }
  60. async function getVideoCaption (req: express.Request, res: express.Response) {
  61. const result = await VideosCaptionCache.Instance.getFilePath({
  62. videoId: req.params.videoId,
  63. language: req.params.captionLanguage
  64. })
  65. if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
  66. return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })
  67. }