mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 10:19:35 +02:00
Use promise cache to load remote thumbnails
This commit is contained in:
parent
2b5dfa2fe0
commit
cf069671f4
3 changed files with 47 additions and 18 deletions
|
@ -1,10 +1,11 @@
|
|||
import express from 'express'
|
||||
import { LRUCache } from 'lru-cache'
|
||||
import { Model } from 'sequelize'
|
||||
import { logger } from '@server/helpers/logger'
|
||||
import { CachePromise } from '@server/helpers/promise-cache'
|
||||
import { LRU_CACHE, STATIC_MAX_AGE } from '@server/initializers/constants'
|
||||
import { downloadImageFromWorker } from '@server/lib/worker/parent-process'
|
||||
import { HttpStatusCode } from '@shared/models'
|
||||
import { Model } from 'sequelize'
|
||||
|
||||
type ImageModel = {
|
||||
fileUrl: string
|
||||
|
@ -41,21 +42,9 @@ export abstract class AbstractPermanentFileCache <M extends ImageModel> {
|
|||
return res.sendFile(this.filenameToPathUnsafeCache.get(filename), { maxAge: STATIC_MAX_AGE.SERVER })
|
||||
}
|
||||
|
||||
const image = await this.loadModel(filename)
|
||||
const image = await this.lazyLoadIfNeeded(filename)
|
||||
if (!image) return res.status(HttpStatusCode.NOT_FOUND_404).end()
|
||||
|
||||
if (image.onDisk === false) {
|
||||
if (!image.fileUrl) return res.status(HttpStatusCode.NOT_FOUND_404).end()
|
||||
|
||||
try {
|
||||
await this.downloadRemoteFile(image)
|
||||
} catch (err) {
|
||||
logger.warn('Cannot process remote image %s.', image.fileUrl, { err })
|
||||
|
||||
return res.status(HttpStatusCode.NOT_FOUND_404).end()
|
||||
}
|
||||
}
|
||||
|
||||
const path = image.getPath()
|
||||
this.filenameToPathUnsafeCache.set(filename, path)
|
||||
|
||||
|
@ -66,6 +55,28 @@ export abstract class AbstractPermanentFileCache <M extends ImageModel> {
|
|||
})
|
||||
}
|
||||
|
||||
@CachePromise({
|
||||
keyBuilder: filename => filename
|
||||
})
|
||||
private async lazyLoadIfNeeded (filename: string) {
|
||||
const image = await this.loadModel(filename)
|
||||
if (!image) return undefined
|
||||
|
||||
if (image.onDisk === false) {
|
||||
if (!image.fileUrl) return undefined
|
||||
|
||||
try {
|
||||
await this.downloadRemoteFile(image)
|
||||
} catch (err) {
|
||||
logger.warn('Cannot process remote image %s.', image.fileUrl, { err })
|
||||
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
return image
|
||||
}
|
||||
|
||||
async downloadRemoteFile (image: M) {
|
||||
logger.info('Download remote image %s lazily.', image.fileUrl)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue