forked from Yara724/api
Fix registrar signing, reviewer re-entry, replacement evidence freshness, owner decision and rating races, multipart objections, vehicle serialization and product reporting. Validate uploaded media with FFmpeg, serve evidence through expiring signed links, and add targeted regressions plus a dry-run repair for prematurely completed visit cases.
76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import {
|
|
CallHandler,
|
|
Controller,
|
|
ExecutionContext,
|
|
Get,
|
|
Injectable,
|
|
Module,
|
|
NestInterceptor,
|
|
Param,
|
|
Query,
|
|
Res,
|
|
ForbiddenException,
|
|
} from "@nestjs/common";
|
|
import { APP_INTERCEPTOR } from "@nestjs/core";
|
|
import {
|
|
EvidenceAccessService,
|
|
evidenceRequestScope,
|
|
} from "./evidence-access.service";
|
|
export { EvidenceAccessService } from "./evidence-access.service";
|
|
import { realpath } from "node:fs/promises";
|
|
import { resolve, sep } from "node:path";
|
|
import { Response } from "express";
|
|
import { map, Observable } from "rxjs";
|
|
|
|
@Injectable()
|
|
class EvidenceLinksInterceptor implements NestInterceptor {
|
|
constructor(private readonly access: EvidenceAccessService) {}
|
|
intercept(context: ExecutionContext, next: CallHandler) {
|
|
const req = context.switchToHttp().getRequest();
|
|
if (!req.user?.sub) return next.handle();
|
|
return new Observable((observer) =>
|
|
evidenceRequestScope.run(true, () =>
|
|
next
|
|
.handle()
|
|
.pipe(map((value) => this.access.project(value)))
|
|
.subscribe(observer),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
@Controller("files")
|
|
class EvidenceController {
|
|
constructor(private readonly access: EvidenceAccessService) {}
|
|
@Get("*path")
|
|
async read(
|
|
@Param("path") raw: string | string[],
|
|
@Query("expires") expires: string,
|
|
@Query("signature") signature: string,
|
|
@Res() res: Response,
|
|
) {
|
|
const path = Array.isArray(raw) ? raw.join("/") : raw;
|
|
this.access.verify(path, expires, signature);
|
|
const root = await realpath(resolve("files"));
|
|
let target: string;
|
|
try {
|
|
target = await realpath(resolve(root, path));
|
|
} catch {
|
|
res.status(404).end();
|
|
return;
|
|
}
|
|
if (!target.startsWith(root + sep)) throw new ForbiddenException();
|
|
res.setHeader("Cache-Control", "private, no-store");
|
|
res.setHeader("X-Content-Type-Options", "nosniff");
|
|
// sendFile retains Range/HEAD support needed by video players.
|
|
res.sendFile(target);
|
|
}
|
|
}
|
|
@Module({
|
|
controllers: [EvidenceController],
|
|
providers: [
|
|
EvidenceAccessService,
|
|
{ provide: APP_INTERCEPTOR, useClass: EvidenceLinksInterceptor },
|
|
],
|
|
})
|
|
export class EvidenceAccessModule {}
|