Skip to content

Commit

Permalink
Merge pull request #828 from UofT-Frosh-Orientation/retreat-accessibi…
Browse files Browse the repository at this point in the history
…lity

Retreat accessibility
  • Loading branch information
ashleyleal authored Jul 30, 2024
2 parents 6da2035 + 4d2d684 commit 8c55cd8
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 25 deletions.
16 changes: 16 additions & 0 deletions client/src/pages/FroshRetreat/FroshRetreat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ const RetreatRegistration = () => {
const { setSnackbar } = useContext(SnackbarContext);
const { axios } = useAxios();
const isRetreat = user?.isRetreat === true;
const isWaiverUploaded = user?.waiver?.filename !== undefined;

const [file, setFile] = useState(null);
const [isUploaded, setIsUploaded] = useState(false);
Expand Down Expand Up @@ -315,6 +316,7 @@ const RetreatRegistration = () => {
}}
style={{ marginBottom: '25px' }}
/>

<div className="display-field">
<h3>UPLOAD SIGNED WAIVER:</h3>
<p>Only PDF files are accepted</p>
Expand All @@ -332,7 +334,21 @@ const RetreatRegistration = () => {
<p>Please view the waiver before uploading the signed copy.</p>
)}
</div>

{isWaiverUploaded ? (
<Button
label="View Uploaded Waiver"
isSecondary
onClick={() => {
window.open(`/view-waiver`, '_blank').focus();
}}
style={{ marginBottom: '25px' }}
/>
) : (
<></>
)}
</div>

{isRetreat ? (
<h2>You have already paid for Frosh Retreat!</h2>
) : outOfTickets ? (
Expand Down
111 changes: 89 additions & 22 deletions server/src/middlewares/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,108 @@ const express = require('express');
const path = require('path');
const multer = require('multer');
const router = express.Router();
const User = require('../models/UserModel');

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 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, async (err) => {
if (err instanceof multer.MulterError) {
if (err.code === 'LIMIT_FILE_SIZE') {
console.log('File too large');
return res.status(400).send('File is too large. Maximum size is 1MB.');
}
console.log('An error occurred while uploading the file.');
return res.status(400).send('An error occurred while uploading the file.');
} else if (err) {
console.log('An unknown error occurred.');
return res.status(400).send('An unknown error occurred.');
}

if (!req.file) {
console.log('No file uploaded.');
return res.status(400).send('No file uploaded.');
}

try {
const userId = req.user._id;
const user = await User.findById(userId); // get user frm DB

if (!req.user) {
console.log('User not found.');
return res.status(404).send('User not found.');
}

// 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
user.waiver = {
filename,
data: req.file.buffer,
contentType: req.file.mimetype,
};

await user.save();

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

router.post('/upload-waiver', upload.single('waiver'), (req, res) => {
if (!req.file) {
return res.status(400).send('No file uploaded.');
router.get('/view-waiver', async (req, res) => {
try {
if (!req.user) {
console.log('User not authenticated.');
return res.status(401).send('User not authenticated.');
}

const userId = req.user._id;
const user = await User.findById(userId); // get user from DB

if (!user) {
console.log('User not found.');
return res.status(404).send('User not found.');
}

if (!user.waiver) {
console.log('No waiver found for this user.');
return res.status(404).send('No waiver found for this user.');
}
const { filename, data, contentType } = user.waiver;

res.setHeader('Content-Disposition', `inline; filename="${filename}"`);
res.setHeader('Content-Type', contentType);
res.send(data);
} catch (error) {
console.error(error);
console.log('Error retrieving file.');
res.status(500).send('Error retrieving file.');
}
res.status(200).send('File uploaded successfully.');
});

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

0 comments on commit 8c55cd8

Please sign in to comment.