1 import cors from 'cors'
2 import express from 'express'
3 import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
4 import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
5 import { logger } from '../helpers/logger'
6 import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
7 import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
8 import { actorImagePathUnsafeCache, pushActorImageProcessInQueue } from '../lib/local-actor'
9 import { asyncMiddleware } from '../middlewares'
10 import { ActorImageModel } from '../models/actor/actor-image'
12 const lazyStaticRouter = express.Router()
14 lazyStaticRouter.use(cors())
17 LAZY_STATIC_PATHS.AVATARS + ':filename',
18 asyncMiddleware(getActorImage)
22 LAZY_STATIC_PATHS.BANNERS + ':filename',
23 asyncMiddleware(getActorImage)
27 LAZY_STATIC_PATHS.PREVIEWS + ':filename',
28 asyncMiddleware(getPreview)
32 LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
33 asyncMiddleware(getVideoCaption)
37 LAZY_STATIC_PATHS.TORRENTS + ':filename',
38 asyncMiddleware(getTorrent)
41 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
51 async function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
52 const filename = req.params.filename
54 if (actorImagePathUnsafeCache.has(filename)) {
55 return res.sendFile(actorImagePathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER })
58 const image = await ActorImageModel.loadByName(filename)
59 if (!image) return res.status(HttpStatusCode.NOT_FOUND_404).end()
61 if (image.onDisk === false) {
62 if (!image.fileUrl) return res.status(HttpStatusCode.NOT_FOUND_404).end()
64 logger.info('Lazy serve remote actor image %s.', image.fileUrl)
67 await pushActorImageProcessInQueue({
68 filename: image.filename,
69 fileUrl: image.fileUrl,
77 logger.warn('Cannot process remote actor image %s.', image.fileUrl, { err })
78 return res.status(HttpStatusCode.NOT_FOUND_404).end()
83 .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
86 const path = image.getPath()
88 actorImagePathUnsafeCache.set(filename, path)
90 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }, (err: any) => {
93 // It seems this actor image is not on the disk anymore
94 if (err.status === HttpStatusCode.NOT_FOUND_404 && !image.isOwned()) {
95 logger.error('Cannot lazy serve actor image %s.', filename, { err })
97 actorImagePathUnsafeCache.del(filename)
101 .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
108 async function getPreview (req: express.Request, res: express.Response) {
109 const result = await VideosPreviewCache.Instance.getFilePath(req.params.filename)
110 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
112 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
115 async function getVideoCaption (req: express.Request, res: express.Response) {
116 const result = await VideosCaptionCache.Instance.getFilePath(req.params.filename)
117 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
119 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
122 async function getTorrent (req: express.Request, res: express.Response) {
123 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
124 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
126 // Torrents still use the old naming convention (video uuid + .torrent)
127 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })