Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit 659cf3b

Browse files
committed
Feat: add koa-send for download file
1 parent 347ea57 commit 659cf3b

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"iconv-lite": "^0.6.2",
2525
"koa": "^2.13.1",
2626
"koa-body": "^4.2.0",
27+
"koa-send": "^5.0.1",
2728
"log4js": "^6.4.0",
2829
"node-disk-info": "^1.3.0",
2930
"node-schedule": "^2.0.0",
@@ -44,17 +45,18 @@
4445
"@types/iconv-lite": "0.0.1",
4546
"@types/koa": "^2.13.4",
4647
"@types/koa__router": "^8.0.7",
48+
"@types/koa-send": "^4.1.3",
4749
"@types/log4js": "^2.3.5",
4850
"@types/mocha": "^8.2.2",
4951
"@types/node": "^18.11.18",
50-
"@types/ssh2": "^1.11.7",
5152
"@types/node-schedule": "^1.3.2",
5253
"@types/os-utils": "0.0.1",
5354
"@types/pidusage": "^2.0.1",
55+
"@types/ssh2": "^1.11.7",
5456
"@types/uuid": "^8.3.0",
55-
"ts-node": "^10.9.1",
5657
"eslint": "^7.13.0",
5758
"nodemon": "^2.0.20",
59+
"ts-node": "^10.9.1",
5860
"typescript": "^4.9.4",
5961
"webpack": "^5.73.0",
6062
"webpack-cli": "^4.10.0",

src/routers/http_router.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { $t } from "../i18n";
44
import Router from "@koa/router";
5+
import send from "koa-send";
56
import fs from "fs-extra";
67
import path from "path";
78
import { missionPassport } from "../service/mission_passport";
@@ -30,17 +31,16 @@ router.get("/download/:key/:fileName", async (ctx) => {
3031

3132
const cwd = instance.config.cwd;
3233
const fileRelativePath = mission.parameter.fileName;
33-
const ext = path.extname(fileRelativePath);
34+
3435
// Check for file cross-directory security risks
3536
const fileManager = new FileManager(cwd);
3637
if (!fileManager.check(fileRelativePath)) throw new Error((ctx.body = "Access denied: Invalid destination"));
3738

38-
// start downloading the file to the user
39-
ctx.response.set("Content-Disposition", `attachment; filename="${encodeURIComponent(paramsFileName)}"`);
40-
ctx.type = ext;
41-
ctx.body = fs.createReadStream(fileManager.toAbsolutePath(fileRelativePath));
42-
// The task has been executed, destroy the passport
43-
missionPassport.deleteMission(key);
39+
// send File
40+
const fileAbsPath = fileManager.toAbsolutePath(fileRelativePath);
41+
const fileDir = path.dirname(fileAbsPath);
42+
const fileName = path.basename(fileAbsPath);
43+
await send(ctx, fileName, { root: fileDir + "/" });
4444
} catch (error) {
4545
ctx.body = $t("http_router.downloadErr", { error: error.message });
4646
ctx.status = 500;

0 commit comments

Comments
 (0)