mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 02:34:21 +00:00
fix(storage): stop deleting covers, place images and journey photos another row may still show
Replacing a trip or list cover, changing or deleting a place or saved-place image and deleting a journey photo removed the bytes at once, without asking whether a trip copy, a journey, a saved place or another photo row still named them (#2924). Those paths now leave the object in place and the reclaim job removes it once no row names it. A journey photo the user deletes takes its local photo row with it when nothing else links it, so its bytes become reclaimable instead of being kept by a row nobody sees. A place image copied from a trip file is still dropped at once when the update that would name it fails: that copy was never anyone else's. Size and warning baselines follow the shrunk files.
This commit is contained in:
@@ -200,15 +200,8 @@ export class TripsController {
|
||||
throw new HttpException({ error: 'Could not save the selected cover image' }, 502);
|
||||
}
|
||||
}
|
||||
const oldCover =
|
||||
body.cover_image !== undefined
|
||||
? ((await this.trips.getRaw(id)) as { cover_image: string | null } | undefined)?.cover_image
|
||||
: undefined;
|
||||
try {
|
||||
const result = await this.trips.update(id, user.id, body, user.role);
|
||||
if (body.cover_image !== undefined && body.cover_image !== oldCover) {
|
||||
await this.trips.deleteOldCover(oldCover);
|
||||
}
|
||||
if (Object.keys(result.changes).length > 0) {
|
||||
await this.audit.writeAudit({
|
||||
userId: user.id,
|
||||
@@ -264,8 +257,9 @@ export class TripsController {
|
||||
}
|
||||
// Commit the spooled upload to its final storage location (atomic
|
||||
// same-volume rename) before anything references the final path.
|
||||
// The replaced cover stays: a trip copy or a journey may show the same
|
||||
// object, and the storage reclaim job removes it once nothing names it.
|
||||
await this.storage.put('covers', file.filename, { tmpPath: file.path });
|
||||
await this.trips.deleteOldCover(trip.cover_image);
|
||||
const coverUrl = `/uploads/covers/${file.filename}`;
|
||||
await this.trips.updateCoverImage(id, coverUrl);
|
||||
return { cover_image: coverUrl };
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import path from 'path';
|
||||
import { EntityManager } from '@mikro-orm/core';
|
||||
import { Trips } from '../../db/entities/Trips.entity';
|
||||
import { Days } from '../../db/entities/Days.entity';
|
||||
@@ -624,17 +623,6 @@ export class TripsService {
|
||||
|
||||
// ── Cover image ───────────────────────────────────────────────────────────
|
||||
|
||||
async deleteOldCover(coverImage: string | null | undefined): Promise<void> {
|
||||
if (!coverImage) return;
|
||||
// cover_image is client-supplied, so treat it as untrusted: covers are flat
|
||||
// filenames in the 'covers' category — basename() confines the delete to
|
||||
// it, and central key validation rejects anything hostile (swallowed like
|
||||
// the old containment guard; an external https URL is likewise a no-op).
|
||||
await this.storage.delete('covers', path.basename(coverImage)).catch(() => {
|
||||
/* external URL or already gone */
|
||||
});
|
||||
}
|
||||
|
||||
/** TP35 — `TripsRepository.setCoverImage`. */
|
||||
async updateCoverImage(tripId: string | number, coverUrl: string): Promise<void> {
|
||||
await this.tripsRepo.setCoverImage(Number(tripId), coverUrl); // safe: only called after `getRaw`/`getOwner` matched the same id through the raw-bind seam
|
||||
|
||||
Reference in New Issue
Block a user