Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue # 11] : Student Assignment Submission #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ dist
# vuepress build output
.vuepress/dist

# ignore upload files
uploads/

# vuepress v2.x temp and cache directory
.temp
.cache
Expand Down
3 changes: 3 additions & 0 deletions config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MONGODB_URL=mongodb://localhost:27017/course
PORT=3000
SECRET_KEY=course-manager
135 changes: 135 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"mongodb": "^6.1.0",
"mongoose": "^7.5.3",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.0.1",
"passport": "^0.6.0",
"passport-local": "^1.0.0",
Expand Down
67 changes: 65 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ const cookieParser = require("cookie-parser");
const mongoSanitize = require("express-mongo-sanitize");
const dotenv = require("dotenv");
const path = require("path");
const multer = require("multer");

const dbConfig = require("./config/dbconfig");
dotenv.config();
dotenv.config({ path: "./config.env" });
// Connect to MongoDB using the configuration
dbConfig();

dotenv.config();

const courseModel = require("./db/courseDB");

const User = require("./db/User");
Expand Down Expand Up @@ -63,7 +66,7 @@ app.use(passport.initialize());
app.use(passport.session());
//changes
const csrfProtection = csrf({ cookie: true });
app.use(csrfProtection);
// app.use(csrfProtection);



Expand All @@ -74,6 +77,66 @@ app.use("/courses", limiter, isAuthenticated, async function (req, res) {



const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./uploads"); //Make a folder named uploads otherwise it will fail
},
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
const upload = multer({ storage: storage });

app.use(
"/submission",
limiter,
isAuthenticated,
csrfProtection,
async function (req, res) {
return res.render("submission");
}
);

app.post(
"/upload",
limiter,
isAuthenticated,
csrfProtection,
upload.array("files"),
async function (req, res) {
console.log("api");
const errors = [];
const maxSize = 15 * 1024 * 1024; //15Mb file size limit
const allowedFileTypes = [
".doc",
".docx",
".xls",
".xlsx",
".ppt",
".pptx",
".txt",
//add or remove allowed files type
];
if (req.files.length <= 0) {
return res.render("submission", { error: "No File found" });
}
req.files.forEach((file) => {
const fileExtension = path.extname(file.originalname).toLowerCase();
if (!allowedFileTypes.includes(fileExtension)) {
errors.push(`Invalid file type: ${file.originalname}`);
}
if (file.size > maxSize) {
errors.push(`${file.originalname} size is greater then 15MB`);
}
});
if (errors.length > 0) {
return res.render("submission", { error: errors });
} else {
return res.render("submission", { message: "File submit succesfully" });
}
}
);

app.use("/css", express.static("src/css"));

// user routes
Expand Down
29 changes: 29 additions & 0 deletions src/views/submission.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Assignment Submission</title>
</head>
<body>
<h1>Submission</h1>
<%if(typeof error === "undefined"){%>
<form
action="/upload"
method="post"
enctype="multipart/form-data"
onsubmit="submitForm(event)"
>
<input
type="file"
name="files"
accept=".doc, .docx, .xls, .xlsx, .ppt, .pptx, .txt"
multiple
/>
<input type="submit" value="Upload Document" />
</form>
<% if (typeof message !== "undefined") { %>
<div class="success-message"><%= message %></div>
<% } %> <% } else { %> <%= error %> <%}%>
</body>
</html>