refactor(memories): thumbnail service category-addressed

This commit is contained in:
jubnl
2026-08-17 06:08:04 +02:00
parent 1a2e4d4ef2
commit 364f376b07
3 changed files with 60 additions and 37 deletions
@@ -3,7 +3,6 @@ import { Response } from 'express';
import type { TrekPhoto } from '../../types';
import { decrypt_api_key } from '../common/crypto/apiKeyCrypto';
import { TrekPhotosRepository } from '../photos/trek-photos.repository';
import { UPLOADS_ROOT } from './uploads-root';
import { ThumbnailService } from './thumbnail.service';
import { TrekPhotoCacheService } from './trek-photo-cache.service';
import { fail, success, type AssetInfo, type ServiceResult } from './memories.helpers';
@@ -90,9 +89,7 @@ export class PhotoResolverService {
// Only raster images get a lazily-generated Jimp thumbnail; Jimp can't decode
// video, so a video relies on the poster captured at upload (#823).
if (!thumbRel && !isVideo) {
// Generation stays root-addressed until ThumbnailService becomes
// category-addressed in slice 4; serving below is already storage-backed.
const result = await this.thumbnails.ensureLocalThumbnail(UPLOADS_ROOT, photo.file_path);
const result = await this.thumbnails.ensureLocalThumbnail(photo.file_path);
if (result) {
thumbRel = result.thumbnailRelPath;
this.photos.recordLocalThumbnail(photo.id, thumbRel, result.width, result.height);
+36 -18
View File
@@ -1,55 +1,73 @@
import { Jimp } from 'jimp'
import path from 'path'
import fs from 'fs/promises'
import crypto from 'crypto'
import path from 'node:path'
import crypto from 'node:crypto'
import { Injectable } from '@nestjs/common'
import { ADDON_IDS } from '../../addons'
import { AddonsService } from '../addons/addons.service'
import { StorageService } from '../storage/storage.service'
const THUMB_MAX = 800
const THUMB_QUALITY = 80
/**
* Storage name (category 'journey') of the derived thumbnail for an
* uploads-relative journey photo path ('journey/<file>'). The hash input is
* deliberately the uploads-relative path — the scheme predates the storage
* layer, and changing it would orphan every existing thumbnail.
*/
export function journeyThumbName(originalRelPath: string): string {
const hash = crypto.createHash('sha1').update(originalRelPath).digest('hex').slice(0, 16)
return `thumbs/${hash}.jpg`
}
/**
* Downscaled JPEGs for locally uploaded journey photos. Gated on the journey
* addon, because that is the only surface that uploads them.
* addon, because that is the only surface that uploads them. Category-addressed
* since storage slice 4: originals and thumbs are ('journey', <name>) objects.
*/
@Injectable()
export class ThumbnailService {
constructor(private readonly addons: AddonsService) {}
constructor(
private readonly addons: AddonsService,
private readonly storage: StorageService,
) {}
async ensureLocalThumbnail(
uploadsRoot: string,
originalRelPath: string,
): Promise<{ thumbnailRelPath: string; width: number; height: number } | null> {
if (!this.addons.isAddonEnabled(ADDON_IDS.JOURNEY)) return null
const originalAbs = path.join(uploadsRoot, originalRelPath)
try { await fs.access(originalAbs) } catch { return null }
// The DB stores uploads-relative paths ('journey/<file>'); anything else
// is not a local journey photo and has no thumbnail to derive.
const origName = originalRelPath.startsWith('journey/') ? originalRelPath.slice('journey/'.length) : null
if (!origName) return null
// Deterministic name so concurrent requests don't race on the same photo.
const hash = crypto.createHash('sha1').update(originalRelPath).digest('hex').slice(0, 16)
const thumbRel = `journey/thumbs/${hash}.jpg`
const thumbAbs = path.join(uploadsRoot, thumbRel)
const thumbName = journeyThumbName(originalRelPath)
const thumbRel = `journey/${thumbName}`
try {
const [srcStat, dstStat] = await Promise.all([
fs.stat(originalAbs),
fs.stat(thumbAbs).catch(() => null),
this.storage.stat('journey', origName),
this.storage.stat('journey', thumbName),
])
if (!srcStat) return null
if (dstStat && dstStat.mtimeMs >= srcStat.mtimeMs) {
const img = await Jimp.read(thumbAbs)
const img = await this.storage.withLocalFile('journey', thumbName, (p) => Jimp.read(p))
return { thumbnailRelPath: thumbRel, width: img.bitmap.width, height: img.bitmap.height }
}
await fs.mkdir(path.dirname(thumbAbs), { recursive: true })
// Jimp auto-applies EXIF orientation on read, matching sharp's .rotate() behavior.
const img = await Jimp.read(originalAbs)
const img = await this.storage.withLocalFile('journey', origName, (p) => Jimp.read(p))
const { width: w, height: h } = img.bitmap
if (w > THUMB_MAX || h > THUMB_MAX) {
img.scaleToFit({ w: THUMB_MAX, h: THUMB_MAX })
}
await img.write(thumbAbs as `${string}.jpg`, { quality: THUMB_QUALITY })
// Jimp writes straight to a path, so spool on the category's volume and
// commit through storage for the atomic rename.
const spool = path.join(this.storage.spoolDirFor('journey'), `thumb-${crypto.randomUUID()}.jpg`)
await img.write(spool as `${string}.jpg`, { quality: THUMB_QUALITY })
await this.storage.put('journey', thumbName, { tmpPath: spool })
return { thumbnailRelPath: thumbRel, width: img.bitmap.width, height: img.bitmap.height }
} catch {