diff options
Diffstat (limited to 'server/controllers/lazy-static.ts')
-rw-r--r-- | server/controllers/lazy-static.ts | 36 |
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 | |||
4 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' | 4 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' |
5 | import { logger } from '../helpers/logger' | 5 | import { logger } from '../helpers/logger' |
6 | import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants' | 6 | import { LAZY_STATIC_PATHS, STATIC_MAX_AGE } from '../initializers/constants' |
7 | import { actorImagePathUnsafeCache, pushActorImageProcessInQueue } from '../lib/actor-image' | ||
8 | import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' | 7 | import { VideosCaptionCache, VideosPreviewCache } from '../lib/files-cache' |
8 | import { actorImagePathUnsafeCache, pushActorImageProcessInQueue } from '../lib/local-actor' | ||
9 | import { asyncMiddleware } from '../middlewares' | 9 | import { asyncMiddleware } from '../middlewares' |
10 | import { ActorImageModel } from '../models/account/actor-image' | 10 | import { ActorImageModel } from '../models/actor/actor-image' |
11 | 11 | ||
12 | const lazyStaticRouter = express.Router() | 12 | const lazyStaticRouter = express.Router() |
13 | 13 | ||
@@ -48,7 +48,7 @@ export { | |||
48 | 48 | ||
49 | // --------------------------------------------------------------------------- | 49 | // --------------------------------------------------------------------------- |
50 | 50 | ||
51 | async function getActorImage (req: express.Request, res: express.Response) { | 51 | async 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 | ||
84 | async function getPreview (req: express.Request, res: express.Response) { | 100 | async 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 | ||
91 | async function getVideoCaption (req: express.Request, res: express.Response) { | 107 | async 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 | ||
98 | async function getTorrent (req: express.Request, res: express.Response) { | 114 | async 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 }) |