1 import cors from 'cors'
2 import express from 'express'
3 import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache'
4 import { MActorImage } from '@server/types/models'
5 import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
6 import { logger } from '../helpers/logger'
7 import { ACTOR_IMAGES_SIZE, LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
8 import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
9 import { actorImagePathUnsafeCache, downloadActorImageFromWorker } from '../lib/local-actor'
10 import { asyncMiddleware, handleStaticError } from '../middlewares'
11 import { ActorImageModel } from '../models/actor/actor-image'
13 const lazyStaticRouter = express.Router()
15 lazyStaticRouter.use(cors())
18 LAZY_STATIC_PATHS.AVATARS + ':filename',
19 asyncMiddleware(getActorImage),
24 LAZY_STATIC_PATHS.BANNERS + ':filename',
25 asyncMiddleware(getActorImage),
30 LAZY_STATIC_PATHS.PREVIEWS + ':filename',
31 asyncMiddleware(getPreview),
36 LAZY_STATIC_PATHS.VIDEO_CAPTIONS + ':filename',
37 asyncMiddleware(getVideoCaption),
42 LAZY_STATIC_PATHS.TORRENTS + ':filename',
43 asyncMiddleware(getTorrent),
47 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 async function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
58 const filename = req.params.filename
60 if (actorImagePathUnsafeCache.has(filename)) {
61 return res.sendFile(actorImagePathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER })
64 const image = await ActorImageModel.loadByName(filename)
65 if (!image) return res.status(HttpStatusCode.NOT_FOUND_404).end()
67 if (image.onDisk === false) {
68 if (!image.fileUrl) return res.status(HttpStatusCode.NOT_FOUND_404).end()
70 logger.info('Lazy serve remote actor image %s.', image.fileUrl)
73 await downloadActorImageFromWorker({
74 filename: image.filename,
75 fileUrl: image.fileUrl,
76 size: getActorImageSize(image),
80 logger.warn('Cannot process remote actor image %s.', image.fileUrl, { err })
81 return res.status(HttpStatusCode.NOT_FOUND_404).end()
86 .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
89 const path = image.getPath()
91 actorImagePathUnsafeCache.set(filename, path)
93 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }, (err: any) => {
96 // It seems this actor image is not on the disk anymore
97 if (err.status === HttpStatusCode.NOT_FOUND_404 && !image.isOwned()) {
98 logger.error('Cannot lazy serve actor image %s.', filename, { err })
100 actorImagePathUnsafeCache.delete(filename)
104 .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
111 function getActorImageSize (image: MActorImage): { width: number, height: number } {
112 if (image.width && image.height) {
114 height: image.height,
119 return ACTOR_IMAGES_SIZE[image.type][0]
122 async function getPreview (req: express.Request, res: express.Response) {
123 const result = await VideosPreviewCache.Instance.getFilePath(req.params.filename)
124 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
126 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
129 async function getVideoCaption (req: express.Request, res: express.Response) {
130 const result = await VideosCaptionCache.Instance.getFilePath(req.params.filename)
131 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
133 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
136 async function getTorrent (req: express.Request, res: express.Response) {
137 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
138 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
140 // Torrents still use the old naming convention (video uuid + .torrent)
141 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })