mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
79 lines
3.0 KiB
TypeScript
79 lines
3.0 KiB
TypeScript
import { Public } from '../auth-core/public.decorator';
|
|
import { contentDisposition } from '../common/content-disposition';
|
|
import { StorageService } from '../storage/storage.service';
|
|
import { FilesService } from './files.service';
|
|
import { Controller, Get, HttpException, Param, Req, Res } from '@nestjs/common';
|
|
|
|
import type { Request, Response } from 'express';
|
|
import path from 'path';
|
|
|
|
/**
|
|
* GET /api/trips/:tripId/files/:id/download — authenticated file download.
|
|
*
|
|
* Deliberately NOT behind the JwtAuthGuard: it accepts a cookie, a Bearer header
|
|
* OR a one-shot `?token=` query param (so links can be opened directly), all via
|
|
* the legacy authenticateDownload helper. Byte-identical to the legacy route:
|
|
* 401 token, 404 trip/file, .pkpass served inline for Wallet. Bytes come from
|
|
* storage.sendToResponse, whose local branch is the root-relative res.sendFile
|
|
* form this route needs under the ExpressAdapter.
|
|
*/
|
|
@Public('authenticates itself: the download link carries a short-lived ?token the client mints')
|
|
@Controller('api/trips/:tripId/files')
|
|
export class FilesDownloadController {
|
|
constructor(
|
|
private readonly files: FilesService,
|
|
private readonly storage: StorageService,
|
|
) {}
|
|
|
|
// response-contract-exempt: streams the file through @Res(), there is no JSON body to check.
|
|
@Get(':id/download')
|
|
async download(
|
|
@Req() req: Request,
|
|
@Res() res: Response,
|
|
@Param('tripId') tripId: string,
|
|
@Param('id') id: string,
|
|
): Promise<void> {
|
|
const auth = await this.files.authenticateDownload(req);
|
|
|
|
const trip = await this.files.verifyTripAccess(tripId, auth.userId);
|
|
if (!trip) {
|
|
throw new HttpException({ error: 'Trip not found' }, 404);
|
|
}
|
|
|
|
const file = await this.files.getFileById(id, tripId);
|
|
if (!file) {
|
|
throw new HttpException({ error: 'File not found' }, 404);
|
|
}
|
|
|
|
// basename() tolerates a stray prefixed row (legacy rows sometimes stored
|
|
// a relative path); the storage layer's key validation refuses the rest.
|
|
const name = path.basename(file.filename);
|
|
if (!(await this.storage.exists('files', name).catch(() => false))) {
|
|
throw new HttpException({ error: 'File not found' }, 404);
|
|
}
|
|
|
|
// Serve Apple Wallet passes inline with the canonical MIME type so Safari
|
|
// (iOS/macOS) hands them to Wallet instead of downloading as a blob. A
|
|
// `.pkpasses` bundle (a ZIP of multiple passes) is a distinct type with its
|
|
// own plural MIME type — without it Wallet won't offer to add the passes.
|
|
const ext = path.extname(name).toLowerCase();
|
|
const walletMime =
|
|
ext === '.pkpass'
|
|
? 'application/vnd.apple.pkpass'
|
|
: ext === '.pkpasses'
|
|
? 'application/vnd.apple.pkpasses'
|
|
: null;
|
|
await this.storage.sendToResponse(
|
|
'files',
|
|
name,
|
|
res,
|
|
walletMime
|
|
? {
|
|
contentType: walletMime,
|
|
disposition: contentDisposition(path.basename(file.original_name || name), 'inline'),
|
|
}
|
|
: undefined,
|
|
);
|
|
}
|
|
}
|