Skip to content

Commit

Permalink
Merge pull request #283 from agiledev-students-fall2023/sprint/3/spik…
Browse files Browse the repository at this point in the history
…e/233/file-renaming-deletion-create-req

added unique file naming in multer
  • Loading branch information
hasiburratul authored Nov 28, 2023
2 parents b73ccab + cff64fe commit 446c00f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
31 changes: 19 additions & 12 deletions back-end/src/routes/createIssue.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import multer from 'multer';
import { createIssueHandler } from '../controllers/createIssueHandler.js';
import express from 'express';
import multer from "multer";
import { createIssueHandler } from "../controllers/createIssueHandler.js";
import express from "express";

const router = express.Router();

const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
const upload = multer({ storage: storage });
destination: function (req, file, cb) {
cb(null, "uploads/");
},
filename: function (req, file, cb) {
// Get the current time
const currentTime = new Date().toISOString().replace(/:/g, "-");
// Append the current time to the original filename
cb(null, currentTime + "-" + file.originalname);
}
});
const upload = multer({ storage });

router.post('/:studentNetID', upload.array('uploadedFiles'), createIssueHandler);
router.post(
"/:studentNetID",
upload.array("uploadedFiles"),
createIssueHandler
);

export default router;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export function CreateRequest({ isVisible, onClose, departmentOptions, studentNa
const handleFileChange = (event) => {
setSelectedFiles([...selectedFiles, ...Array.from(event.target.files)]);
};
const handleFileRemove = (fileIndex) => {
// Filter out the file at the specified index
const newSelectedFiles = selectedFiles.filter((_, index) => index !== fileIndex);
setSelectedFiles(newSelectedFiles);
};
const handleFormSubmit = async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
Expand Down Expand Up @@ -134,7 +139,7 @@ export function CreateRequest({ isVisible, onClose, departmentOptions, studentNa
<ul className="file-list">
{selectedFiles.length > 0 ? (
selectedFiles.map((file, index) => {
return <li key={index}>{file.name}</li>;
return <li key={index} onClick={() => handleFileRemove(index)}>{file.name}</li>;
})
) : (
<li>No files uploaded</li>
Expand Down

0 comments on commit 446c00f

Please sign in to comment.