fix: reuse sharp instance and UPLOADS_DIR constant in eager processing
This commit is contained in:
@@ -77,13 +77,14 @@ export async function generateAllSizes(uuid, subdir, originalPath) {
|
|||||||
await fs.promises.mkdir(cacheDir, { recursive: true })
|
await fs.promises.mkdir(cacheDir, { recursive: true })
|
||||||
|
|
||||||
const sharp = (await import('sharp')).default
|
const sharp = (await import('sharp')).default
|
||||||
|
const source = sharp(originalPath)
|
||||||
|
|
||||||
for (const width of VALID_WIDTHS) {
|
for (const width of VALID_WIDTHS) {
|
||||||
for (const format of SUPPORTED_FORMATS) {
|
for (const format of SUPPORTED_FORMATS) {
|
||||||
const cacheFileName = `${uuid}_w${width}.${format}`
|
const cacheFileName = `${uuid}_w${width}.${format}`
|
||||||
const cachePath = path.join(CACHE_DIR, cacheSubdir, cacheFileName)
|
const cachePath = path.join(CACHE_DIR, cacheSubdir, cacheFileName)
|
||||||
|
|
||||||
const pipeline = sharp(originalPath).resize(width, null, { withoutEnlargement: true })
|
const pipeline = source.clone().resize(width, null, { withoutEnlargement: true })
|
||||||
const options = format === 'avif' ? { quality: 75, effort: 4 } : { quality: 80 }
|
const options = format === 'avif' ? { quality: 75, effort: 4 } : { quality: 80 }
|
||||||
await pipeline[format](options).toFile(cachePath)
|
await pipeline[format](options).toFile(cachePath)
|
||||||
}
|
}
|
||||||
@@ -97,8 +98,7 @@ export async function generateAllSizes(uuid, subdir, originalPath) {
|
|||||||
* @returns {string} New URL path like `/uploads/<uuid>.webp`
|
* @returns {string} New URL path like `/uploads/<uuid>.webp`
|
||||||
*/
|
*/
|
||||||
export async function convertOriginalToWebp(uuid, subdir) {
|
export async function convertOriginalToWebp(uuid, subdir) {
|
||||||
const uploadsDir = path.join(process.cwd(), 'uploads')
|
const targetDir = subdir ? path.join(UPLOADS_DIR, subdir) : UPLOADS_DIR
|
||||||
const targetDir = subdir ? path.join(uploadsDir, subdir) : uploadsDir
|
|
||||||
|
|
||||||
const originalPath = await findOriginalFile(uuid, subdir)
|
const originalPath = await findOriginalFile(uuid, subdir)
|
||||||
if (!originalPath) {
|
if (!originalPath) {
|
||||||
|
|||||||
Reference in New Issue
Block a user