@webtypen/webframez-core 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dev/app.ts +5 -0
- package/dist/Router/Response.d.ts +1 -0
- package/dist/Router/Response.js +40 -0
- package/package.json +5 -2
- package/src/Router/Response.ts +45 -0
package/dev/app.ts
CHANGED
|
@@ -32,6 +32,11 @@ app.boot({
|
|
|
32
32
|
});
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
+
Route.get("/download", async (req: Request, res: Response) => {
|
|
36
|
+
const file = path.join(__dirname, "uploads", "2024-06-09_test.zip");
|
|
37
|
+
return await res.download(file);
|
|
38
|
+
});
|
|
39
|
+
|
|
35
40
|
Route.post("/fileupload", (req: Request, res: Response) => {
|
|
36
41
|
if (!req.message) {
|
|
37
42
|
throw Error("Missing IncomingMessage ...");
|
package/dist/Router/Response.js
CHANGED
|
@@ -8,8 +8,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
exports.Response = void 0;
|
|
16
|
+
const fs_1 = __importDefault(require("fs"));
|
|
17
|
+
const path_1 = __importDefault(require("path"));
|
|
18
|
+
const mime_types_1 = __importDefault(require("mime-types"));
|
|
13
19
|
class Response {
|
|
14
20
|
constructor(options) {
|
|
15
21
|
this.statusCode = 200;
|
|
@@ -85,6 +91,40 @@ class Response {
|
|
|
85
91
|
}
|
|
86
92
|
return this;
|
|
87
93
|
}
|
|
94
|
+
download(filepath, options) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
if (!this.res) {
|
|
98
|
+
return reject("Cannot download: Original response object is missing");
|
|
99
|
+
}
|
|
100
|
+
if (!fs_1.default.existsSync(filepath)) {
|
|
101
|
+
return reject("Cannot download: File '" + filepath + "' does not exist");
|
|
102
|
+
}
|
|
103
|
+
let mimeType = options && options.contentType ? options.contentType : "application/octet-stream";
|
|
104
|
+
if (options && options.inline) {
|
|
105
|
+
let tempMime = mime_types_1.default.lookup(filepath);
|
|
106
|
+
if (tempMime) {
|
|
107
|
+
mimeType = tempMime;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const stats = fs_1.default.lstatSync(filepath);
|
|
111
|
+
this.header("Content-Disposition", (options && options.inline ? "inline" : "attachment") +
|
|
112
|
+
"; filename=" +
|
|
113
|
+
(options && options.filename ? options.filename : path_1.default.basename(filepath)));
|
|
114
|
+
this.header("Content-Type", mimeType);
|
|
115
|
+
this.header("Content-Length", stats.size.toString());
|
|
116
|
+
const readStream = fs_1.default.createReadStream(filepath);
|
|
117
|
+
readStream.pipe(this.res);
|
|
118
|
+
readStream.on("close", () => {
|
|
119
|
+
resolve();
|
|
120
|
+
});
|
|
121
|
+
readStream.on("error", (streamErr) => {
|
|
122
|
+
console.error(streamErr);
|
|
123
|
+
reject(streamErr);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
88
128
|
end() {
|
|
89
129
|
var _a;
|
|
90
130
|
(_a = this.res) === null || _a === void 0 ? void 0 : _a.end();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webtypen/webframez-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
|
|
5
5
|
"homepage": "https://webtypen.de/",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,11 +19,13 @@
|
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
21
|
"build": "tsc",
|
|
22
|
-
"build:check": "tsc --noEmit"
|
|
22
|
+
"build:check": "tsc --noEmit",
|
|
23
|
+
"dev": "PORT=3210 ts-node ./dev/app.ts"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"@types/busboy": "^1.5.4",
|
|
26
27
|
"@types/lodash": "^4.14.190",
|
|
28
|
+
"@types/mime-types": "^2.1.4",
|
|
27
29
|
"@types/node": "^18.11.9",
|
|
28
30
|
"@types/qs": "^6.9.7",
|
|
29
31
|
"ts-node": "^10.9.1",
|
|
@@ -34,6 +36,7 @@
|
|
|
34
36
|
"commander": "^9.4.1",
|
|
35
37
|
"dotenv": "^8.2.0",
|
|
36
38
|
"lodash": "^4.17.20",
|
|
39
|
+
"mime-types": "^2.1.35",
|
|
37
40
|
"qs": "^6.11.0",
|
|
38
41
|
"querystring": "^0.2.1"
|
|
39
42
|
}
|
package/src/Router/Response.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import mime from "mime-types";
|
|
1
4
|
import { ServerResponse } from "http";
|
|
2
5
|
|
|
3
6
|
export class Response {
|
|
@@ -86,6 +89,48 @@ export class Response {
|
|
|
86
89
|
return this;
|
|
87
90
|
}
|
|
88
91
|
|
|
92
|
+
async download(filepath: string, options?: any) {
|
|
93
|
+
return new Promise((resolve: Function, reject: Function) => {
|
|
94
|
+
if (!this.res) {
|
|
95
|
+
return reject("Cannot download: Original response object is missing");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!fs.existsSync(filepath)) {
|
|
99
|
+
return reject("Cannot download: File '" + filepath + "' does not exist");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let mimeType = options && options.contentType ? options.contentType : "application/octet-stream";
|
|
103
|
+
if (options && options.inline) {
|
|
104
|
+
let tempMime = mime.lookup(filepath);
|
|
105
|
+
if (tempMime) {
|
|
106
|
+
mimeType = tempMime;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const stats = fs.lstatSync(filepath);
|
|
111
|
+
this.header(
|
|
112
|
+
"Content-Disposition",
|
|
113
|
+
(options && options.inline ? "inline" : "attachment") +
|
|
114
|
+
"; filename=" +
|
|
115
|
+
(options && options.filename ? options.filename : path.basename(filepath))
|
|
116
|
+
);
|
|
117
|
+
this.header("Content-Type", mimeType);
|
|
118
|
+
this.header("Content-Length", stats.size.toString());
|
|
119
|
+
|
|
120
|
+
const readStream = fs.createReadStream(filepath);
|
|
121
|
+
readStream.pipe(this.res);
|
|
122
|
+
|
|
123
|
+
readStream.on("close", () => {
|
|
124
|
+
resolve();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
readStream.on("error", (streamErr) => {
|
|
128
|
+
console.error(streamErr);
|
|
129
|
+
reject(streamErr);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
89
134
|
end() {
|
|
90
135
|
this.res?.end();
|
|
91
136
|
return this;
|