Skip to content

Commit

Permalink
fix: refactor file storage
Browse files Browse the repository at this point in the history
  • Loading branch information
marrouchi committed Dec 29, 2024
1 parent 5bd8643 commit 203b45c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
22 changes: 12 additions & 10 deletions api/src/attachment/controllers/attachment.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,20 @@ describe('AttachmentController', () => {
},
{ context: 'block_attachment' },
);
expect(attachmentService.create).toHaveBeenCalledWith({
size: attachmentFile.size,
type: attachmentFile.mimetype,
name: attachmentFile.filename,
location: `/${attachmentFile.filename}`,
ownerType: 'User',
owner: id,
context: 'block_attachment',
});
// const [name, ext] = attachmentFile.filename.split('.');
expect(attachmentService.create).toHaveBeenCalledWith(
expect.objectContaining({
size: attachmentFile.size,
type: attachmentFile.mimetype,
name: attachmentFile.filename,
ownerType: 'User',
owner: id,
context: 'block_attachment',
}),
);
expect(result).toEqualPayload(
[{ ...attachment, ownerType: 'User', owner: id }],
[...IGNORED_TEST_FIELDS, 'url'],
[...IGNORED_TEST_FIELDS, 'url', 'location'],
);
});
});
Expand Down
11 changes: 2 additions & 9 deletions api/src/attachment/controllers/attachment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import {
import { AttachmentGuard } from '../guards/attachment-ability.guard';
import { Attachment } from '../schemas/attachment.schema';
import { AttachmentService } from '../services/attachment.service';
import { generateUniqueFilename } from '../utilities';

@UseInterceptors(CsrfInterceptor)
@Controller('attachment')
Expand Down Expand Up @@ -124,12 +123,7 @@ export class AttachmentController extends BaseController<Attachment> {
if (config.parameters.storageMode === 'memory') {
return memoryStorage();
} else {
return diskStorage({
destination: config.parameters.uploadDir,
filename: (_req, file, cb) => {
cb(null, generateUniqueFilename(file.originalname));
},
});
return diskStorage({});
}
})(),
}),
Expand All @@ -152,14 +146,13 @@ export class AttachmentController extends BaseController<Attachment> {

const attachments = [];
for (const file of files.file) {
const attachment = await this.attachmentService.store(file.buffer, {
const attachment = await this.attachmentService.store(file, {
name: file.originalname,
size: file.size,
type: file.mimetype,
context,
owner: userId,
ownerType: 'User',
location: `/${file.filename}`,
});
attachments.push(attachment);
}
Expand Down

0 comments on commit 203b45c

Please sign in to comment.