Files
TREK/server/src/nest/trips/trips-storage-refs.service.ts
T
Maurice 790fccb92c fix(storage): judge unreferenced files without a brake anyone can trip
The 20 % brake skipped a category and wiped its marks every night once a
fifth of it looked unreferenced. Any user could push a small install over
that line, a backlog from #2924 started braked, and a second install on the
same bucket below the line was not caught at all. It is gone:

- Each pass leaves a trek-owner-<install id> marker at the root of every
  category it judges. A category holding another install's marker is
  skipped as foreign_install, one where the marker cannot be written as
  owner_unknown. Backups leave the markers out.
- The install id lives in app_settings outside the storage.* prefix. The
  switch is stored as that id, so a database from another install reads as
  off, and every reopen (a restore) writes this install's id back, turns
  deletion off and drops the marks, confirmations and reports.
- More than a fifth unreferenced still marks and reports, but deletes only
  after an admin confirmed the count shown (POST .../reclaim/confirm). A
  larger count later waits again.

A skip no longer touches the marks, so a failed listing or lookup does not
restart the week. Check now runs in the background and answers 202; the
admin state says running and keeps the last deleting report apart, so a
report-only check never resets what was freed. A pass that deletes writes a
system audit entry, a pass that stops or fails keeps its partial counts and
marks the rest interrupted, a backend error stops deleting in its category,
and deleting stops for the rest of the pass once the switch is off or a
backup is reading the uploads. The usage scan waits for a pass still
running.

Sources read each category once per pass and answer the pre-delete
re-check with a query narrowed to the keys' plain runs. Bare names keep
their raw spelling (a '#', '?' or trailing blank is part of an upload's
name), and local roots are resolved through their symlinks before the
overlap check. A pass lists only a category's root.
2026-11-01 16:18:08 +01:00

25 lines
1.1 KiB
TypeScript

import { Trips } from '../../db/entities/Trips.entity';
import { likeAny, StorageReclaimSource } from '../storage/storage-reclaim-source.service';
import { EVERY_RECLAIMABLE_CATEGORY } from '../storage/storage-reclaim.types';
import { Injectable } from '@nestjs/common';
/**
* Tells the storage reclaim pass which stored objects a trip still names
* (#2924). `trips.cover_image` is whatever the client sent: usually
* `/uploads/covers/<name>`, but just as well an absolute URL of this instance
* or a path into another category, and a trip copy shares its source's cover.
* So every category is claimed and every spelling is parsed.
*/
@Injectable()
export class TripsStorageRefsService extends StorageReclaimSource {
readonly id = 'trips';
readonly claims = EVERY_RECLAIMABLE_CATEGORY;
protected async storedValues(containing: readonly string[] | null): Promise<Array<string | null | undefined>> {
const rows = await this.em
.getRepository(Trips)
.find(likeAny<Trips>(['cover_image'], containing), { fields: ['cover_image'] });
return rows.map((row) => row.cover_image);
}
}