Skip to content

Commit

Permalink
file downloading done
Browse files Browse the repository at this point in the history
  • Loading branch information
Tauke190 committed Dec 5, 2023
1 parent e236430 commit 980b937
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
8 changes: 5 additions & 3 deletions back-end/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import login from "./src/routes/login.js";
import studentIssues from "./src/routes/studentIssues.js";
import studentIssueUpdate from "./src/routes/studentIssueUpdate.js";
import adminIssues from "./src/routes/adminIssues.js";
import downloadFiles from "./src/routes/downloadFiles.js";
import adminPostDetails from "./src/routes/adminPostDetails.js";
import createIssue from "./src/routes/createIssue.js";
/* eslint-disable no-unused-vars */
Expand Down Expand Up @@ -45,7 +46,7 @@ app.use(cors(corsOptions));

// serve static files from the public folders
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
export const publicpath = path.join(__dirname, "public");
export const backendpath = path.join(__dirname);
app.use(express.static(path.join(__dirname, "public")));

app.use(checkReferer);
Expand All @@ -72,8 +73,7 @@ setInterval(() => {
} catch (error) {
console.error("Error in scheduled updatePriorityForOpenIssues:", error);
}
}, 30 * 60 * 1000);

}, 30 * 60 * 1000);
// an interval to run updateResolved every hour
setInterval(() => {
try {
Expand Down Expand Up @@ -118,6 +118,8 @@ app.use("/api/actions/admin/", adminPostDetails);

app.use("/api/actions/student", createIssue);

app.use("/download", downloadFiles);

// Temporary route to create users
// import User from "./models/UserModel.js";
// app.post("/create-user", async (req, res) => {
Expand Down
15 changes: 15 additions & 0 deletions back-end/src/controllers/downLoadFilesHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { backendpath } from "../../app.js";

export async function downLoadFilesHandler(req, res) {
const filename = req.params.filename;
const fileDirectory = backendpath + '/uploads/';
res.setHeader('Content-Disposition', 'attachment; filename=' + filename);
res.setHeader('Access-Control-Expose-Headers', 'Content-Disposition');

res.download(fileDirectory + filename, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
});
}
1 change: 1 addition & 0 deletions back-end/src/controllers/studentIssueUpdateHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Issue from '../../models/issueModel.js';


// The function updates the issue related to this student
export async function studentIssueUpdateHandler(req, res) {
const { paramName } = req.params; // Get the issue index from request params
Expand Down
8 changes: 8 additions & 0 deletions back-end/src/routes/downloadFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import express from "express";
import { downLoadFilesHandler } from "../controllers/downloadfilesHandler.js";

const router = express.Router();

router.get("/:filename", downLoadFilesHandler);

export default router;
1 change: 0 additions & 1 deletion back-end/src/routes/studentIssueUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ const upload = multer({ storage });
// New POST route for updating issues
router.post('/:studentNetID/:paramName', upload.array("uploadedFiles"), studentIssueUpdateHandler);


export default router;
32 changes: 31 additions & 1 deletion front-end/src/components/admin/AttachmentBar/AttachmentBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import './AttachmentBar.css';
import { useState, useEffect} from 'react';
import FileUploadOverlay from '../FileUploadOverlay/FileUploadOverlay';
import axios from 'axios';

const BASE_URL = process.env.REACT_APP_BACKEND_URL;
function AttachmentBar({ index, name, tags, fileNames, currentDepartment,isAdmin,studentNetID}) {
Expand All @@ -12,6 +13,35 @@ function AttachmentBar({ index, name, tags, fileNames, currentDepartment,isAdmin
// setSelectedFiles(newSelectedFiles);
};

const downloadFile = async (fileIndex) => {

const fileName = fileNames[fileIndex];

axios({
url: `${BASE_URL}/download/${fileName}`,
method: 'GET',
responseType: 'blob', // Important to handle binary files
}).then((response) => {

const file = new Blob(
[response.data],
{ type: 'application/octet-stream' }); // Change type based on your file type

const fileURL = URL.createObjectURL(file);

const fileLink = document.createElement('a');
fileLink.href = fileURL;
fileLink.setAttribute('download', fileName.slice(25));
document.body.appendChild(fileLink);

fileLink.click();

fileLink.remove();

URL.revokeObjectURL(fileURL);
}).catch(error => console.error('Download error:', error));
};

const openOverlay = () => {
setIsOverlayVisible(true);
};
Expand All @@ -28,7 +58,7 @@ function AttachmentBar({ index, name, tags, fileNames, currentDepartment,isAdmin
<ul className="file-list">
{fileNames.length > 0 && fileNames[0] ? (
fileNames.map((file, index) => {
return <li key={index} onClick={() => handleFileRemove(index)}>{file?.slice(25)}</li>;
return <li key={index} onClick={() => downloadFile(index)}>{file?.slice(25)}</li>;
})
) :
(
Expand Down

0 comments on commit 980b937

Please sign in to comment.