fix(files): serve a plugin the files a trip keeps while Documents is off

The plugin file surface follows the REST rule: the list and reads keep booking, place, expense, note and message files, a write on a loose file goes through when it attaches it, and uploads and deletes stay open.
This commit is contained in:
Maurice
2026-11-23 15:37:13 +01:00
parent a0d66eb898
commit 4cd3f3d650
5 changed files with 106 additions and 37 deletions
+34 -22
View File
@@ -1,19 +1,21 @@
import { ADDON_IDS } from '../../addons';
import { readEnv } from '../../app-config';
import { Users } from '../../db/entities/Users.entity';
import type { TripFileRow } from '../../db/repositories/TripFiles.repository';
import type { UsersRepository } from '../../db/repositories/Users.repository';
import { PluginGuards, type RpcAddon } from '../../nest-rpc/plugin-guards.service';
import { PluginGuards } from '../../nest-rpc/plugin-guards.service';
import { BadParams, ForbiddenResource } from '../../nest-rpc/rpc-errors';
import { PluginController, PluginMethod } from '../../nest-rpc/rpc-kit/decorators';
import type { PluginRpcContext } from '../../nest-rpc/rpc-kit/types';
import { asPayload, rowIdParam } from '../../nest-rpc/rpc-params';
import { AddonsService } from '../addons/addons.service';
import { isDemoEmail } from '../common/demo';
import { RealtimeService } from '../realtime/realtime.service';
import { StorageService } from '../storage/storage.service';
import { BLOCKED_EXTENSIONS } from './files.constants';
// The read cap is the same number on both byte paths, so the upload side of
// this surface reads it off the service that owns the read side.
import { FilesService, FileContentError, FILE_CONTENT_MAX as CONTENT_MAX } from './files.service';
import { FilesService, FileContentError, FILE_CONTENT_MAX as CONTENT_MAX, attachesFile } from './files.service';
import { InjectRepository } from '@mikro-orm/nestjs';
import { randomUUID } from 'node:crypto';
@@ -34,9 +36,6 @@ type CreateInput = {
reservation_id?: number;
};
/** Every read and write here belongs to the documents addon and follows it per trip. */
const FILES_ADDON: RpcAddon = { addonId: ADDON_IDS.DOCUMENTS, noun: 'documents' };
/**
* The file surface a plugin may reach (#plugins).
*
@@ -49,6 +48,10 @@ const FILES_ADDON: RpcAddon = { addonId: ADDON_IDS.DOCUMENTS, noun: 'documents'
* through the storage layer, so `ctx.files` behaves identically whether the backend
* is local disk, S3, or a mirrored pair. The extension blocklist and size cap still
* run before the put, and a demo user is refused outright.
*
* Documents gates it the way it gates the REST routes: while the trip has it off,
* the list leaves out the files only the file manager shows, and a call on such a
* file is refused unless it attaches the file. Uploads and deletes stay open.
*/
@PluginController()
export class FilesRpc {
@@ -58,26 +61,31 @@ export class FilesRpc {
@InjectRepository(Users) private readonly users: UsersRepository,
private readonly guards: PluginGuards,
private readonly storage: StorageService,
private readonly addons: AddonsService,
) {}
private documentsOn(tripId: number): Promise<boolean> {
return this.addons.isEnabledForTrip(tripId, ADDON_IDS.DOCUMENTS);
}
private async requireReachable(tripId: number, file: TripFileRow, targets: Parameters<typeof attachesFile>[0] = {}) {
if (attachesFile(targets) || (await this.documentsOn(tripId)) || (await this.files.isAttached(file))) return;
throw new ForbiddenResource('the documents addon is disabled');
}
@PluginMethod('files.list', { permission: 'db:read:files' })
async list(params: Record<string, unknown>, ctx: PluginRpcContext): Promise<unknown> {
// Trash excluded, the same view the files tab shows.
return await this.guards.tripRead(
params,
ctx,
async () => this.files.listFiles(rowIdParam(params.tripId, 'tripId'), false),
FILES_ADDON,
);
return await this.guards.tripRead(params, ctx, async () => {
const tripId = rowIdParam(params.tripId, 'tripId');
return this.files.listFiles(tripId, false, !(await this.documentsOn(tripId)));
});
}
@PluginMethod('files.getContent', { permission: 'db:read:files:content' })
getContent(params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
return this.guards.tripRead(
params,
ctx,
() => this.readContent(rowIdParam(params.tripId, 'tripId'), rowIdParam(params.fileId, 'fileId')),
FILES_ADDON,
return this.guards.tripRead(params, ctx, () =>
this.readContent(rowIdParam(params.tripId, 'tripId'), rowIdParam(params.fileId, 'fileId')),
);
}
@@ -89,6 +97,8 @@ export class FilesRpc {
* caused (too large) is BAD_PARAMS, everything else RESOURCE_FORBIDDEN.
*/
private async readContent(tripId: number, fileId: number): Promise<unknown> {
const file = await this.files.getFileById(fileId, tripId);
if (file) await this.requireReachable(tripId, file);
try {
const { name, mimetype, bytes } = await this.files.readContent(tripId, fileId);
return { name, mimetype, size: bytes.length, content_base64: bytes.toString('base64') };
@@ -116,7 +126,7 @@ export class FilesRpc {
if (input.content_base64.length > 14 * 1024 * 1024) {
throw new BadParams('file exceeds the 10MB plugin upload cap');
}
await this.guards.requireTripEdit(tripId, actor, UPLOAD_ACTION, FILES_ADDON);
await this.guards.requireTripEdit(tripId, actor, UPLOAD_ACTION);
return this.writeFile(tripId, input as unknown as CreateInput, actor);
}
@@ -168,10 +178,11 @@ export class FilesRpc {
const tripId = rowIdParam(params.tripId, 'tripId');
const fileId = rowIdParam(params.fileId, 'fileId');
const actor = this.guards.requireActor(ctx, 'file link');
await this.guards.requireTripEdit(tripId, actor, EDIT_ACTION, FILES_ADDON);
if (!(await this.files.getFileById(fileId, tripId)))
throw new ForbiddenResource(`no file ${fileId} on trip ${tripId}`);
await this.guards.requireTripEdit(tripId, actor, EDIT_ACTION);
const file = await this.files.getFileById(fileId, tripId);
if (!file) throw new ForbiddenResource(`no file ${fileId} on trip ${tripId}`);
const opts = asPayload(params.opts) as { reservation_id?: number; assignment_id?: number; place_id?: number };
await this.requireReachable(tripId, file, opts);
// A link target on another trip would otherwise attach this trip's file to it.
const foreign = await this.files.findForeignLinkTarget(tripId, opts);
if (foreign) throw new ForbiddenResource(`${foreign} does not belong to trip ${tripId}`);
@@ -187,7 +198,7 @@ export class FilesRpc {
const tripId = rowIdParam(params.tripId, 'tripId');
const fileId = rowIdParam(params.fileId, 'fileId');
const actor = this.guards.requireActor(ctx, 'file');
await this.guards.requireTripEdit(tripId, actor, EDIT_ACTION, FILES_ADDON);
await this.guards.requireTripEdit(tripId, actor, EDIT_ACTION);
const current = await this.files.getFileById(fileId, tripId);
if (!current) throw new ForbiddenResource(`no file ${fileId} on trip ${tripId}`);
const input = asPayload(params.input) as {
@@ -195,6 +206,7 @@ export class FilesRpc {
place_id?: number | null;
reservation_id?: number | null;
};
await this.requireReachable(tripId, current, input);
const foreign = await this.files.findForeignLinkTarget(tripId, {
reservation_id: input.reservation_id ?? null,
place_id: input.place_id ?? null,
@@ -216,7 +228,7 @@ export class FilesRpc {
const tripId = rowIdParam(params.tripId, 'tripId');
const fileId = rowIdParam(params.fileId, 'fileId');
const actor = this.guards.requireActor(ctx, 'file');
await this.guards.requireTripEdit(tripId, actor, DELETE_ACTION, FILES_ADDON);
await this.guards.requireTripEdit(tripId, actor, DELETE_ACTION);
if (!(await this.files.getFileById(fileId, tripId)))
throw new ForbiddenResource(`no file ${fileId} on trip ${tripId}`);
await this.files.softDeleteFile(fileId);