Skip to content

Commit

Permalink
twaek: GridFsStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Aug 31, 2024
1 parent d039c29 commit 1a0e056
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion node-cpanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const express = require('express');
const uploadRoutes = require('./routes/file-upload');
const mongoUploadRoutes = require('./routes/mongo-file-upload');
const connectDB = require('./utils/mongo_db');
const { connectDB } = require('./utils/mongo_db');
const { initGridFS } = require('./utils/gridfs');
require('dotenv').config();

Expand Down
14 changes: 13 additions & 1 deletion node-cpanel/utils/gridfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
const Grid = require('gridfs-stream');
const mongoose = require('mongoose');
const multer = require('multer');
const { GridFsStorage } = require('multer-gridfs-storage');
const { mongoURI } = require('./mongo_db');
const path = require('path');

let gfs;

Expand All @@ -21,7 +24,16 @@ const getGFS = () => {
return gfs;
};

const storage = multer.memoryStorage();
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return {
bucketName: 'uploads', // Set the collection name
filename: `${Date.now()}${path.extname(file.originalname)}`, // Set the filename
};
},
});

const uploadMongo = multer({ storage });

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion node-cpanel/utils/mongo_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ const connectDB = async () => {
}
};

module.exports = connectDB;
module.exports = { connectDB, mongoURI };

0 comments on commit 1a0e056

Please sign in to comment.