shareoverlan
Version:
Simple local file sharing over LAN.
54 lines • 2.24 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const archiver_1 = __importDefault(require("archiver"));
const DownloadFolder = (req, res) => {
const baseDir = path_1.default.resolve(process.cwd());
const folderQuery = req.query.name;
const currentDirQuery = req.query.currentdir || "./";
if (typeof folderQuery !== "string") {
res.status(400).send("Missing or invalid 'name' query parameter");
return;
}
if (typeof currentDirQuery !== "string") {
res.status(400).send("Missing or invalid 'currentdir' query parameter");
return;
}
// Normalize and resolve safely
const safeCurrentDir = path_1.default.normalize(currentDirQuery);
const resolvedFolderPath = path_1.default.resolve(baseDir, safeCurrentDir, folderQuery);
// Security check: must stay inside baseDir
if (!resolvedFolderPath.startsWith(baseDir)) {
console.warn(`Blocked folder download attempt outside baseDir: ${resolvedFolderPath}`);
res.status(403).send("Forbidden");
return;
}
// Confirm the folder exists
if (!fs_1.default.existsSync(resolvedFolderPath) || !fs_1.default.statSync(resolvedFolderPath).isDirectory()) {
res.status(404).send("Folder not found");
return;
}
// Name the zip file
const zipName = `${folderQuery}.zip`;
res.setHeader("Content-Disposition", `attachment; filename=${zipName}`);
res.setHeader("Content-Type", "application/zip");
const archive = (0, archiver_1.default)("zip", {
zlib: { level: 9 },
});
archive.on("error", (err) => {
console.error(err);
res.status(500).send("Error creating archive");
});
// Pipe archive data to response
archive.pipe(res);
// Add the folder contents to the archive
archive.directory(resolvedFolderPath, false);
// Finalize the archive
archive.finalize();
};
exports.default = DownloadFolder;
//# sourceMappingURL=ZipFolderDownload.js.map