Skip to content

Commit

Permalink
uploaded files to database instead of disk storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyleal committed Jul 30, 2024
1 parent 100ea25 commit 2420b4a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
60 changes: 41 additions & 19 deletions server/src/middlewares/upload.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
const express = require('express');
const path = require('path');
const multer = require('multer');
const mongoose = require('mongoose');
const router = express.Router();

const storage = multer.diskStorage({
destination(req, file, cb) {
cb(null, 'src/uploads/'); //location file is saved
},
filename(req, file, cb) {
const ext = path.extname(file.originalname);
const firstName = req.user.firstName
? req.user.firstName.replace(/\s+/g, '_')
: 'unknown_firstname';
const lastName = req.user.lastName
? req.user.lastName.replace(/\s+/g, '_')
: 'unknown_lastname';
const filename = `${firstName}_${lastName}_Retreat_Waiver${ext}`;
cb(null, filename);
},
const fileSchema = new mongoose.Schema({
filename: String,
contentType: String,
data: Buffer,
firstName: String,
lastName: String,
});

const File = mongoose.model('File', fileSchema);

const storage = multer.memoryStorage(); // Store files in memory temporarily
const upload = multer({
storage,
limits: { fileSize: 1 * 1024 * 1024 }, // 1MB limit
fileFilter(req, file, callback) {
if (file.mimetype == 'application/pdf') {
callback(null, true);
} else {
console.log('only pdf files accepted!');
console.log('Only PDF files are accepted!');
callback(null, false);
}
},
}).single('waiver');

router.post('/upload-waiver', (req, res) => {
upload(req, res, (err) => {
upload(req, res, async (err) => {
if (err instanceof multer.MulterError) {
if (err.code === 'LIMIT_FILE_SIZE') {
return res.status(400).send('File is too large. Maximum size is 1MB.');
Expand All @@ -44,11 +39,38 @@ router.post('/upload-waiver', (req, res) => {
return res.status(400).send('An unknown error occurred.');
}

// all good
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
res.status(200).send('File uploaded successfully.');

try {
// do file processing here
const firstName = req.user.firstName
? req.user.firstName.replace(/\s+/g, '_')
: 'unknown_firstname';
const lastName = req.user.lastName
? req.user.lastName.replace(/\s+/g, '_')
: 'unknown_lastname';
const filename = `${firstName}_${lastName}_Retreat_Waiver${path.extname(
req.file.originalname,
)}`;

// save to MongoDB
const fileDoc = new File({
filename,
contentType: req.file.mimetype,
data: req.file.buffer,
firstName: req.user.firstName,
lastName: req.user.lastName,
});

await fileDoc.save();

res.status(200).send('File uploaded and saved successfully.');
} catch (error) {
console.error(error);
res.status(500).send('Error saving file.');
}
});
});

Expand Down
9 changes: 5 additions & 4 deletions server/src/models/UserModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ const UserSchema = new mongoose.Schema(
type: String,
required: false,
},
waiver: {
type: String,
required: false,
}
// waiver: {
// filename: String,
// contentType: String,
// data: Buffer,
// }
},
{ discriminatorKey: 'userType' },
);
Expand Down

0 comments on commit 2420b4a

Please sign in to comment.