expressjs / multer

Node.js middleware for handling `multipart/form-data`.
MIT License
11.63k stars 1.06k forks source link

Upload file use Multer with Multer-gridfs-storage in environment of bun is not working but when I use environment of nodejs then it's working? #1236

Open hungattack opened 1 year ago

hungattack commented 1 year ago
const storage = new GridFsStorage({
    url: URL,
    cache: true,

    file: (req: any, files: any) => {
        return new Promise((resolve, reject) => {
// is not running in here
            try {
                crypto.randomBytes(16, (err: any, buf: { toString: (arg0: string) => any }) => {
                    if (err) {
                        return reject(err);
                    }
                    const id = primaryKey();
                    const filename = buf.toString('hex') + path.extname(files.originalname);
                    const fileInfo = {
                        filename: filename,
                        bucketName: 'uploads',
                        metadata: {
                            id_file: id, // Trường để lưu _id của tệp tin
                        },
                    };
                    resolve(fileInfo);
                });
            } catch (error) {
                console.log(error);
            }
        });
    },
});
console.log(storage, 'storage');

export const upload = multer({ storage: storage });
router.post('/sendChat', upload.array('files'), sendChatController.sendChat);
joeyguerra commented 8 months ago

Does bun have a crypto.randomBytes module and API?