Skip to content

Commit

Permalink
Merge pull request #88 from Saifullah-dev/feature/multi-selection
Browse files Browse the repository at this point in the history
Feature/multi selection
  • Loading branch information
Saifullah-dev authored Oct 4, 2024
2 parents 95ddb3e + c84234f commit b30e988
Show file tree
Hide file tree
Showing 10 changed files with 1,003 additions and 65 deletions.
4 changes: 2 additions & 2 deletions backend/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ Make sure you have the following installed:

### ![swagger-icon](https://github.com/user-attachments/assets/9cb14fef-febc-4b52-873c-52dfc80e601e) API Documentation

The API documentation is generated through **Swagger** and can be accessed after starting the server.
The API documentation is generated through **Swagger** and can be viewed [here](https://app.swaggerhub.com/apis-docs/SaifullahZubair/file-system_api/1.0.0).

1. Generate the Swagger docs:
1. To Generate the Swagger docs:

```bash
npm run genDocs
Expand Down
78 changes: 59 additions & 19 deletions backend/app/controllers/downloadFile.controller.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,73 @@
const FileSystem = require("../models/FileSystem.model");
const path = require("path");
const fs = require("fs");
const mongoose = require("mongoose");
const archiver = require("archiver");

const downloadFile = async (req, res) => {
// #swagger.summary = 'Downloads a file.'
// #swagger.produces = ["application/octet-stream"]
/*
#swagger.responses[200] = {
description: 'A file is successfully downloaded.',
content: {
'application/octet-stream': {
schema: {
type: 'string',
format: 'binary'
}
}
/* #swagger.parameters['filePath'] = {
in: 'query',
type: 'string',
required: 'true',
}
}
#swagger.responses[200] = {description:'File Downloaded Successfully'}
*/
try {
const { id } = req.params;
let files = req.query.files;
const isSingleFile = mongoose.Types.ObjectId.isValid(files);
const isMultipleFiles = Array.isArray(files);

if (!files || (!isSingleFile && !isMultipleFiles)) {
return res
.status(400)
.json({ error: "Invalid request body, expected a file ID or an array of file IDs." });
}

const file = await FileSystem.findById(id);
if (!file || file.isDirectory) {
res.status(404).json({ error: "File not found!" });
if (isSingleFile) {
const file = await FileSystem.findById(files);
if (!file) return res.status(404).json({ error: "File not found!" });

if (file.isDirectory) {
files = [files];
} else {
const filePath = path.join(__dirname, "../../public/uploads", file.path);
if (fs.existsSync(filePath)) {
res.setHeader("Content-Disposition", `attachment; filename="${file.name}"`);
return res.sendFile(filePath);
} else {
return res.status(404).send("File not found");
}
}
}

const filePath = path.join(__dirname, "../../public/uploads", file.path);
res.header("Access-Control-Expose-Headers", "Content-Disposition");
res.download(filePath, file.name);
const multipleFiles = await FileSystem.find({ _id: { $in: files } });
if (!multipleFiles || multipleFiles.length !== files.length) {
return res.status(404).json({ error: "One or more of the provided file IDs do not exist." });
}

const archive = archiver("zip", { zlib: { level: 9 } });

archive.on("error", (err) => {
throw err;
});

archive.pipe(res);

multipleFiles.forEach((file) => {
const filePath = path.join(__dirname, "../../public/uploads", file.path);
if (fs.existsSync(filePath)) {
if (file.isDirectory) {
archive.directory(filePath, file.name);
} else {
archive.file(filePath, { name: file.name });
}
} else {
console.log("File not found");
}
});

await archive.finalize();
} catch (error) {
res.status(500).json({ error: error.message });
}
Expand Down
2 changes: 1 addition & 1 deletion backend/app/routes/fileSystem.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ router.post("/folder", createFolderController);
router.post("/upload", upload.single("file"), uploadFileController);
router.post("/copy", copyItemController);
router.get("/", getItemsController);
router.get("/download/:id", downloadFileController);
router.get("/download", downloadFileController);
router.put("/move", moveItemController);
router.patch("/rename", renameItemController);
router.delete("/", deleteItemController);
Expand Down
Loading

0 comments on commit b30e988

Please sign in to comment.