fix(demo): a demo instance keeps the files its baseline points at

The hourly reset puts only the database back, so a cover, photo, place image or trip file a visitor deleted in between was missing from the restored rows for good. On a demo instance StorageService.delete now leaves the content categories alone; the caches, the backups and the assistant's attachments are still deleted, and the orphan thumb sweep no longer counts a kept thumb as removed.
This commit is contained in:
Maurice
2026-10-25 00:12:20 +00:00
parent a9fb0fc475
commit 42ed858853
6 changed files with 273 additions and 3 deletions
@@ -9,7 +9,7 @@ import { dbNow } from '../../db/types';
import { logError, logInfo } from '../audit/audit-log.logger';
import { withRequestContext } from '../database/request-context';
import { LlmParseService } from '../llm-parse/llm-parse.service';
import { StorageService } from '../storage/storage.service';
import { ASSISTANT_FILES_PREFIX, StorageService } from '../storage/storage.service';
import type { AgentToolRun } from './assistant-agent';
import { NO_SUCH_ATTACHMENT, NO_TEXT, type AttachedTurn } from './assistant-attachment-tools';
import { AssistantKeepFileService } from './assistant-keep-file.service';
@@ -32,9 +32,10 @@ const DAY_MS = 24 * 60 * 60 * 1000;
/**
* Where the bytes sit: under the trip files' category, which is never served
* by path (`/uploads/files` refuses every request), in a folder of their own.
* Storage names the folder, so a demo instance still deletes these bytes.
*/
const STORAGE_CATEGORY = 'files';
const KEY_PREFIX = 'assistant/';
const KEY_PREFIX = ASSISTANT_FILES_PREFIX;
/** The most text kept of one file; a model is sent a window of it, never all of it. */
const MAX_STORED_TEXT_CHARS = 200_000;
@@ -199,6 +199,7 @@ export class JourneyController {
* The commit has to happen first (the Immich mirror reads the final path), so
* every refusal after it has to take the bytes back out. Nothing sweeps
* orphans, and a loop of rejected POSTs would otherwise just fill the disk.
* A demo instance keeps them all the same (StorageService.delete).
*/
private async discardJourneyUploads(files: Express.Multer.File[]): Promise<void> {
for (const f of files) {
@@ -106,6 +106,8 @@ export class ThumbnailService {
let removed = 0;
for await (const stat of this.storage.list('journey', 'thumbs/')) {
if (!stat.key.endsWith('.jpg') || live.has(stat.key)) continue;
// A demo instance keeps it (StorageService.keepsOnDelete): not reclaimed, so not counted.
if (this.storage.keepsOnDelete('journey', stat.key)) continue;
try {
await this.storage.delete('journey', stat.key);
removed++;
+73 -1
View File
@@ -1,3 +1,5 @@
import { RuntimeEnvService } from '../app-config/runtime-env.service';
import { logDebug } from '../audit/audit-log.logger';
import { contentTypeFor } from './content-type';
import type { ReplicaFailure } from './drivers/mirror.driver';
import { decideRange, isNotModified, validatorsFor, type ServingHeaders, type Validators } from './http-serving';
@@ -33,6 +35,47 @@ interface Resolved {
key: string;
}
/**
* The folder under `files` the assistant keeps the files attached to a
* conversation in (assistant-attachments.service.ts takes it from here). They
* live by a retention of days, so no demo baseline can rely on their bytes, and
* a demo instance deletes them as any other does: attaching is not refused to
* the demo account, and kept bytes of a public visitor's files would stay for good.
*/
export const ASSISTANT_FILES_PREFIX = 'assistant/';
/**
* What a delete does on a demo instance, per category: `true` leaves the
* object where it is. The hourly demo reset (src/demo/demo-reset.ts) puts only
* the database back to its baseline, so a file a visitor deleted in between
* would be missing from the restored rows for good. The categories holding
* content that rows point at are kept; the caches and the backups are deleted
* as on any other instance. A Record, so a new category does not compile until
* it is decided here.
*/
const KEPT_ON_DEMO: Readonly<Record<ServedCategory, boolean>> = {
// Trip files, collab note files and chat images. The assistant's attachments
// under ASSISTANT_FILES_PREFIX are the exception, see keepsOnDelete.
files: true,
// Journey photos, videos, posters, covers and thumbs. A thumb a row records
// is served as it is and never made again, so it stays with its photo.
journey: true,
// Trip and collection covers.
covers: true,
avatars: true,
// Images uploaded for places.
places: true,
// The legacy shared photo folder, served and backed up like the uploads.
photos: true,
// A cache of the photos the map providers hand back, written by every lookup
// and swept nightly. Kept, it would grow without bound on a public demo.
'photos-google': false,
// A cache of Immich and Synology assets that lives an hour and is fetched again on a miss.
'photos-trek': false,
// Never part of the baseline: the admin deletes them and auto-backup rotates them.
backups: false,
};
/**
* The facade all call sites use — and the ONLY entry point: callers address
* storage exclusively as (category, name) on this injected service. They
@@ -47,7 +90,12 @@ interface Resolved {
*/
@Injectable()
export class StorageService {
constructor(private readonly registry: StorageRegistryService) {}
constructor(
private readonly registry: StorageRegistryService,
// Whether DEMO_MODE is on, read on every delete. A hand-built facade (the
// unit suites) reads the same live env the container's provider does.
private readonly runtimeEnv: RuntimeEnvService = new RuntimeEnvService(),
) {}
/** Driver-agnostic global scratch space (data/tmp). */
tempDir(): string {
@@ -90,8 +138,32 @@ export class StorageService {
return (await this.stat(category, name)) !== null;
}
/**
* Whether delete() leaves this object where it is: on a demo instance, one of
* a category KEPT_ON_DEMO keeps, the assistant's attachments excepted. A
* sweep asks first, so it never counts as reclaimed an object that stays.
*/
keepsOnDelete(category: ServedCategory, name: string): boolean {
if (!KEPT_ON_DEMO[category]) return false;
if (category === 'files' && name.startsWith(ASSISTANT_FILES_PREFIX)) return false;
return this.runtimeEnv.isDemoMode();
}
/**
* Idempotent, like the drivers. On a demo instance an object keepsOnDelete()
* names is left alone and the call resolves as if it were gone; a later put()
* under the same name still replaces it. That holds for the bytes a request
* takes back out after a refused upload too: on a demo they stay, as a
* visitor's uploads already do once the hourly reset drops their rows. The
* name is validated first in both modes, so a hostile one is refused either way.
*/
async delete(category: ServedCategory, name: string): Promise<void> {
const { driver, key } = this.resolve(category, name);
if (this.keepsOnDelete(category, name)) {
// The category only: the name can come from a request, and a log line is no place for it.
logDebug(`[storage] demo mode keeps a deleted ${category} object, the hourly reset restores rows that name it`);
return;
}
return driver.delete(key);
}