fix(storage): release a deleted journey's photos and a deleted list's images, and keep one bad key from stopping a category

Deleting a journey left its uploaded photos behind for good, and deleting
a list queued neither its cover nor its place images. An object with a key
the store refuses no longer fails every night's pass, and the admin view
keeps the counts of a category skipped for a running backup.
This commit is contained in:
Maurice
2026-11-05 13:02:26 +01:00
parent 402a02a302
commit 9dd58fe32f
26 changed files with 350 additions and 17 deletions
@@ -93,6 +93,7 @@ export async function accommodationsOver(
await createTestJourneyPhotosRepo(conn),
await createTestJourneyEntryPhotosRepo(conn),
await createTestPlacesRepo(conn),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
),
await createTestUnitOfWork(conn),
await createTestDayAssignmentsRepo(conn),
+1
View File
@@ -361,6 +361,7 @@ export async function createPluginRpcHostParts(
await createTestJourneyPhotosRepo(db),
await createTestJourneyEntryPhotosRepo(db),
await createTestPlacesRepo(db),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
);
const atlas = new AtlasService(
await createTestBucketListRepo(db),
@@ -109,6 +109,7 @@ beforeAll(async () => {
await createTestJourneyPhotosRepo(testDb),
await createTestJourneyEntryPhotosRepo(testDb),
await createTestPlacesRepo(testDb),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
),
await createTestUnitOfWork(testDb),
await createTestDayAssignmentsRepo(testDb),
@@ -700,6 +700,24 @@ describe('deleteCollection', () => {
expect(await placesIn(col.id)).toEqual({ n: 0 });
expect(await membersOf(col.id)).toEqual({ n: 0 });
});
it('COLLECTIONS-SVC-040b: deleting a list releases its cover and the images of its saved places (#2924)', async () => {
const owner = createUser(testDb).user;
const col = await svc.createCollection(owner.id, { name: 'Doomed' });
const files = ['list-cover.jpg', 'list-place-a.jpg', 'list-place-b.jpg'].map((name) => {
const filePath = path.join(storageFx.root, name);
fs.writeFileSync(filePath, 'jpeg-bytes');
return filePath;
});
await svc.setCollectionCover(owner.id, col.id, '/uploads/covers/list-cover.jpg');
await svc.savePlace(owner.id, { collection_id: col.id, name: 'A', image_url: '/uploads/places/list-place-a.jpg' });
await svc.savePlace(owner.id, { collection_id: col.id, name: 'B', image_url: '/uploads/places/list-place-b.jpg' });
await svc.savePlace(owner.id, { collection_id: col.id, name: 'No image' });
await svc.deleteCollection(owner.id, col.id);
expect(files.map((file) => fs.existsSync(file))).toEqual([false, false, false]);
});
});
// ── owner_id semantics: member account deletion keeps shared content ─────────
@@ -153,6 +153,7 @@ beforeAll(async () => {
await createTestJourneyPhotosRepo(testDb),
await createTestJourneyEntryPhotosRepo(testDb),
await createTestPlacesRepo(testDb),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
),
makeStorageFixture('').storage,
await accommodationsOver(testDb),
@@ -77,6 +77,7 @@ beforeAll(async () => {
await createTestJourneyPhotosRepo(testDb),
await createTestJourneyEntryPhotosRepo(testDb),
await createTestPlacesRepo(testDb),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
);
// Plan 3g Task 3: JourneyBooksRepository (JB1-JB7), not `dbs` any more.
// task-5-fix-brief constructor-ripple (M1): `UnitOfWork`, so `saveBook`'s
@@ -52,6 +52,7 @@ import {
// ported from the reviewer's probe (scratchpad r3g/probe/zz-r3g-parity-probe.test.ts).
import { todayUtc } from '@trek/shared';
import { GALLERY_CHRONOLOGICAL_ORDER } from '../../helpers/legacy-gallery-order';
import { releaseNothing } from '../../helpers/storage-release';
import type { EntityClass, EntityDTO, FilterQuery, FindOptions, RequiredEntityData } from '@mikro-orm/core';
import { countRows, deleteRows, findRows, insertRow, updateRows } from '../../helpers/factories/rows';
import { DayAssignments } from '../../../src/db/entities/DayAssignments.entity';
@@ -98,6 +99,7 @@ async function pickAll<T extends object, K extends keyof EntityDTO<T>>(
}
let svc: JourneyDomainService;
const release = releaseNothing();
// Plan 3g Task 1's own additions (below, "repositories (R9's parity + mutation
// proofs)" describe block) reach the repositories directly — held here so
// that block doesn't need its own second construction.
@@ -145,6 +147,7 @@ beforeAll(async () => {
// R9, Task 2 only appends to what Task 1's construction already
// established, never re-touches the wrapping mechanism itself.
photosRepoDirect, entryPhotosRepoDirect, placesRepoDirect,
release,
);
});
@@ -540,6 +543,49 @@ describe('deleteJourney', () => {
const row = await one(Journeys, { id: journey.id });
expect(row).toBeDefined();
});
it('JOURNEY-SVC-022b: deleting a journey drops the photo rows only it held and releases their files', async () => {
const { user } = createUser(testDb);
const journey = await svc.createJourney(user.id, { title: 'J' });
const [photo, video] = await svc.uploadGalleryPhotos(journey.id, user.id, [
{ path: 'journey/a.jpg' },
{ path: 'journey/b.mp4', thumbnail: 'journey/b-poster.jpg', mediaType: 'video' },
]);
release.releaseValues.mockClear();
expect(await svc.deleteJourney(journey.id, user.id)).toBe(true);
expect(await one(TrekPhotos, { id: { $in: [photo.photo_id, video.photo_id] } })).toBeUndefined();
expect(release.releaseValues).toHaveBeenCalledTimes(1);
const [values, options] = release.releaseValues.mock.calls[0] as unknown as [Array<string | null>, unknown];
expect(values.filter(Boolean).sort()).toEqual(['journey/a.jpg', 'journey/b-poster.jpg', 'journey/b.mp4']);
expect(options).toEqual({ bare: 'journey' });
});
it('JOURNEY-SVC-022c: a photo another journey or a trip still links keeps its row through the delete', async () => {
const { user } = createUser(testDb);
const journey = await svc.createJourney(user.id, { title: 'J' });
const other = await svc.createJourney(user.id, { title: 'Other' });
const trip = createTrip(testDb, user.id);
const [shared, inTrip, alone] = await svc.uploadGalleryPhotos(journey.id, user.id, [
{ path: 'journey/shared.jpg' },
{ path: 'journey/trip.jpg' },
{ path: 'journey/alone.jpg' },
]);
// The same file uploaded to a second journey reuses its photo row.
const [again] = await svc.uploadGalleryPhotos(other.id, user.id, [{ path: 'journey/shared.jpg' }]);
expect(again.photo_id).toBe(shared.photo_id);
testDb
.prepare('INSERT INTO trip_photos (trip_id, user_id, photo_id) VALUES (?, ?, ?)')
.run(trip.id, user.id, inTrip.photo_id);
expect(await svc.deleteJourney(journey.id, user.id)).toBe(true);
expect(await one(TrekPhotos, { id: shared.photo_id })).toBeDefined();
expect(await one(TrekPhotos, { id: inTrip.photo_id })).toBeDefined();
expect(await one(TrekPhotos, { id: alone.photo_id })).toBeUndefined();
expect(await one(JourneyPhotos, { journey: other.id, photo: shared.photo_id })).toBeDefined();
});
});
// -- Trip management ----------------------------------------------------------
@@ -101,6 +101,7 @@ beforeAll(async () => {
await createTestJourneyPhotosRepo(testDb),
await createTestJourneyEntryPhotosRepo(testDb),
await createTestPlacesRepo(testDb),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
),
new SettingsService(
uow,
@@ -108,6 +108,7 @@ beforeAll(async () => {
await createTestJourneyPhotosRepo(testDb),
await createTestJourneyEntryPhotosRepo(testDb),
await createTestPlacesRepo(testDb),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
);
});
@@ -137,6 +137,7 @@ async function svc(searchNominatim: OsmClient['searchNominatim']): Promise<Place
await createTestJourneyPhotosRepo(testDb),
await createTestJourneyEntryPhotosRepo(testDb),
await createTestPlacesRepo(testDb),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
),
storageFx.storage,
await accommodationsOver(testDb),
@@ -145,7 +145,7 @@ async function makePlacesService(
await createTestJourneysRepo(testDb), await createTestJourneyContributorsRepo(testDb),
await createTestJourneyTripsRepo(testDb), await createTestJourneyEntriesRepo(testDb), await createTestTripsRepo(testDb),
// Plan 3g Task 2 constructor-ripple: JourneyPhotosRepository/JourneyEntryPhotosRepository/PlacesRepository.
await createTestJourneyPhotosRepo(testDb), await createTestJourneyEntryPhotosRepo(testDb), await createTestPlacesRepo(testDb),
await createTestJourneyPhotosRepo(testDb), await createTestJourneyEntryPhotosRepo(testDb), await createTestPlacesRepo(testDb), { releaseValues: async () => 0 } as never,
),
placesStorageFx.storage,
await accommodationsOver(testDb), await createTestUnitOfWork(testDb),
@@ -10,7 +10,7 @@
* an owner marker of another install keeps its hands off; a restore, or a
* database naming another install, resets the switch, the marks, the queue
* and the reports but keeps this install's id, and so does a restore's reset
* file whatever id the database names. RCLS-001 to RCLS-046.
* file whatever id the database names. RCLS-001 to RCLS-047.
*/
import { AppSettings } from '../../../../src/db/entities/AppSettings.entity';
import { AuditLog } from '../../../../src/db/entities/AuditLog.entity';
@@ -342,6 +342,22 @@ describe('StorageReclaimService: mark, then sweep', () => {
expect(again.categories.covers.objects).toBe(5);
expect((await marksOf('covers')).map((mark) => mark.key)).toEqual(['orphan.jpg']);
});
it('RCLS-047: a listed name the storage layer refuses is never judged, so it cannot fail the batch it would sit in', async () => {
// A file put into the folder by hand with a backslash in its name: a Linux
// local backend lists it, but stat and delete refuse the key.
const listOf = storage.list.bind(storage);
vi.spyOn(storage, 'list').mockImplementation(async function* (category, prefix, opts) {
yield* listOf(category, prefix, opts);
if (category === 'covers') yield { key: 'trip\\photo.jpg', size: 5, mtimeMs: 1 };
});
await markedWeekOld(['orphan.jpg']);
expect((await marksOf('covers')).map((mark) => mark.key)).toEqual(['orphan.jpg']);
const report = await reclaim.run({ now: WEEK_LATER });
expect(report.categories.covers).toMatchObject({ objects: 5, deleted: { objects: 1, bytes: 5 }, skipped: null });
expect(fs.existsSync(objectPath('covers', 'orphan.jpg'))).toBe(false);
});
});
describe('StorageReclaimService: a large unreferenced share waits for the admin', () => {
@@ -126,6 +126,7 @@ async function makePlacesService(): Promise<PlacesService> {
await createTestJourneyPhotosRepo(testDb),
await createTestJourneyEntryPhotosRepo(testDb),
await createTestPlacesRepo(testDb),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
),
storage,
await accommodationsOver(testDb),
@@ -222,6 +222,7 @@ beforeAll(async () => {
await createTestJourneyPhotosRepo(testDb),
await createTestJourneyEntryPhotosRepo(testDb),
await createTestPlacesRepo(testDb),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
),
makeStorageFixture('').storage,
await accommodationsOver(testDb, realtime),
@@ -265,6 +265,7 @@ beforeAll(async () => {
await createTestJourneyTripsRepo(testDb), await createTestJourneyEntriesRepo(testDb), await createTestTripsRepo(testDb),
// Plan 3g Task 2 constructor-ripple: JourneyPhotosRepository/JourneyEntryPhotosRepository/PlacesRepository.
await createTestJourneyPhotosRepo(testDb), await createTestJourneyEntryPhotosRepo(testDb), await createTestPlacesRepo(testDb),
{ releaseValues: async () => 0 } as never, // StorageReleaseService
),
makeStorageFixture('').storage,
await accommodationsOver(testDb, realtime), await createTestUnitOfWork(testDb),
@@ -23,6 +23,7 @@ import { Plugins } from '../../../src/db/entities/Plugins.entity';
import { ShareTokens } from '../../../src/db/entities/ShareTokens.entity';
import { TrekPhotos } from '../../../src/db/entities/TrekPhotos.entity';
import { TripMembers } from '../../../src/db/entities/TripMembers.entity';
import { TripPhotos } from '../../../src/db/entities/TripPhotos.entity';
import { Users } from '../../../src/db/entities/Users.entity';
import type { BudgetItemsRepository } from '../../../src/db/repositories/BudgetItems.repository';
import type { BudgetSettlementsRepository } from '../../../src/db/repositories/BudgetSettlements.repository';
@@ -459,4 +460,39 @@ describe('deleteUserCompletely', () => {
await svc.deleteUserCompletely(user.id);
expect(cleanupRelease.releaseValues).toHaveBeenCalledWith(['face.png'], { bare: 'avatars' });
});
it('USER-CLEANUP-012: drops the photo rows only their own journeys held and releases those files', async () => {
const { user: victim } = createUser(testDb, { username: 'photo-victim' });
const { user: other } = createUser(testDb, { username: 'photo-other' });
const ownJourney = await createJourney(victim.id, 'Mine');
const otherJourney = await createJourney(other.id, 'Theirs');
const trip = createTrip(testDb, other.id);
const photo = async (file: string, thumb: string | null = null) => {
const row = { provider: 'local', owner: victim.id, file_path: file, thumbnail_path: thumb };
const id = await insertRow(orm, TrekPhotos, row);
await insertRow(orm, JourneyPhotos, { journey: ownJourney, photo: id, created_at: 0 });
return id;
};
const alone = await photo('journey/alone.jpg', 'journey/alone-poster.jpg');
const inOtherJourney = await photo('journey/shared.jpg');
const inTrip = await photo('journey/trip.jpg');
await insertRow(orm, JourneyPhotos, { journey: otherJourney, photo: inOtherJourney, created_at: 0 });
await insertRow(orm, TripPhotos, { trip: trip.id, user: other.id, photo: inTrip });
await svc.deleteUserCompletely(victim.id);
expect(await findRow(orm, TrekPhotos, { id: alone })).toBeNull();
expect(await findRow(orm, TrekPhotos, { id: inOtherJourney })).not.toBeNull();
expect(await findRow(orm, TrekPhotos, { id: inTrip })).not.toBeNull();
// Every file the gallery named is handed on: the release keeps what another row still names.
type Released = [Array<string | null>, { bare?: string }];
const calls = cleanupRelease.releaseValues.mock.calls as unknown as Released[];
const handed = calls.find(([, options]) => options.bare === 'journey')?.[0] ?? [];
expect(handed.filter(Boolean).sort()).toEqual([
'journey/alone-poster.jpg',
'journey/alone.jpg',
'journey/shared.jpg',
'journey/trip.jpg',
]);
});
});