fix(memories): send share link viewers the Immich preview instead of the original file

A public view of an Immich image now asks for the preview rendition, which Immich re-encodes without the stored metadata, so photo locations no longer go out with share links; owners still get the fullsize original.
This commit is contained in:
Maurice
2026-11-23 15:00:55 +01:00
parent e7e3592d97
commit a604f84899
8 changed files with 66 additions and 7 deletions
+2 -1
View File
@@ -200,6 +200,7 @@ export class JourneyService {
* between them with `provider === 'immich' ? … : trySynology()`, so a third
* provider would have been handed to Synology and 404'd from inside its
* parser. An unregistered provider is now told apart from a failing one.
* It goes to a share link's viewer, so the provider sends the public rendition.
*/
streamProviderAsset(
res: Response,
@@ -209,6 +210,6 @@ export class JourneyService {
): Promise<void> | null {
const backend = this.providers.get(provider);
if (!backend) return null;
return backend.streamAsset(res, ref, kind);
return backend.streamAsset(res, { ...ref, audience: 'public' }, kind);
}
}
+5 -2
View File
@@ -520,7 +520,7 @@ export class ImmichService {
assetId: string,
kind: 'thumbnail' | 'original',
ownerUserId?: number,
opts?: { mediaType?: string | null; range?: string },
opts?: { mediaType?: string | null; range?: string; publicView?: boolean },
): Promise<void> {
const effectiveUserId = ownerUserId ?? userId;
const creds = await this.getImmichCredentials(effectiveUserId);
@@ -548,7 +548,10 @@ export class ImmichService {
cacheControl = 'private, max-age=3600';
timeout = undefined;
} else {
url = `${creds.immich_url}/api/assets/${assetId}/thumbnail?size=fullsize`;
// Without a generated fullsize rendition Immich answers with the original file, GPS and all;
// its preview is re-encoded without the metadata, so that is what a share link gets.
const size = opts?.publicView ? 'preview' : 'fullsize';
url = `${creds.immich_url}/api/assets/${assetId}/thumbnail?size=${size}`;
timeout = 30000;
}
@@ -25,6 +25,8 @@ export interface PhotoAssetRef {
mediaType?: string | null;
/** Forwarded Range header, for seeking in a video. */
range?: string;
/** Who the asset goes to; a share link's viewer gets an image rendered without the stored metadata. */
audience?: 'owner' | 'public';
}
export interface PhotoBytes {
@@ -105,8 +105,9 @@ export class PhotoResolverService {
/**
* A public share's view. A local file goes out as its location-free copy
* (PublicMediaService) and never as stored; one that cannot be cleaned falls
* back to the thumbnail TREK re-encoded itself, else a 404. Provider assets
* stream as for an owner.
* back to the thumbnail TREK re-encoded itself, else a 404. A provider is
* told the asset goes to the public, so it can send a rendition without the
* stored location in place of the original file.
*/
async streamPublicPhoto(
res: Response,
@@ -215,7 +216,7 @@ export class PhotoResolverService {
);
return;
}
await provider.streamAsset(res, { ...ref, range }, kind);
await provider.streamAsset(res, { ...ref, range, audience }, kind);
}
/**
@@ -23,6 +23,7 @@ export class ImmichPhotoProvider implements PhotoProvider {
return this.immich.streamImmichAsset(res, ref.userId, ref.assetId, kind, ref.ownerId, {
mediaType: ref.mediaType,
range: ref.range,
publicView: ref.audience === 'public',
});
}
+24 -1
View File
@@ -16,6 +16,7 @@ import { JourneyMapService } from '../../src/nest/journey/journey-map.service';
import { JourneyShareService } from '../../src/nest/journey/journey-share.service';
import { JourneyTracksMcp } from '../../src/nest/journey/journey-tracks.mcp';
import { JourneyModule } from '../../src/nest/journey/journey.module';
import { ImmichService } from '../../src/nest/memories/immich.service';
import { PhotoCaptureBackfillService } from '../../src/nest/memories/photo-capture-backfill.service';
import { StorageService } from '../../src/nest/storage/storage.service';
import { createTestMikroOrmModule } from '../helpers/test-orm';
@@ -117,7 +118,9 @@ const { jsvc } = vi.hoisted(() => ({
// journey hears about it after a provider add, never after an upload (#1587).
const { backfillRun, backfillSchedule } = vi.hoisted(() => ({ backfillRun: vi.fn(), backfillSchedule: vi.fn() }));
const { sharesvc } = vi.hoisted(() => ({ sharesvc: { getPublicJourney: vi.fn() } }));
const { sharesvc } = vi.hoisted(() => ({
sharesvc: { getPublicJourney: vi.fn(), validateShareTokenForAsset: vi.fn() },
}));
// What the share service answers for a link with every section on: the route
// answers under a strict @ResponseContract, so this is also the allow-list.
@@ -688,6 +691,26 @@ describe('Journey e2e (real auth guard + temp SQLite)', () => {
expect(missing.body).toEqual({ error: 'Not found' });
});
it('an Immich original behind an old share link is asked for as the public rendition', async () => {
sharesvc.validateShareTokenForAsset.mockResolvedValueOnce({ ownerId: 1 });
const stream = vi.mocked(ImmichService.prototype.streamImmichAsset);
stream.mockImplementationOnce(async (res) => {
res.status(204).end();
});
const res = await request(server).get('/api/public/journey/tok/photo/immich/a1/1/original');
expect(res.status).toBe(204);
expect(stream).toHaveBeenCalledWith(
expect.anything(),
1,
'a1',
'original',
1,
expect.objectContaining({ publicView: true }),
);
});
// ── GPX tracks: the route and get_journey_tracks read the same service ────
const TRACK = {
@@ -973,6 +973,23 @@ describe('streamImmichAsset', () => {
expect(url).toContain('/api/assets/a1/thumbnail');
expect((init.headers as Record<string, string>)['x-api-key']).toBe('owner-key');
});
it('IMMICH-041e: a public view of an image asks for the preview rendition, an owner and a video keep theirs', async () => {
await seedUser(13, 'https://immich.example', 'owner-key');
safeFetch.mockResolvedValue(upstream({ contentType: 'image/jpeg', body: 'bytes' }));
await svc.streamImmichAsset(makeRes() as never, USER, 'a1', 'original', 13, { publicView: true });
await svc.streamImmichAsset(makeRes() as never, USER, 'a1', 'original', 13);
await svc.streamImmichAsset(makeRes() as never, USER, 'v1', 'original', 13, {
mediaType: 'video',
publicView: true,
});
const urls = safeFetch.mock.calls.map((call) => String(call[0]));
expect(urls[0]).toContain('/api/assets/a1/thumbnail?size=preview');
expect(urls[1]).toContain('/api/assets/a1/thumbnail?size=fullsize');
expect(urls[2]).toContain('/api/assets/v1/video/playback');
});
});
describe('collectAlbumSelection', () => {
@@ -209,6 +209,17 @@ describe('streamPublicPhoto: what a share link gets', () => {
expect(res.set).not.toHaveBeenCalled();
});
it('RESOLVE-035: an immich original is asked for as the public rendition, and an owner still gets the original', async () => {
const id = await insertPhoto({ provider: 'immich', asset_id: 'a1', owner_id: 5 });
const res = makeRes();
await svc.streamPublicPhoto(res as never, 5, id, 'original');
await svc.streamPhoto(res as never, 5, id, 'original');
const views = immich.streamImmichAsset.mock.calls.map((call) => (call[5] as { publicView?: boolean }).publicView);
expect(views).toEqual([true, false]);
});
it('RESOLVE-034: an owner still gets the stored original', async () => {
const id = await insertPhoto({ provider: 'local', file_path: 'journey/x.jpg' });
storage.exists.mockResolvedValue(true);