aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/lazy-static.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/lazy-static.ts')
-rw-r--r--server/controllers/lazy-static.ts36
1 files changed, 26 insertions, 10 deletions
diff --git a/server/controllers/lazy-static.ts b/server/controllers/lazy-static.ts
index 6f71fdb16..9a7dacba0 100644
--- a/server/controllers/lazy-static.ts
+++ b/server/controllers/lazy-static.ts
@@ -4,10 +4,10 @@ import { VideosTorrentCache } from '@server/lib/files-cache/videos-torrent-cache
4import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' 4import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
5import { logger } from '../helpers/logger' 5import { logger } from '../helpers/logger'
6import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants' 6import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants'
7import { actorImagePathUnsafeCache, pushActorImageProcessInQueue } from '../lib/actor-image'
8import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' 7import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache'
8import { actorImagePathUnsafeCache, pushActorImageProcessInQueue } from '../lib/local-actor'
9import { asyncMiddleware } from '../middlewares' 9import { asyncMiddleware } from '../middlewares'
10import { ActorImageModel } from '../models/account/actor-image' 10import { ActorImageModel } from '../models/actor/actor-image'
11 11
12const lazyStaticRouter = express.Router() 12const lazyStaticRouter = express.Router()
13 13
@@ -48,7 +48,7 @@ export {
48 48
49// --------------------------------------------------------------------------- 49// ---------------------------------------------------------------------------
50 50
51async function getActorImage (req: express.Request, res: express.Response) { 51async function getActorImage (req: express.Request, res: express.Response, next: express.NextFunction) {
52 const filename = req.params.filename 52 const filename = req.params.filename
53 53
54 if (actorImagePathUnsafeCache.has(filename)) { 54 if (actorImagePathUnsafeCache.has(filename)) {
@@ -56,10 +56,10 @@ async function getActorImage (req: express.Request, res: express.Response) {
56 } 56 }
57 57
58 const image = await ActorImageModel.loadByName(filename) 58 const image = await ActorImageModel.loadByName(filename)
59 if (!image) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) 59 if (!image) return res.status(HttpStatusCode.NOT_FOUND_404).end()
60 60
61 if (image.onDisk === false) { 61 if (image.onDisk === false) {
62 if (!image.fileUrl) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) 62 if (!image.fileUrl) return res.status(HttpStatusCode.NOT_FOUND_404).end()
63 63
64 logger.info('Lazy serve remote actor image %s.', image.fileUrl) 64 logger.info('Lazy serve remote actor image %s.', image.fileUrl)
65 65
@@ -67,7 +67,7 @@ async function getActorImage (req: express.Request, res: express.Response) {
67 await pushActorImageProcessInQueue({ filename: image.filename, fileUrl: image.fileUrl, type: image.type }) 67 await pushActorImageProcessInQueue({ filename: image.filename, fileUrl: image.fileUrl, type: image.type })
68 } catch (err) { 68 } catch (err) {
69 logger.warn('Cannot process remote actor image %s.', image.fileUrl, { err }) 69 logger.warn('Cannot process remote actor image %s.', image.fileUrl, { err })
70 return res.sendStatus(HttpStatusCode.NOT_FOUND_404) 70 return res.status(HttpStatusCode.NOT_FOUND_404).end()
71 } 71 }
72 72
73 image.onDisk = true 73 image.onDisk = true
@@ -78,26 +78,42 @@ async function getActorImage (req: express.Request, res: express.Response) {
78 const path = image.getPath() 78 const path = image.getPath()
79 79
80 actorImagePathUnsafeCache.set(filename, path) 80 actorImagePathUnsafeCache.set(filename, path)
81 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }) 81
82 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }, (err: any) => {
83 if (!err) return
84
85 // It seems this actor image is not on the disk anymore
86 if (err.status === HttpStatusCode.NOT_FOUND_404 && !image.isOwned()) {
87 logger.error('Cannot lazy serve actor image %s.', filename, { err })
88
89 actorImagePathUnsafeCache.del(filename)
90
91 image.onDisk = false
92 image.save()
93 .catch(err => logger.error('Cannot save new actor image disk state.', { err }))
94 }
95
96 return next(err)
97 })
82} 98}
83 99
84async function getPreview (req: express.Request, res: express.Response) { 100async function getPreview (req: express.Request, res: express.Response) {
85 const result = await VideosPreviewCache.Instance.getFilePath(req.params.filename) 101 const result = await VideosPreviewCache.Instance.getFilePath(req.params.filename)
86 if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) 102 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
87 103
88 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }) 104 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
89} 105}
90 106
91async function getVideoCaption (req: express.Request, res: express.Response) { 107async function getVideoCaption (req: express.Request, res: express.Response) {
92 const result = await VideosCaptionCache.Instance.getFilePath(req.params.filename) 108 const result = await VideosCaptionCache.Instance.getFilePath(req.params.filename)
93 if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) 109 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
94 110
95 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }) 111 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER })
96} 112}
97 113
98async function getTorrent (req: express.Request, res: express.Response) { 114async function getTorrent (req: express.Request, res: express.Response) {
99 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename) 115 const result = await VideosTorrentCache.Instance.getFilePath(req.params.filename)
100 if (!result) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) 116 if (!result) return res.status(HttpStatusCode.NOT_FOUND_404).end()
101 117
102 // Torrents still use the old naming convention (video uuid + .torrent) 118 // Torrents still use the old naming convention (video uuid + .torrent)
103 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER }) 119 return res.sendFile(result.path, { maxAge: STATIC_MAX_AGE.SERVER })