diff options
author | Chocobozzz <me@florianbigard.com> | 2023-06-19 15:42:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-06-29 10:19:55 +0200 |
commit | cf069671f44a4b03d6d5d34f17160b2253f29654 (patch) | |
tree | cbdab0f8229b20fe435cd9b052374d47aaf8f6f9 /server/lib/files-cache/shared | |
parent | 2b5dfa2fe00a433ff59c4ca922600f3d860e0903 (diff) | |
download | PeerTube-cf069671f44a4b03d6d5d34f17160b2253f29654.tar.gz PeerTube-cf069671f44a4b03d6d5d34f17160b2253f29654.tar.zst PeerTube-cf069671f44a4b03d6d5d34f17160b2253f29654.zip |
Use promise cache to load remote thumbnails
Diffstat (limited to 'server/lib/files-cache/shared')
-rw-r--r-- | server/lib/files-cache/shared/abstract-permanent-file-cache.ts | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/server/lib/files-cache/shared/abstract-permanent-file-cache.ts b/server/lib/files-cache/shared/abstract-permanent-file-cache.ts index 297461035..f990e9872 100644 --- a/server/lib/files-cache/shared/abstract-permanent-file-cache.ts +++ b/server/lib/files-cache/shared/abstract-permanent-file-cache.ts | |||
@@ -1,10 +1,11 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { LRUCache } from 'lru-cache' | 2 | import { LRUCache } from 'lru-cache' |
3 | import { Model } from 'sequelize' | ||
3 | import { logger } from '@server/helpers/logger' | 4 | import { logger } from '@server/helpers/logger' |
5 | import { CachePromise } from '@server/helpers/promise-cache' | ||
4 | import { LRU_CACHE, STATIC_MAX_AGE } from '@server/initializers/constants' | 6 | import { LRU_CACHE, STATIC_MAX_AGE } from '@server/initializers/constants' |
5 | import { downloadImageFromWorker } from '@server/lib/worker/parent-process' | 7 | import { downloadImageFromWorker } from '@server/lib/worker/parent-process' |
6 | import { HttpStatusCode } from '@shared/models' | 8 | import { HttpStatusCode } from '@shared/models' |
7 | import { Model } from 'sequelize' | ||
8 | 9 | ||
9 | type ImageModel = { | 10 | type ImageModel = { |
10 | fileUrl: string | 11 | fileUrl: string |
@@ -41,29 +42,39 @@ export abstract class AbstractPermanentFileCache <M extends ImageModel> { | |||
41 | return res.sendFile(this.filenameToPathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER }) | 42 | return res.sendFile(this.filenameToPathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER }) |
42 | } | 43 | } |
43 | 44 | ||
44 | const image = await this.loadModel(filename) | 45 | const image = await this.lazyLoadIfNeeded(filename) |
45 | if (!image) return res.status(HttpStatusCode.NOT_FOUND_404).end() | 46 | if (!image) return res.status(HttpStatusCode.NOT_FOUND_404).end() |
46 | 47 | ||
48 | const path = image.getPath() | ||
49 | this.filenameToPathUnsafeCache.set(filename, path) | ||
50 | |||
51 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }, (err: any) => { | ||
52 | if (!err) return | ||
53 | |||
54 | this.onServeError({ err, image, next, filename }) | ||
55 | }) | ||
56 | } | ||
57 | |||
58 | @CachePromise({ | ||
59 | keyBuilder: filename => filename | ||
60 | }) | ||
61 | private async lazyLoadIfNeeded (filename: string) { | ||
62 | const image = await this.loadModel(filename) | ||
63 | if (!image) return undefined | ||
64 | |||
47 | if (image.onDisk === false) { | 65 | if (image.onDisk === false) { |
48 | if (!image.fileUrl) return res.status(HttpStatusCode.NOT_FOUND_404).end() | 66 | if (!image.fileUrl) return undefined |
49 | 67 | ||
50 | try { | 68 | try { |
51 | await this.downloadRemoteFile(image) | 69 | await this.downloadRemoteFile(image) |
52 | } catch (err) { | 70 | } catch (err) { |
53 | logger.warn('Cannot process remote image %s.', image.fileUrl, { err }) | 71 | logger.warn('Cannot process remote image %s.', image.fileUrl, { err }) |
54 | 72 | ||
55 | return res.status(HttpStatusCode.NOT_FOUND_404).end() | 73 | return undefined |
56 | } | 74 | } |
57 | } | 75 | } |
58 | 76 | ||
59 | const path = image.getPath() | 77 | return image |
60 | this.filenameToPathUnsafeCache.set(filename, path) | ||
61 | |||
62 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.LAZY_SERVER }, (err: any) => { | ||
63 | if (!err) return | ||
64 | |||
65 | this.onServeError({ err, image, next, filename }) | ||
66 | }) | ||
67 | } | 78 | } |
68 | 79 | ||
69 | async downloadRemoteFile (image: M) { | 80 | async downloadRemoteFile (image: M) { |