mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
refactor(memories): serve photo bytes through storage
This commit is contained in:
@@ -63,6 +63,9 @@ const cache = {
|
||||
setInFlight: vi.fn(),
|
||||
put: vi.fn(),
|
||||
};
|
||||
// Local bytes go through the storage facade (slice 3); the resolver only ever
|
||||
// addresses the 'journey' category with the 'journey/' prefix stripped.
|
||||
const storage = { exists: vi.fn(), sendToResponse: vi.fn() };
|
||||
|
||||
const dbs = new DatabaseService(testDb);
|
||||
const repo = new TrekPhotosRepository(dbs);
|
||||
@@ -80,6 +83,7 @@ const svc = new PhotoResolverService(
|
||||
thumbnails as unknown as ThumbnailService,
|
||||
cache as unknown as TrekPhotoCacheService,
|
||||
providers,
|
||||
storage as unknown as import('../../../src/nest/storage/storage.service').StorageService,
|
||||
);
|
||||
|
||||
function makeRes() {
|
||||
@@ -111,6 +115,8 @@ beforeEach(() => {
|
||||
cache.serveFresh.mockReturnValue(false);
|
||||
cache.getInFlight.mockReturnValue(undefined);
|
||||
cache.cacheKey.mockReturnValue('k');
|
||||
storage.exists.mockResolvedValue(false);
|
||||
storage.sendToResponse.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
afterAll(() => testDb.close());
|
||||
@@ -168,6 +174,64 @@ describe('streamPhoto — dispatch', () => {
|
||||
expect(row).toEqual({ thumbnail_path: 'journey/thumbs/abc.jpg', width: 800, height: 600 });
|
||||
});
|
||||
|
||||
it('RESOLVE-014: a local thumbnail hit streams through storage with the immutable headers', async () => {
|
||||
const id = insertPhoto({ provider: 'local', file_path: 'journey/x.jpg', thumbnail_path: 'journey/thumbs/h.jpg' });
|
||||
storage.exists.mockResolvedValue(true);
|
||||
const res = makeRes();
|
||||
|
||||
await svc.streamPhoto(res as never, 1, id, 'thumbnail');
|
||||
|
||||
expect(storage.exists).toHaveBeenCalledWith('journey', 'thumbs/h.jpg');
|
||||
expect(res.set).toHaveBeenCalledWith('Cache-Control', 'public, max-age=86400, immutable');
|
||||
expect(res.set).toHaveBeenCalledWith('X-Content-Type-Options', 'nosniff');
|
||||
expect(storage.sendToResponse).toHaveBeenCalledWith('journey', 'thumbs/h.jpg', res);
|
||||
});
|
||||
|
||||
it('RESOLVE-015: a local original hit streams through storage with the day-long cache header', async () => {
|
||||
const id = insertPhoto({ provider: 'local', file_path: 'journey/x.jpg' });
|
||||
storage.exists.mockResolvedValue(true);
|
||||
const res = makeRes();
|
||||
|
||||
await svc.streamPhoto(res as never, 1, id, 'original');
|
||||
|
||||
expect(res.set).toHaveBeenCalledWith('Cache-Control', 'public, max-age=86400');
|
||||
expect(res.set).toHaveBeenCalledWith('X-Content-Type-Options', 'nosniff');
|
||||
expect(storage.sendToResponse).toHaveBeenCalledWith('journey', 'x.jpg', res);
|
||||
});
|
||||
|
||||
it('RESOLVE-016: a missing thumbnail falls through to the original for images', async () => {
|
||||
const id = insertPhoto({ provider: 'local', file_path: 'journey/x.jpg', thumbnail_path: 'journey/thumbs/h.jpg' });
|
||||
storage.exists.mockResolvedValueOnce(false).mockResolvedValueOnce(true);
|
||||
const res = makeRes();
|
||||
|
||||
await svc.streamPhoto(res as never, 1, id, 'thumbnail');
|
||||
|
||||
expect(storage.sendToResponse).toHaveBeenCalledWith('journey', 'x.jpg', res);
|
||||
});
|
||||
|
||||
it('RESOLVE-017: a video whose recorded poster is gone 404s instead of falling through (#823)', async () => {
|
||||
const id = insertPhoto({ provider: 'local', file_path: 'journey/clip.mp4', thumbnail_path: 'journey/thumbs/t.jpg', media_type: 'video' });
|
||||
storage.exists.mockResolvedValue(false);
|
||||
const res = makeRes();
|
||||
|
||||
await svc.streamPhoto(res as never, 1, id, 'thumbnail');
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(404);
|
||||
expect(res.json).toHaveBeenCalledWith({ error: 'No poster available' });
|
||||
expect(storage.sendToResponse).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('RESOLVE-018: a file_path outside journey/ reads as a local miss without touching storage', async () => {
|
||||
const id = insertPhoto({ provider: 'local', file_path: 'nope/other.jpg' });
|
||||
const res = makeRes();
|
||||
|
||||
await svc.streamPhoto(res as never, 1, id, 'original');
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(404);
|
||||
expect(res.json).toHaveBeenCalledWith({ error: 'File not found' });
|
||||
expect(storage.exists).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('RESOLVE-006: an immich original goes straight to the provider with the range header', async () => {
|
||||
const id = insertPhoto({ provider: 'immich', asset_id: 'a1', owner_id: 5 });
|
||||
const res = makeRes();
|
||||
|
||||
@@ -27,8 +27,13 @@ import { runMigrations } from '../../../src/db/migrations';
|
||||
import { DatabaseService } from '../../../src/nest/database/database.service';
|
||||
import { TrekPhotoCacheService, CACHE_TTL } from '../../../src/nest/memories/trek-photo-cache.service';
|
||||
import { UPLOADS_ROOT } from '../../../src/nest/memories/uploads-root';
|
||||
import { StorageNotFoundError } from '../../../src/nest/storage/storage.types';
|
||||
import type { StorageService } from '../../../src/nest/storage/storage.service';
|
||||
|
||||
const svc = new TrekPhotoCacheService(new DatabaseService(testDb));
|
||||
// serveFresh is the one byte-serving method; the cache's disk internals
|
||||
// (getFresh/put/sweep) stay raw-fs until slice 4.
|
||||
const storage = { sendToResponse: vi.fn().mockResolvedValue(undefined) };
|
||||
const svc = new TrekPhotoCacheService(new DatabaseService(testDb), storage as unknown as StorageService);
|
||||
const CACHE_DIR = path.join(UPLOADS_ROOT, 'photos/trek');
|
||||
const written: string[] = [];
|
||||
|
||||
@@ -112,22 +117,32 @@ describe('put / getFresh', () => {
|
||||
});
|
||||
|
||||
describe('serveFresh', () => {
|
||||
it('CACHE-007: sends the cached file with a one-hour cache header', async () => {
|
||||
it('CACHE-007: sends the cached bytes through storage with a one-hour cache header', async () => {
|
||||
const key = freshKey('serve');
|
||||
await svc.put(key, Buffer.from('bytes'), 'image/webp');
|
||||
const res = { set: vi.fn(), sendFile: vi.fn() };
|
||||
const res = { set: vi.fn() };
|
||||
|
||||
expect(svc.serveFresh(res as never, key)).toBe(true);
|
||||
expect(await svc.serveFresh(res as never, key)).toBe(true);
|
||||
expect(res.set).toHaveBeenCalledWith('Content-Type', 'image/webp');
|
||||
expect(res.set).toHaveBeenCalledWith('Cache-Control', 'public, max-age=3600');
|
||||
expect(res.sendFile).toHaveBeenCalledOnce();
|
||||
expect(storage.sendToResponse).toHaveBeenCalledWith('photos-trek', `${key}.bin`, res);
|
||||
});
|
||||
|
||||
it('CACHE-008: reports a miss without touching the response', () => {
|
||||
const res = { set: vi.fn(), sendFile: vi.fn() };
|
||||
expect(svc.serveFresh(res as never, 'nothing-cached')).toBe(false);
|
||||
it('CACHE-008: reports a miss without touching the response or storage', async () => {
|
||||
storage.sendToResponse.mockClear();
|
||||
const res = { set: vi.fn() };
|
||||
expect(await svc.serveFresh(res as never, 'nothing-cached')).toBe(false);
|
||||
expect(res.set).not.toHaveBeenCalled();
|
||||
expect(res.sendFile).not.toHaveBeenCalled();
|
||||
expect(storage.sendToResponse).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('CACHE-013: a getFresh→send delete race reads as a miss, not a crash', async () => {
|
||||
const key = freshKey('race');
|
||||
await svc.put(key, Buffer.from('bytes'), 'image/webp');
|
||||
storage.sendToResponse.mockRejectedValueOnce(new StorageNotFoundError(`photos/trek/${key}.bin`));
|
||||
const res = { set: vi.fn(), headersSent: false };
|
||||
|
||||
expect(await svc.serveFresh(res as never, key)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -151,7 +166,7 @@ describe('the stampede guard', () => {
|
||||
// Load-bearing: a per-instance map would hand any second instance a private
|
||||
// guard and the dedup would be silently gone. The sweep cron injects the
|
||||
// container singleton now, but the invariant stays pinned.
|
||||
const other = new TrekPhotoCacheService(new DatabaseService(testDb));
|
||||
const other = new TrekPhotoCacheService(new DatabaseService(testDb), storage as unknown as StorageService);
|
||||
const promise = Promise.resolve<Buffer | null>(null);
|
||||
svc.setInFlight('shared-key', promise);
|
||||
expect(other.getInFlight('shared-key')).toBe(promise);
|
||||
|
||||
Reference in New Issue
Block a user