feat: add attachment upload with streaming and image validation
This commit is contained in:
@@ -19,6 +19,7 @@ RUN npm run build
|
||||
|
||||
FROM build AS test
|
||||
COPY contracts/openapi.json ./contracts/openapi.json
|
||||
COPY tests/fixtures/sample.png ./tests/fixtures/sample.png
|
||||
COPY test ./test
|
||||
CMD ["npm", "test"]
|
||||
|
||||
|
||||
@@ -19,6 +19,21 @@ services:
|
||||
image: redis:7.4.1-alpine
|
||||
command: ["redis-server", "--save", "", "--appendonly", "no"]
|
||||
|
||||
s3:
|
||||
image: pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372
|
||||
command: ["server", "/data", "--address", ":9000"]
|
||||
environment:
|
||||
MINIO_ROOT_USER: test-root
|
||||
MINIO_ROOT_PASSWORD: test-root-password
|
||||
MINIO_BROWSER: "off"
|
||||
tmpfs:
|
||||
- /data
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://127.0.0.1:9000/minio/health/live"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 15
|
||||
|
||||
tests:
|
||||
build:
|
||||
context: .
|
||||
@@ -27,8 +42,14 @@ services:
|
||||
environment:
|
||||
DATABASE_URL: postgresql://test:test@postgres:5432/messenger_test?connection_limit=20
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_BUCKET: messenger-test
|
||||
S3_ACCESS_KEY_ID: test-root
|
||||
S3_SECRET_ACCESS_KEY: test-root-password
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_started
|
||||
s3:
|
||||
condition: service_healthy
|
||||
|
||||
@@ -36,6 +36,7 @@ frontend public
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
http-request set-timeout server 60s if { method POST } { path_reg ^/api/v1/chats/[^/]+/attachments$ }
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
default-server check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr none
|
||||
|
||||
Generated
+2117
-1
File diff suppressed because it is too large
Load Diff
+3
-1
@@ -13,6 +13,7 @@
|
||||
"test:watch": "jest --config ./test/jest-e2e.json --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "3.700.0",
|
||||
"@fastify/static": "7.0.4",
|
||||
"@nestjs/common": "^10.4.15",
|
||||
"@nestjs/core": "^10.4.15",
|
||||
@@ -24,7 +25,8 @@
|
||||
"class-validator": "^0.14.1",
|
||||
"ioredis": "5.4.1",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1"
|
||||
"rxjs": "^7.8.1",
|
||||
"sharp": "0.33.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "^10.4.9",
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AttachmentsModule } from './attachments/attachments.module';
|
||||
import { AuthModule } from './auth/auth.module';
|
||||
import { CacheModule } from './cache/cache.module';
|
||||
import { ChatsModule } from './chats/chats.module';
|
||||
import { DbModule } from './db/db.module';
|
||||
import { HealthModule } from './health/health.module';
|
||||
import { MessagesModule } from './messages/messages.module';
|
||||
import { StorageModule } from './storage/storage.module';
|
||||
import { UsersModule } from './users/users.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
DbModule,
|
||||
CacheModule,
|
||||
StorageModule,
|
||||
AuthModule,
|
||||
HealthModule,
|
||||
UsersModule,
|
||||
ChatsModule,
|
||||
MessagesModule,
|
||||
AttachmentsModule,
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
Controller,
|
||||
HttpCode,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { FastifyRequest } from 'fastify';
|
||||
import { Readable } from 'stream';
|
||||
import { AuthGuard } from '../auth/auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { User } from '../common/domain';
|
||||
import { AttachmentsService } from './attachments.service';
|
||||
import {
|
||||
AttachmentResponse,
|
||||
toAttachmentResponse,
|
||||
} from './dto/attachment.response';
|
||||
|
||||
@Controller()
|
||||
@UseGuards(AuthGuard)
|
||||
export class AttachmentsController {
|
||||
constructor(private readonly attachments: AttachmentsService) {}
|
||||
|
||||
@Post('api/v1/chats/:chatId/attachments')
|
||||
@HttpCode(201)
|
||||
async upload(
|
||||
@Param('chatId') chatId: string,
|
||||
@Query('filename') filename: unknown,
|
||||
@Req() request: FastifyRequest,
|
||||
@CurrentUser() user: User,
|
||||
): Promise<AttachmentResponse> {
|
||||
const attachment = await this.attachments.upload({
|
||||
chatId,
|
||||
ownerId: user.id,
|
||||
filename,
|
||||
contentType: request.uploadContentType,
|
||||
contentLength: request.headers['content-length'],
|
||||
body: request.body as Readable | undefined,
|
||||
});
|
||||
return toAttachmentResponse(attachment);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AttachmentsController } from './attachments.controller';
|
||||
import { AttachmentsService } from './attachments.service';
|
||||
|
||||
@Module({
|
||||
controllers: [AttachmentsController],
|
||||
providers: [AttachmentsService],
|
||||
})
|
||||
export class AttachmentsModule {}
|
||||
@@ -0,0 +1,160 @@
|
||||
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||
import { Attachment, Prisma } from '@prisma/client';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { mkdtemp, readdir, rm, stat } from 'fs/promises';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
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 { isDatabaseUnavailable, PrismaService } from '../db/prisma.service';
|
||||
import { S3Service } from '../storage/s3.service';
|
||||
import { assertValidImage, isSupportedContentType } from './image-validation';
|
||||
import { receiveBody } from './upload-body';
|
||||
|
||||
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;
|
||||
|
||||
export interface UploadInput {
|
||||
chatId: string;
|
||||
ownerId: string;
|
||||
filename: unknown;
|
||||
contentType: string | undefined;
|
||||
contentLength: string | undefined;
|
||||
body: Readable | undefined;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AttachmentsService implements OnModuleInit {
|
||||
private readonly logger = new Logger('AttachmentsService');
|
||||
private readonly maxBytes = Number(process.env.MAX_UPLOAD_BYTES ?? 10485760);
|
||||
private readonly maxPixels = Number(process.env.MAX_IMAGE_PIXELS ?? 40000000);
|
||||
private readonly maxConcurrent = Number(process.env.MAX_CONCURRENT_UPLOADS ?? 16);
|
||||
private readonly bodyTimeoutMs = Number(process.env.UPLOAD_BODY_TIMEOUT_MS ?? 60000);
|
||||
private activeUploads = 0;
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly s3: S3Service,
|
||||
) {}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
await this.removeStaleTempDirs();
|
||||
}
|
||||
|
||||
async upload(input: UploadInput): Promise<Attachment> {
|
||||
await assertChatMember(this.prisma, input.chatId, input.ownerId);
|
||||
const filename = parseFilename(input.filename);
|
||||
const contentType = parseContentType(input.contentType);
|
||||
if (Number(input.contentLength) > this.maxBytes) {
|
||||
throw new ApiError('payload_too_large', 'attachment exceeds size limit');
|
||||
}
|
||||
if (this.activeUploads >= this.maxConcurrent) {
|
||||
throw new ApiError('unavailable', 'too many concurrent uploads');
|
||||
}
|
||||
this.activeUploads++;
|
||||
const dir = await mkdtemp(join(tmpdir(), TEMP_PREFIX));
|
||||
try {
|
||||
return await this.store(input, filename, contentType, join(dir, 'body'));
|
||||
} finally {
|
||||
this.activeUploads--;
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
private async store(
|
||||
input: UploadInput,
|
||||
filename: string,
|
||||
contentType: string,
|
||||
path: string,
|
||||
): Promise<Attachment> {
|
||||
if (!input.body) {
|
||||
throw new ApiError('invalid_request', 'request body must not be empty');
|
||||
}
|
||||
const { size, sha256 } = await receiveBody(
|
||||
input.body,
|
||||
path,
|
||||
this.maxBytes,
|
||||
this.bodyTimeoutMs,
|
||||
);
|
||||
if (size === 0) {
|
||||
throw new ApiError('invalid_request', 'request body must not be empty');
|
||||
}
|
||||
await assertValidImage(path, size, contentType, this.maxPixels);
|
||||
const id = randomUUID();
|
||||
const objectKey = `attachments/${id}`;
|
||||
await this.s3.putFile({ key: objectKey, path, size, contentType, sha256 });
|
||||
const now = new Date();
|
||||
try {
|
||||
return await this.prisma.attachment.create({
|
||||
data: {
|
||||
id,
|
||||
chatId: input.chatId,
|
||||
ownerId: input.ownerId,
|
||||
filename,
|
||||
contentType,
|
||||
sizeBytes: size,
|
||||
sha256,
|
||||
objectKey,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (isDefiniteRejection(error)) {
|
||||
await this.s3.delete(objectKey).catch(() => {
|
||||
this.logger.warn(`compensation failed for ${objectKey}`);
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async removeStaleTempDirs(): Promise<void> {
|
||||
const root = tmpdir();
|
||||
const names = await readdir(root);
|
||||
for (const name of names.filter((n) => n.startsWith(TEMP_PREFIX))) {
|
||||
const path = join(root, name);
|
||||
const info = await stat(path).catch(() => null);
|
||||
if (info && Date.now() - info.mtimeMs > STALE_TEMP_MS) {
|
||||
await rm(path, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isDefiniteRejection(error: unknown): boolean {
|
||||
return (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
!isDatabaseUnavailable(error)
|
||||
);
|
||||
}
|
||||
|
||||
function parseFilename(raw: unknown): string {
|
||||
if (
|
||||
typeof raw !== 'string' ||
|
||||
raw.trim() === '' ||
|
||||
codePointLength(raw) > MAX_FILENAME_CODE_POINTS ||
|
||||
FORBIDDEN_FILENAME.test(raw)
|
||||
) {
|
||||
throw new ApiError(
|
||||
'invalid_request',
|
||||
'filename must be 1-255 characters without path separators or control characters',
|
||||
);
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
function parseContentType(raw: string | undefined): string {
|
||||
const contentType = raw?.split(';')[0].trim().toLowerCase() ?? '';
|
||||
if (!isSupportedContentType(contentType)) {
|
||||
throw new ApiError(
|
||||
'unsupported_media_type',
|
||||
'Content-Type must be image/png or image/jpeg',
|
||||
);
|
||||
}
|
||||
return contentType;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Attachment } from '@prisma/client';
|
||||
|
||||
export interface AttachmentResponse {
|
||||
id: string;
|
||||
chat_id: string;
|
||||
owner_id: string;
|
||||
filename: string;
|
||||
content_type: string;
|
||||
size_bytes: number;
|
||||
sha256: string;
|
||||
status: 'ready';
|
||||
variants: never[];
|
||||
error_code: null;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export function toAttachmentResponse(row: Attachment): AttachmentResponse {
|
||||
return {
|
||||
id: row.id,
|
||||
chat_id: row.chatId,
|
||||
owner_id: row.ownerId,
|
||||
filename: row.filename,
|
||||
content_type: row.contentType,
|
||||
size_bytes: row.sizeBytes,
|
||||
sha256: row.sha256,
|
||||
status: 'ready',
|
||||
variants: [],
|
||||
error_code: null,
|
||||
created_at: row.createdAt.toISOString(),
|
||||
updated_at: row.updatedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import { open } from 'fs/promises';
|
||||
import sharp from 'sharp';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
|
||||
sharp.cache(false);
|
||||
sharp.concurrency(1);
|
||||
|
||||
const PNG_SIGNATURE = Buffer.from('89504e470d0a1a0a', 'hex');
|
||||
const JPEG_SIGNATURE = Buffer.from('ffd8ff', 'hex');
|
||||
const PNG_END_CHUNK = Buffer.from('0000000049454e44ae426082', 'hex');
|
||||
const PNG_TAIL_BYTES = 64 * 1024;
|
||||
const MAX_CONCURRENT_DECODES = 2;
|
||||
|
||||
const FORMAT_BY_CONTENT_TYPE: Record<string, string> = {
|
||||
'image/png': 'png',
|
||||
'image/jpeg': 'jpeg',
|
||||
};
|
||||
|
||||
let activeDecodes = 0;
|
||||
const decodeQueue: (() => void)[] = [];
|
||||
|
||||
export function isSupportedContentType(contentType: string): boolean {
|
||||
return contentType in FORMAT_BY_CONTENT_TYPE;
|
||||
}
|
||||
|
||||
export async function assertValidImage(
|
||||
path: string,
|
||||
size: number,
|
||||
contentType: string,
|
||||
maxPixels: number,
|
||||
): Promise<void> {
|
||||
const invalid = new ApiError(
|
||||
'unsupported_media_type',
|
||||
'file is not a valid image of the declared type',
|
||||
);
|
||||
const format = FORMAT_BY_CONTENT_TYPE[contentType];
|
||||
if (!(await hasExpectedFraming(path, size, format))) {
|
||||
throw invalid;
|
||||
}
|
||||
const { width, height, format: actual } = await sharp(path, {
|
||||
limitInputPixels: false,
|
||||
})
|
||||
.metadata()
|
||||
.catch(() => {
|
||||
throw invalid;
|
||||
});
|
||||
if (!width || !height || actual !== format) {
|
||||
throw invalid;
|
||||
}
|
||||
if (width * height > maxPixels) {
|
||||
throw new ApiError('payload_too_large', 'image exceeds pixel limit');
|
||||
}
|
||||
await acquireDecodeSlot();
|
||||
try {
|
||||
await sharp(path, {
|
||||
failOn: 'warning',
|
||||
sequentialRead: true,
|
||||
limitInputPixels: maxPixels,
|
||||
}).stats();
|
||||
} catch {
|
||||
throw invalid;
|
||||
} finally {
|
||||
releaseDecodeSlot();
|
||||
}
|
||||
}
|
||||
|
||||
async function hasExpectedFraming(
|
||||
path: string,
|
||||
size: number,
|
||||
format: string,
|
||||
): Promise<boolean> {
|
||||
const file = await open(path, 'r');
|
||||
try {
|
||||
const head = Buffer.alloc(PNG_SIGNATURE.length);
|
||||
await file.read(head, 0, head.length, 0);
|
||||
if (format === 'jpeg') {
|
||||
return head.subarray(0, JPEG_SIGNATURE.length).equals(JPEG_SIGNATURE);
|
||||
}
|
||||
if (!head.equals(PNG_SIGNATURE)) {
|
||||
return false;
|
||||
}
|
||||
const tail = Buffer.alloc(Math.min(size, PNG_TAIL_BYTES));
|
||||
await file.read(tail, 0, tail.length, size - tail.length);
|
||||
return tail.includes(PNG_END_CHUNK);
|
||||
} finally {
|
||||
await file.close();
|
||||
}
|
||||
}
|
||||
|
||||
function acquireDecodeSlot(): Promise<void> {
|
||||
if (activeDecodes < MAX_CONCURRENT_DECODES) {
|
||||
activeDecodes++;
|
||||
return Promise.resolve();
|
||||
}
|
||||
return new Promise((resolve) => decodeQueue.push(resolve));
|
||||
}
|
||||
|
||||
function releaseDecodeSlot(): void {
|
||||
const next = decodeQueue.shift();
|
||||
if (next) {
|
||||
next();
|
||||
} else {
|
||||
activeDecodes--;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { createHash } from 'crypto';
|
||||
import { FastifyInstance, FastifyRequest } from 'fastify';
|
||||
import { createWriteStream } from 'fs';
|
||||
import { Readable } from 'stream';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
|
||||
const UPLOAD_ROUTE = '/api/v1/chats/:chatId/attachments';
|
||||
const PASS_THROUGH_TYPE = 'application/x-messenger-upload';
|
||||
|
||||
declare module 'fastify' {
|
||||
interface FastifyRequest {
|
||||
uploadContentType?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export function registerUploadBody(instance: FastifyInstance): void {
|
||||
instance.addContentTypeParser(PASS_THROUGH_TYPE, (_request, payload, done) => {
|
||||
done(null, payload);
|
||||
});
|
||||
instance.addHook('onRequest', (request: FastifyRequest, _reply, done) => {
|
||||
if (request.method === 'POST' && request.routeOptions.url === UPLOAD_ROUTE) {
|
||||
request.uploadContentType = request.headers['content-type'];
|
||||
request.headers['content-type'] = PASS_THROUGH_TYPE;
|
||||
}
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
export interface ReceivedBody {
|
||||
size: number;
|
||||
sha256: string;
|
||||
}
|
||||
|
||||
export function receiveBody(
|
||||
body: Readable,
|
||||
path: string,
|
||||
maxBytes: number,
|
||||
timeoutMs: number,
|
||||
): Promise<ReceivedBody> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const hash = createHash('sha256');
|
||||
const out = createWriteStream(path, { flags: 'wx', mode: 0o600 });
|
||||
let size = 0;
|
||||
let settled = false;
|
||||
|
||||
const timer = setTimeout(() => body.destroy(), timeoutMs);
|
||||
const settle = (done: () => void): void => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
body.off('data', onData);
|
||||
body.off('end', onEnd);
|
||||
body.off('close', onClose);
|
||||
done();
|
||||
};
|
||||
const fail = (error: ApiError): void => {
|
||||
settle(() => {
|
||||
out.destroy();
|
||||
reject(error);
|
||||
});
|
||||
};
|
||||
const onData = (chunk: Buffer): void => {
|
||||
size += chunk.length;
|
||||
if (size > maxBytes) {
|
||||
fail(new ApiError('payload_too_large', 'attachment exceeds size limit'));
|
||||
return;
|
||||
}
|
||||
hash.update(chunk);
|
||||
if (!out.write(chunk)) {
|
||||
body.pause();
|
||||
out.once('drain', () => {
|
||||
if (!settled) {
|
||||
body.resume();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
const onEnd = (): void => {
|
||||
settle(() => {
|
||||
out.end(() =>
|
||||
resolve({ size, sha256: hash.digest('hex') }),
|
||||
);
|
||||
});
|
||||
};
|
||||
const onClose = (): void => {
|
||||
fail(new ApiError('invalid_request', 'upload was interrupted'));
|
||||
};
|
||||
|
||||
out.on('error', () => {
|
||||
fail(new ApiError('unavailable', 'temporary storage unavailable'));
|
||||
});
|
||||
body.on('data', onData);
|
||||
body.on('end', onEnd);
|
||||
body.on('close', onClose);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { validate as isUuid } from '../common/uuid';
|
||||
import { PrismaService } from '../db/prisma.service';
|
||||
|
||||
export async function assertChatMember(
|
||||
prisma: PrismaService,
|
||||
chatId: string,
|
||||
userId: string,
|
||||
): Promise<void> {
|
||||
if (!isUuid(chatId)) {
|
||||
throw new ApiError('invalid_request', 'chatId must be a valid UUID');
|
||||
}
|
||||
const member = await prisma.chatMember.findUnique({
|
||||
where: { chatId_userId: { chatId, userId } },
|
||||
});
|
||||
if (!member) {
|
||||
throw new ApiError('not_found', 'chat not found');
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||
import { registerUploadBody } from './attachments/upload-body';
|
||||
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
|
||||
import { registerRequestId } from './common/interceptors/request-id';
|
||||
|
||||
export function configureApp(app: NestFastifyApplication): void {
|
||||
registerRequestId(app.getHttpAdapter().getInstance());
|
||||
const instance = app.getHttpAdapter().getInstance();
|
||||
registerRequestId(instance);
|
||||
registerUploadBody(instance);
|
||||
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { createHash } from 'crypto';
|
||||
import { assertChatMember } from '../chats/chat-access';
|
||||
import { codePointLength } from '../common/dto/code-point-length.validator';
|
||||
import { decodeMessageCursor, encodeMessageCursor } from '../common/cursor';
|
||||
import { Message } from '../common/domain';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { validate as isUuid } from '../common/uuid';
|
||||
import { PrismaService } from '../db/prisma.service';
|
||||
import { MessagePageResponse, toMessageResponse } from './dto/message.response';
|
||||
|
||||
@@ -125,16 +125,8 @@ export class MessagesService {
|
||||
};
|
||||
}
|
||||
|
||||
private async assertMember(chatId: string, callerId: string): Promise<void> {
|
||||
if (!isUuid(chatId)) {
|
||||
throw new ApiError('invalid_request', 'chatId must be a valid UUID');
|
||||
}
|
||||
const member = await this.prisma.chatMember.findUnique({
|
||||
where: { chatId_userId: { chatId, userId: callerId } },
|
||||
});
|
||||
if (!member) {
|
||||
throw new ApiError('not_found', 'chat not found');
|
||||
}
|
||||
private assertMember(chatId: string, callerId: string): Promise<void> {
|
||||
return assertChatMember(this.prisma, chatId, callerId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import {
|
||||
DeleteObjectCommand,
|
||||
PutObjectCommand,
|
||||
S3Client,
|
||||
} from '@aws-sdk/client-s3';
|
||||
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common';
|
||||
import { createReadStream, readFileSync } from 'fs';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
|
||||
const CONNECT_TIMEOUT_MS = 1000;
|
||||
const IDLE_TIMEOUT_MS = 5000;
|
||||
const OPERATION_TIMEOUT_MS = 20000;
|
||||
|
||||
export interface StoredFile {
|
||||
key: string;
|
||||
path: string;
|
||||
size: number;
|
||||
contentType: string;
|
||||
sha256: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class S3Service implements OnModuleDestroy {
|
||||
private readonly logger = new Logger('S3Service');
|
||||
private readonly client: S3Client;
|
||||
private readonly bucket = process.env.S3_BUCKET ?? 'messenger-attachments';
|
||||
|
||||
constructor() {
|
||||
this.client = new S3Client({
|
||||
endpoint: process.env.S3_ENDPOINT ?? 'http://s3:9000',
|
||||
region: process.env.S3_REGION ?? 'us-east-1',
|
||||
forcePathStyle: true,
|
||||
maxAttempts: 1,
|
||||
credentials: {
|
||||
accessKeyId: readSecret('S3_ACCESS_KEY_ID'),
|
||||
secretAccessKey: readSecret('S3_SECRET_ACCESS_KEY'),
|
||||
},
|
||||
requestHandler: {
|
||||
connectionTimeout: CONNECT_TIMEOUT_MS,
|
||||
requestTimeout: IDLE_TIMEOUT_MS,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async putFile(file: StoredFile): Promise<void> {
|
||||
await this.guard(`put ${file.key}`, () =>
|
||||
this.client.send(
|
||||
new PutObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: file.key,
|
||||
Body: createReadStream(file.path),
|
||||
ContentLength: file.size,
|
||||
ContentType: file.contentType,
|
||||
ChecksumSHA256: Buffer.from(file.sha256, 'hex').toString('base64'),
|
||||
}),
|
||||
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async delete(key: string): Promise<void> {
|
||||
await this.guard(`delete ${key}`, () =>
|
||||
this.client.send(
|
||||
new DeleteObjectCommand({ Bucket: this.bucket, Key: key }),
|
||||
{ abortSignal: AbortSignal.timeout(OPERATION_TIMEOUT_MS) },
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
onModuleDestroy(): void {
|
||||
this.client.destroy();
|
||||
}
|
||||
|
||||
private async guard<T>(action: string, run: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await run();
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.name : 'unknown error';
|
||||
this.logger.error(`${action} failed: ${reason}`);
|
||||
throw new ApiError('unavailable', 'object storage unavailable');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function readSecret(name: string): string {
|
||||
const file = process.env[`${name}_FILE`];
|
||||
const value = file ? readFileSync(file, 'utf8').trim() : process.env[name];
|
||||
if (!value) {
|
||||
throw new Error(`${name}_FILE or ${name} must be set`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { S3Service } from './s3.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [S3Service],
|
||||
exports: [S3Service],
|
||||
})
|
||||
export class StorageModule {}
|
||||
@@ -0,0 +1,358 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import Ajv2020 from 'ajv/dist/2020';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import { readdirSync } from 'fs';
|
||||
import { connect } from 'net';
|
||||
import { tmpdir } from 'os';
|
||||
import request from 'supertest';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import { loadDocument } from '../src/docs/swagger';
|
||||
import { S3Service } from '../src/storage/s3.service';
|
||||
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
|
||||
import {
|
||||
corruptedJpegs,
|
||||
corruptedPngs,
|
||||
jpeg,
|
||||
png,
|
||||
pngHeaderOnly,
|
||||
pngOfExactSize,
|
||||
SAMPLE_PNG,
|
||||
} from './images';
|
||||
|
||||
const MAX_BYTES = 10 * 1024 * 1024;
|
||||
|
||||
describe('Загрузка вложений (e2e)', () => {
|
||||
let app: INestApplication;
|
||||
let server: any;
|
||||
let alice: TestUser;
|
||||
let eve: TestUser;
|
||||
let chatId: string;
|
||||
|
||||
const upload = (
|
||||
body: Buffer | undefined,
|
||||
{
|
||||
type = 'image/png',
|
||||
filename = 'sample.png',
|
||||
user = alice,
|
||||
chat = chatId,
|
||||
}: {
|
||||
type?: string | null;
|
||||
filename?: string | null;
|
||||
user?: TestUser | null;
|
||||
chat?: string;
|
||||
} = {},
|
||||
) => {
|
||||
const query = filename === null ? '' : `?filename=${encodeURIComponent(filename)}`;
|
||||
const req = request(server).post(`/api/v1/chats/${chat}/attachments${query}`);
|
||||
if (user) {
|
||||
req.set('Authorization', bearer(user));
|
||||
}
|
||||
if (type) {
|
||||
req.set('Content-Type', type);
|
||||
}
|
||||
return body ? req.send(body) : req;
|
||||
};
|
||||
|
||||
const expectError = (res: request.Response, status: number, code: string) => {
|
||||
expect({ status: res.status, code: res.body.error?.code }).toEqual({ status, code });
|
||||
expect(res.headers['x-request-id']).toBeTruthy();
|
||||
expect(res.headers['x-instance-id']).toBeTruthy();
|
||||
expect(res.body.error.request_id).toBe(res.headers['x-request-id']);
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
({ app, server } = await createTestApp());
|
||||
alice = await registerUser(server, 'att_alice');
|
||||
eve = await registerUser(server, 'att_eve');
|
||||
const chat = await request(server)
|
||||
.post('/api/v1/chats')
|
||||
.set('Authorization', bearer(alice))
|
||||
.send({ title: 'Files', member_ids: [alice.id] })
|
||||
.expect(201);
|
||||
chatId = chat.body.id;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe('успешная загрузка', () => {
|
||||
it('возвращает 201 Attachment по контракту и сохраняет метаданные', async () => {
|
||||
const res = await upload(SAMPLE_PNG).expect(201);
|
||||
const document: any = loadDocument();
|
||||
const ajv = new Ajv2020({ strict: false, validateFormats: false });
|
||||
ajv.addSchema(document, 'contract');
|
||||
const validate = ajv.compile({ $ref: 'contract#/components/schemas/Attachment' });
|
||||
expect({ valid: validate(res.body), errors: validate.errors }).toEqual({
|
||||
valid: true,
|
||||
errors: null,
|
||||
});
|
||||
expect(res.body).toMatchObject({
|
||||
chat_id: chatId,
|
||||
owner_id: alice.id,
|
||||
filename: 'sample.png',
|
||||
content_type: 'image/png',
|
||||
size_bytes: SAMPLE_PNG.length,
|
||||
sha256: createHash('sha256').update(SAMPLE_PNG).digest('hex'),
|
||||
status: 'ready',
|
||||
variants: [],
|
||||
error_code: null,
|
||||
});
|
||||
const row = await app.get(PrismaService).attachment.findUnique({
|
||||
where: { id: res.body.id },
|
||||
});
|
||||
expect(row?.objectKey).toBe(`attachments/${res.body.id}`);
|
||||
expect(JSON.stringify(res.body)).not.toContain('attachments/');
|
||||
});
|
||||
|
||||
it('принимает реальный JPEG', async () => {
|
||||
const res = await upload(await jpeg(), {
|
||||
type: 'image/jpeg',
|
||||
filename: 'фото 1.jpg',
|
||||
}).expect(201);
|
||||
expect(res.body).toMatchObject({
|
||||
content_type: 'image/jpeg',
|
||||
filename: 'фото 1.jpg',
|
||||
});
|
||||
});
|
||||
|
||||
it('игнорирует параметры и регистр Content-Type', async () => {
|
||||
await upload(SAMPLE_PNG, { type: 'IMAGE/PNG; charset=binary' }).expect(201);
|
||||
});
|
||||
|
||||
it('принимает файл ровно 10 MiB, filename ровно 255 символов и PNG с байтами после IEND', async () => {
|
||||
const exact = pngOfExactSize(MAX_BYTES);
|
||||
expect(exact.length).toBe(MAX_BYTES);
|
||||
const res = await upload(exact).expect(201);
|
||||
expect(res.body.size_bytes).toBe(MAX_BYTES);
|
||||
await upload(SAMPLE_PNG, { filename: `${'ф'.repeat(251)}.png` }).expect(201);
|
||||
await upload(Buffer.concat([png(4, 4), Buffer.from('tail')])).expect(201);
|
||||
});
|
||||
|
||||
it('принимает ровно 40 млн пикселей', async () => {
|
||||
const res = await upload(await bigPng(8000, 5000));
|
||||
expect(res.status).toBe(201);
|
||||
});
|
||||
});
|
||||
|
||||
describe('порядок проверок и доступ', () => {
|
||||
it('без токена — 401 раньше любых проверок содержимого', async () => {
|
||||
const cases: [string | null, string | null][] = [
|
||||
['image/png', 'a.png'],
|
||||
['text/plain', 'a.png'],
|
||||
['application/json', 'a.png'],
|
||||
['image/png', '../a.png'],
|
||||
[null, null],
|
||||
];
|
||||
for (const [type, filename] of cases) {
|
||||
const res = await upload(Buffer.from('{bad'), { type, filename, user: null });
|
||||
expectError(res, 401, 'unauthorized');
|
||||
expect(res.headers['www-authenticate']).toBe('Bearer realm="messenger"');
|
||||
}
|
||||
});
|
||||
|
||||
it('посторонний и несуществующий чат — одинаковый 404, чужой файл не сохраняется', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const before = await prisma.attachment.count();
|
||||
expectError(await upload(SAMPLE_PNG, { user: eve }), 404, 'not_found');
|
||||
expectError(await upload(SAMPLE_PNG, { chat: randomUUID() }), 404, 'not_found');
|
||||
expect(await prisma.attachment.count()).toBe(before);
|
||||
});
|
||||
|
||||
it('неверный UUID чата — 400', async () => {
|
||||
expectError(await upload(SAMPLE_PNG, { chat: 'not-a-uuid' }), 400, 'invalid_request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('filename', () => {
|
||||
const invalid = {
|
||||
'отсутствует': null,
|
||||
'пустой': '',
|
||||
'только пробелы': ' ',
|
||||
'256 символов': 'x'.repeat(256),
|
||||
'путь через /': '../x.png',
|
||||
'путь через \\': 'a\\b.png',
|
||||
'NUL': 'a\u0000b.png',
|
||||
'перевод строки': 'a\nb.png',
|
||||
'DEL': 'bad\u007f.png',
|
||||
'C1 U+0085': 'c1\u0085.png',
|
||||
'разделитель строк U+2028': 'a
b.png',
|
||||
};
|
||||
it.each(Object.entries(invalid))('%s — 400', async (_name, filename) => {
|
||||
expectError(await upload(SAMPLE_PNG, { filename }), 400, 'invalid_request');
|
||||
});
|
||||
|
||||
it('повторённый параметр — 400', async () => {
|
||||
const res = await request(server)
|
||||
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png&filename=b.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG);
|
||||
expectError(res, 400, 'invalid_request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('тип содержимого', () => {
|
||||
it.each(['text/plain', 'application/json', 'application/octet-stream', 'multipart/form-data; boundary=x', 'image/gif', 'image/webp'])(
|
||||
'Content-Type %s — 415',
|
||||
async (type) => {
|
||||
expectError(await upload(SAMPLE_PNG, { type }), 415, 'unsupported_media_type');
|
||||
},
|
||||
);
|
||||
|
||||
it('без Content-Type — 415', async () => {
|
||||
expectError(await upload(SAMPLE_PNG, { type: null }), 415, 'unsupported_media_type');
|
||||
});
|
||||
|
||||
it('подмена MIME в обе стороны и чужие форматы — 415', async () => {
|
||||
const realJpeg = await jpeg();
|
||||
expectError(await upload(SAMPLE_PNG, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
|
||||
expectError(await upload(realJpeg, { type: 'image/png' }), 415, 'unsupported_media_type');
|
||||
for (const body of [
|
||||
Buffer.from('просто текст'),
|
||||
Buffer.from('GIF89a\u0001\u0000\u0001\u0000'),
|
||||
Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>'),
|
||||
]) {
|
||||
expectError(await upload(body), 415, 'unsupported_media_type');
|
||||
}
|
||||
});
|
||||
|
||||
it('пустое тело — 400', async () => {
|
||||
expectError(await upload(undefined), 400, 'invalid_request');
|
||||
expectError(await upload(Buffer.alloc(0)), 400, 'invalid_request');
|
||||
});
|
||||
});
|
||||
|
||||
describe('повреждённые изображения', () => {
|
||||
it.each(Object.entries(corruptedPngs()))('PNG: %s — 415', async (_name, body) => {
|
||||
expectError(await upload(body), 415, 'unsupported_media_type');
|
||||
});
|
||||
|
||||
it('JPEG: все повреждения — 415', async () => {
|
||||
for (const body of Object.values(corruptedJpegs(await jpeg()))) {
|
||||
expectError(await upload(body, { type: 'image/jpeg' }), 415, 'unsupported_media_type');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('лимиты', () => {
|
||||
it('10 MiB + 1 байт — 413 и без чтения объявленного тела', async () => {
|
||||
expectError(await upload(pngOfExactSize(MAX_BYTES + 1)), 413, 'payload_too_large');
|
||||
});
|
||||
|
||||
it('превышение при chunked без Content-Length — 413', async () => {
|
||||
const res = await new Promise<request.Response>((resolve, reject) => {
|
||||
const req = upload(undefined);
|
||||
req.set('Transfer-Encoding', 'chunked');
|
||||
req.write(pngOfExactSize(MAX_BYTES));
|
||||
req.write(Buffer.alloc(1024));
|
||||
req.end((error, response) => (response ? resolve(response) : reject(error)));
|
||||
});
|
||||
expectError(res, 413, 'payload_too_large');
|
||||
});
|
||||
|
||||
it('превышение лимита пикселей — 413 до декодирования', async () => {
|
||||
for (const body of [pngHeaderOnly(8001, 5000), pngHeaderOnly(20000, 10000)]) {
|
||||
expectError(await upload(body), 413, 'payload_too_large');
|
||||
}
|
||||
});
|
||||
|
||||
it('оборванная загрузка не оставляет временных файлов и строк в БД', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const before = await prisma.attachment.count();
|
||||
const { port } = server.address();
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const socket = connect(port, '127.0.0.1', () => {
|
||||
socket.write(
|
||||
`POST /api/v1/chats/${chatId}/attachments?filename=a.png HTTP/1.1\r\n` +
|
||||
`Host: test\r\nAuthorization: ${bearer(alice)}\r\n` +
|
||||
`Content-Type: image/png\r\nContent-Length: ${SAMPLE_PNG.length}\r\n\r\n`,
|
||||
);
|
||||
socket.write(SAMPLE_PNG.subarray(0, 1000));
|
||||
setTimeout(() => {
|
||||
socket.destroy();
|
||||
resolve();
|
||||
}, 300);
|
||||
});
|
||||
socket.on('error', reject);
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
expect(await prisma.attachment.count()).toBe(before);
|
||||
expect(activeTempDirs()).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('отказы', () => {
|
||||
it('S3 недоступен — 503 с заголовками, метаданные не создаются', async () => {
|
||||
const { app: down, server: downServer } = await createTestApp({
|
||||
S3_ENDPOINT: 'http://127.0.0.1:9',
|
||||
});
|
||||
try {
|
||||
const prisma = app.get(PrismaService);
|
||||
const before = await prisma.attachment.count();
|
||||
const started = Date.now();
|
||||
const res = await request(downServer)
|
||||
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG);
|
||||
expectError(res, 503, 'unavailable');
|
||||
expect(Date.now() - started).toBeLessThan(3000);
|
||||
expect(await prisma.attachment.count()).toBe(before);
|
||||
} finally {
|
||||
await down.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('явный отказ INSERT после PUT удаляет объект', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const s3 = app.get(S3Service);
|
||||
const remove = jest.spyOn(s3, 'delete');
|
||||
const create = jest.spyOn(prisma.attachment, 'create').mockRejectedValueOnce(
|
||||
new Prisma.PrismaClientKnownRequestError('rejected', {
|
||||
code: 'P2003',
|
||||
clientVersion: 'test',
|
||||
}),
|
||||
);
|
||||
try {
|
||||
const res = await upload(SAMPLE_PNG);
|
||||
expect(res.status).toBe(500);
|
||||
expect(remove).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
create.mockRestore();
|
||||
remove.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('при неясном исходе COMMIT объект не удаляется', async () => {
|
||||
const prisma = app.get(PrismaService);
|
||||
const s3 = app.get(S3Service);
|
||||
const remove = jest.spyOn(s3, 'delete');
|
||||
const create = jest.spyOn(prisma.attachment, 'create').mockRejectedValueOnce(
|
||||
new Prisma.PrismaClientKnownRequestError('connection lost', {
|
||||
code: 'P1017',
|
||||
clientVersion: 'test',
|
||||
}),
|
||||
);
|
||||
try {
|
||||
expectError(await upload(SAMPLE_PNG), 503, 'unavailable');
|
||||
expect(remove).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
create.mockRestore();
|
||||
remove.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function activeTempDirs(): string[] {
|
||||
return readdirSync(tmpdir()).filter((name) => name.startsWith('messenger-upload-'));
|
||||
}
|
||||
|
||||
async function bigPng(width: number, height: number): Promise<Buffer> {
|
||||
const { default: sharp } = await import('sharp');
|
||||
return sharp({ create: { width, height, channels: 3, background: '#336699' } })
|
||||
.png({ compressionLevel: 9 })
|
||||
.toBuffer();
|
||||
}
|
||||
+24
-1
@@ -1,5 +1,28 @@
|
||||
import {
|
||||
BucketAlreadyOwnedByYou,
|
||||
CreateBucketCommand,
|
||||
S3Client,
|
||||
} from '@aws-sdk/client-s3';
|
||||
import { execSync } from 'child_process';
|
||||
|
||||
export default function globalSetup(): void {
|
||||
export default async function globalSetup(): Promise<void> {
|
||||
execSync('npx prisma migrate deploy', { stdio: 'inherit' });
|
||||
const client = new S3Client({
|
||||
endpoint: process.env.S3_ENDPOINT,
|
||||
region: 'us-east-1',
|
||||
forcePathStyle: true,
|
||||
credentials: {
|
||||
accessKeyId: process.env.S3_ACCESS_KEY_ID!,
|
||||
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
|
||||
},
|
||||
});
|
||||
try {
|
||||
await client.send(new CreateBucketCommand({ Bucket: process.env.S3_BUCKET! }));
|
||||
} catch (error) {
|
||||
if (!(error instanceof BucketAlreadyOwnedByYou)) {
|
||||
throw error;
|
||||
}
|
||||
} finally {
|
||||
client.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import sharp from 'sharp';
|
||||
import { deflateSync } from 'zlib';
|
||||
|
||||
const CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
|
||||
let c = n;
|
||||
for (let k = 0; k < 8; k++) {
|
||||
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
||||
}
|
||||
return c >>> 0;
|
||||
});
|
||||
|
||||
function crc32(data: Buffer): number {
|
||||
let c = 0xffffffff;
|
||||
for (const byte of data) {
|
||||
c = CRC_TABLE[(c ^ byte) & 255] ^ (c >>> 8);
|
||||
}
|
||||
return (c ^ 0xffffffff) >>> 0;
|
||||
}
|
||||
|
||||
export function pngChunk(type: string, data: Buffer): Buffer {
|
||||
const name = Buffer.from(type, 'latin1');
|
||||
const length = Buffer.alloc(4);
|
||||
length.writeUInt32BE(data.length);
|
||||
const crc = Buffer.alloc(4);
|
||||
crc.writeUInt32BE(crc32(Buffer.concat([name, data])));
|
||||
return Buffer.concat([length, name, data, crc]);
|
||||
}
|
||||
|
||||
const SIGNATURE = Buffer.from('89504e470d0a1a0a', 'hex');
|
||||
const END = pngChunk('IEND', Buffer.alloc(0));
|
||||
|
||||
function header(width: number, height: number, colorType = 2): Buffer {
|
||||
const data = Buffer.alloc(13);
|
||||
data.writeUInt32BE(width, 0);
|
||||
data.writeUInt32BE(height, 4);
|
||||
data[8] = 8;
|
||||
data[9] = colorType;
|
||||
return pngChunk('IHDR', data);
|
||||
}
|
||||
|
||||
function rows(width: number, height: number, filter = 0): Buffer {
|
||||
const stride = width * 3 + 1;
|
||||
const raw = Buffer.alloc(stride * height, 7);
|
||||
for (let y = 0; y < height; y++) {
|
||||
raw[y * stride] = filter;
|
||||
}
|
||||
return raw;
|
||||
}
|
||||
|
||||
export function png(width: number, height: number, extra: Buffer[] = []): Buffer {
|
||||
return Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(width, height),
|
||||
...extra,
|
||||
pngChunk('IDAT', deflateSync(rows(width, height))),
|
||||
END,
|
||||
]);
|
||||
}
|
||||
|
||||
export function pngOfExactSize(size: number): Buffer {
|
||||
const base = png(4, 4);
|
||||
const padding = size - base.length - 12;
|
||||
return png(4, 4, [pngChunk('abCd', Buffer.alloc(padding))]);
|
||||
}
|
||||
|
||||
export function pngHeaderOnly(width: number, height: number): Buffer {
|
||||
return Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(width, height),
|
||||
pngChunk('IDAT', deflateSync(Buffer.alloc(16))),
|
||||
END,
|
||||
]);
|
||||
}
|
||||
|
||||
export const SAMPLE_PNG = readFileSync(
|
||||
join(__dirname, '..', 'tests', 'fixtures', 'sample.png'),
|
||||
);
|
||||
|
||||
export const JPEG_WITHOUT_DATA = Buffer.from(
|
||||
'ffd8ffc0000b080001000101011100ffda0008010100003f00ffd9',
|
||||
'hex',
|
||||
);
|
||||
|
||||
export async function jpeg(): Promise<Buffer> {
|
||||
const noise = Buffer.alloc(320 * 200 * 3);
|
||||
for (let i = 0; i < noise.length; i++) {
|
||||
noise[i] = (i * 7919) & 255;
|
||||
}
|
||||
return sharp(noise, { raw: { width: 320, height: 200, channels: 3 } })
|
||||
.jpeg()
|
||||
.toBuffer();
|
||||
}
|
||||
|
||||
export function corruptedPngs(): Record<string, Buffer> {
|
||||
const lastRowFilter = rows(64, 64);
|
||||
lastRowFilter[63 * (64 * 3 + 1)] = 5;
|
||||
const flipped = Buffer.from(SAMPLE_PNG);
|
||||
flipped[100] ^= 255;
|
||||
return {
|
||||
'первые 33 байта sample.png': SAMPLE_PNG.subarray(0, 33),
|
||||
'половина файла': SAMPLE_PNG.subarray(0, SAMPLE_PNG.length >> 1),
|
||||
'без последних 20 байт': SAMPLE_PNG.subarray(0, SAMPLE_PNG.length - 20),
|
||||
'без IEND': SAMPLE_PNG.subarray(0, SAMPLE_PNG.length - 12),
|
||||
'неверный CRC': flipped,
|
||||
'фильтр строки 5': pngFromRaw(1, 1, rows(1, 1, 5)),
|
||||
'фильтр строки 255': pngFromRaw(1, 1, rows(1, 1, 255)),
|
||||
'неверный фильтр в последней строке': pngFromRaw(64, 64, lastRowFilter),
|
||||
'мусор в IDAT с верным CRC': Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(64, 64),
|
||||
pngChunk('IDAT', Buffer.alloc(40, 0xab)),
|
||||
END,
|
||||
]),
|
||||
'данных меньше, чем строк': pngFromRaw(64, 64, rows(64, 10)),
|
||||
'палитра на 257 записей': Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(1, 1, 3),
|
||||
pngChunk('PLTE', Buffer.alloc(771)),
|
||||
pngChunk('IDAT', deflateSync(Buffer.from([0, 0]))),
|
||||
END,
|
||||
]),
|
||||
};
|
||||
}
|
||||
|
||||
export function corruptedJpegs(valid: Buffer): Record<string, Buffer> {
|
||||
const garbled = Buffer.from(valid);
|
||||
for (let i = 800; i < garbled.length - 2; i += 3) {
|
||||
garbled[i] = 0x55;
|
||||
}
|
||||
return {
|
||||
'27 байт без таблиц и данных': JPEG_WITHOUT_DATA,
|
||||
'половина файла': valid.subarray(0, valid.length >> 1),
|
||||
'без последних 20 байт': valid.subarray(0, valid.length - 20),
|
||||
'испорченные данные скана': garbled,
|
||||
};
|
||||
}
|
||||
|
||||
function pngFromRaw(width: number, height: number, raw: Buffer): Buffer {
|
||||
return Buffer.concat([
|
||||
SIGNATURE,
|
||||
header(width, height),
|
||||
pngChunk('IDAT', deflateSync(raw)),
|
||||
END,
|
||||
]);
|
||||
}
|
||||
Reference in New Issue
Block a user