mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
555027f10f
Entry cards and the entry photo grid loaded the provider thumbnail (about 250 px for Immich) and drew it blurry. Photos now have a medium "preview" rendition: Immich's preview, Synology's xl and a local photo's 800 px thumbnail. GET /api/photos/:id/preview serves it behind the same access check, and both share link photo routes now hand a preview on instead of reading every size but thumbnail as the original. The entry grid, the entry covers, the phone feed cards and the share page's large tiles ask for it; small tiles keep the thumbnail. The Synology picker named no size and got the 240 px rendition; its tiles now ask for the medium one.
76 lines
2.9 KiB
TypeScript
76 lines
2.9 KiB
TypeScript
import type { User } from '../../types';
|
|
import { CurrentUser } from '../auth-core/current-user.decorator';
|
|
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
|
|
import { toRowId } from '../common/row-id';
|
|
import { PhotosService } from './photos.service';
|
|
import { Controller, Get, Headers, HttpException, Param, Res, UseGuards } from '@nestjs/common';
|
|
|
|
import type { Response } from 'express';
|
|
|
|
/**
|
|
* /api/photos/:id/{thumbnail,preview,original,info} — global (not trip-scoped) photo
|
|
* access for the memories/journey features. Streaming endpoints write straight
|
|
* to the response via the resolver service.
|
|
*
|
|
* Byte-identical to the legacy Express route (server/src/routes/photos.ts):
|
|
* a row-id guard (400), the canAccessTrekPhoto check (403), then stream or
|
|
* the provider info (404 inside the service / mapped error for info).
|
|
*/
|
|
@Controller('api/photos')
|
|
@UseGuards(JwtAuthGuard)
|
|
export class PhotosController {
|
|
constructor(private readonly photos: PhotosService) {}
|
|
|
|
private async requireAccess(user: User, rawId: string): Promise<number> {
|
|
// toRowId: Number() read '0x1' and '1e0' as photo 1, and 1e20 reached Postgres as an out of range bigint.
|
|
const photoId = toRowId(rawId);
|
|
if (photoId === null) {
|
|
throw new HttpException({ error: 'Invalid photo ID' }, 400);
|
|
}
|
|
if (!(await this.photos.canAccess(user.id, photoId))) {
|
|
throw new HttpException({ error: 'Forbidden' }, 403);
|
|
}
|
|
return photoId;
|
|
}
|
|
|
|
@Get(':id/thumbnail')
|
|
async thumbnail(@CurrentUser() user: User, @Param('id') id: string, @Res() res: Response): Promise<void> {
|
|
const photoId = await this.requireAccess(user, id);
|
|
await this.photos.stream(res, user.id, photoId, 'thumbnail');
|
|
}
|
|
|
|
/**
|
|
* The medium rendition for a large tile (#2874): the provider's preview
|
|
* (Immich `preview`, Synology `xl`), a local photo's 800 px thumbnail. Same
|
|
* access check as the other two.
|
|
*/
|
|
// response-contract-exempt: streams the image bytes through @Res(), there is no JSON body to check.
|
|
@Get(':id/preview')
|
|
async preview(@CurrentUser() user: User, @Param('id') id: string, @Res() res: Response): Promise<void> {
|
|
const photoId = await this.requireAccess(user, id);
|
|
await this.photos.stream(res, user.id, photoId, 'preview');
|
|
}
|
|
|
|
@Get(':id/original')
|
|
async original(
|
|
@CurrentUser() user: User,
|
|
@Param('id') id: string,
|
|
@Res() res: Response,
|
|
@Headers('range') range?: string,
|
|
): Promise<void> {
|
|
const photoId = await this.requireAccess(user, id);
|
|
await this.photos.stream(res, user.id, photoId, 'original', range);
|
|
}
|
|
|
|
@Get(':id/info')
|
|
async info(@CurrentUser() user: User, @Param('id') id: string, @Res() res: Response): Promise<void> {
|
|
const photoId = await this.requireAccess(user, id);
|
|
const result = await this.photos.info(user.id, photoId);
|
|
if ('error' in result) {
|
|
res.status(result.error.status).json({ error: result.error.message });
|
|
return;
|
|
}
|
|
res.json(result.data);
|
|
}
|
|
}
|