Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion modules/uploads/services/uploads.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const createFromBuffer = async (buffer, contentType, kind, metadata = {}) => {
throw new AppError(`Upload: buffer size ${buffer.length} exceeds limit ${kindConfig.limits.fileSize}`, { code: 'SERVICE_ERROR', status: 422 });
}

const ext = MIME_TO_EXT[contentType] || 'bin';
const ext = { ...MIME_TO_EXT, ...(config.uploads?.mimeTypes ?? {}) }[contentType] || 'bin';
Comment thread
PierreBrisorgueil marked this conversation as resolved.
Outdated
const filename = `${crypto.randomBytes(32).toString('hex')}.${ext}`;
Comment thread
PierreBrisorgueil marked this conversation as resolved.
Outdated

const result = await gridfs.createFromBuffer(buffer, filename, contentType, { ...metadata, kind, contentType });
Expand Down
16 changes: 16 additions & 0 deletions modules/uploads/tests/uploads.createFromBuffer.unit.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ describe('Uploads createFromBuffer unit tests:', () => {
expect(mockGridfs.createFromBuffer).toHaveBeenCalledTimes(1);
});

test('should use custom mimeType extension from config.uploads.mimeTypes', async () => {
mockConfig.uploads.mimeTypes = { 'text/html': 'html' };
mockConfig.uploads.report = {
kind: 'report',
formats: ['text/html'],
limits: { fileSize: 1 * 1024 * 1024 },
};
mockGridfs.createFromBuffer.mockResolvedValue({ ...fakeFile, contentType: 'text/html' });

const buffer = Buffer.alloc(512);
await UploadsService.createFromBuffer(buffer, 'text/html', 'report', { user: '507f1f77bcf86cd799439011' });

const [, filename] = mockGridfs.createFromBuffer.mock.calls[0];
expect(filename).toMatch(/^[a-f0-9]{64}\.html$/);
});
Comment thread
PierreBrisorgueil marked this conversation as resolved.

test('should throw error when kind has no formats configured', async () => {
// Adding 'broken' kind at runtime — service reads config dynamically via module reference
mockConfig.uploads.broken = { kind: 'broken', limits: { fileSize: 1024 } };
Expand Down
Loading