Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add empty file check and tests for the same #175

Merged
merged 9 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/common/src/controllers/file-upload.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export class FileUploadController {
file?: { url: string } | undefined;
}> {
try {
// file.size is an added attribute that comes from MultipartFile Interface defined in file-upload.interface
if (file.buffer.length === 0) {
return {
statusCode: 400,
message: 'empty file uploads are not allowed'
}
}
const fileUploadRequestDto = new FileUploadRequestDTO();
fileUploadRequestDto.file = file;
fileUploadRequestDto.destination = destination;
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/interceptors/file-upload.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export function FastifyFileInterceptor(
next: CallHandler,
): Promise<Observable<any>> {
const ctx = context.switchToHttp();
const request = ctx.getRequest();
request.raw = request.raw || request;

await new Promise<void>((resolve, reject) =>
this.multer.single(fieldName)(
Expand Down
34 changes: 34 additions & 0 deletions packages/common/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,38 @@ describe('AppController (e2e)', () => {
.expect(200)
.expect('Hello World!');
});

it('/files/upload-file (POST); for file with content', async () => {

const mockDestination = 'uploads';
const mockFilename = 'content.txt';

const response = await request(app.getHttpServer())
.post(
`/files/upload-file?destination=${mockDestination}&filename=${mockFilename}`,
)
.attach('file', Buffer.from('content'), mockFilename);

expect(response.body).toEqual({
message: 'File uploaded successfully',
file: { url: `${mockDestination}/${mockFilename}` },
});
});

it('/files/upload-file (POST); for empty file check', async () => {
const mockDestination = 'uploads';
const mockFilename = 'empty.txt';

const response = await request(app.getHttpServer())
.post(
`/files/upload-file?destination=${mockDestination}&filename=${mockFilename}`,
)
.attach('file', Buffer.from(''), mockFilename);


expect(response.body).toEqual({
statusCode: 400,
message: 'empty file uploads are not allowed',
});
});
});
Loading