forked from mtusi2026/mtusi2026lab4
feat: add attachment download and message attachment links
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
Res,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { FastifyRequest } from 'fastify';
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { Readable } from 'stream';
|
||||
import { AuthGuard } from '../auth/auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
@@ -41,4 +43,37 @@ export class AttachmentsController {
|
||||
});
|
||||
return toAttachmentResponse(attachment);
|
||||
}
|
||||
|
||||
@Get('api/v1/attachments/:attachmentId')
|
||||
async metadata(
|
||||
@Param('attachmentId') attachmentId: string,
|
||||
@CurrentUser() user: User,
|
||||
): Promise<AttachmentResponse> {
|
||||
return toAttachmentResponse(
|
||||
await this.attachments.getVisible(attachmentId, user.id),
|
||||
);
|
||||
}
|
||||
|
||||
@Get('api/v1/attachments/:attachmentId/content')
|
||||
async content(
|
||||
@Param('attachmentId') attachmentId: string,
|
||||
@Query('variant') variant: unknown,
|
||||
@CurrentUser() user: User,
|
||||
@Res() reply: FastifyReply,
|
||||
): Promise<void> {
|
||||
const { attachment, content } = await this.attachments.openContent(
|
||||
attachmentId,
|
||||
user.id,
|
||||
variant,
|
||||
);
|
||||
await reply
|
||||
.header('content-type', attachment.contentType)
|
||||
.header('content-length', attachment.sizeBytes)
|
||||
.header('x-content-type-options', 'nosniff')
|
||||
.header(
|
||||
'content-disposition',
|
||||
`inline; filename*=UTF-8''${encodeURIComponent(attachment.filename)}`,
|
||||
)
|
||||
.send(content);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import { Readable } from 'stream';
|
||||
import { assertChatMember } from '../chats/chat-access';
|
||||
import { codePointLength } from '../common/dto/code-point-length.validator';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { validate as isUuid } from '../common/uuid';
|
||||
import { isDatabaseUnavailable, PrismaService } from '../db/prisma.service';
|
||||
import { S3Service } from '../storage/s3.service';
|
||||
import { assertValidImage, isSupportedContentType } from './image-validation';
|
||||
@@ -17,6 +18,7 @@ const TEMP_PREFIX = 'messenger-upload-';
|
||||
const STALE_TEMP_MS = 60 * 60 * 1000;
|
||||
const MAX_FILENAME_CODE_POINTS = 255;
|
||||
const FORBIDDEN_FILENAME = /[/\\\p{Cc}\p{Zl}\p{Zp}]/u;
|
||||
const KNOWN_VARIANTS = new Set(['thumbnail', 'crop', 'optimized']);
|
||||
|
||||
export interface UploadInput {
|
||||
chatId: string;
|
||||
@@ -65,6 +67,32 @@ export class AttachmentsService implements OnModuleInit {
|
||||
}
|
||||
}
|
||||
|
||||
async getVisible(attachmentId: string, userId: string): Promise<Attachment> {
|
||||
if (!isUuid(attachmentId)) {
|
||||
throw new ApiError('invalid_request', 'attachmentId must be a valid UUID');
|
||||
}
|
||||
const row = await this.prisma.attachment.findFirst({
|
||||
where: {
|
||||
id: attachmentId,
|
||||
chat: { members: { some: { userId } } },
|
||||
},
|
||||
});
|
||||
if (!row) {
|
||||
throw new ApiError('not_found', 'attachment not found');
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
async openContent(
|
||||
attachmentId: string,
|
||||
userId: string,
|
||||
variant: unknown,
|
||||
): Promise<{ attachment: Attachment; content: Readable }> {
|
||||
const attachment = await this.getVisible(attachmentId, userId);
|
||||
assertOriginalVariant(variant);
|
||||
return { attachment, content: await this.s3.getStream(attachment.objectKey) };
|
||||
}
|
||||
|
||||
private async store(
|
||||
input: UploadInput,
|
||||
filename: string,
|
||||
@@ -126,6 +154,16 @@ export class AttachmentsService implements OnModuleInit {
|
||||
}
|
||||
}
|
||||
|
||||
function assertOriginalVariant(raw: unknown): void {
|
||||
if (raw === undefined || raw === 'original') {
|
||||
return;
|
||||
}
|
||||
if (typeof raw === 'string' && KNOWN_VARIANTS.has(raw)) {
|
||||
throw new ApiError('not_found', 'attachment variant not found');
|
||||
}
|
||||
throw new ApiError('invalid_request', 'unknown attachment variant');
|
||||
}
|
||||
|
||||
function isDefiniteRejection(error: unknown): boolean {
|
||||
return (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
|
||||
Reference in New Issue
Block a user