-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
285 lines (242 loc) · 8.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import formidable from "formidable";
import express from "express";
import fs from "fs";
import http from "http";
import https from "https";
import cors from "cors";
import path from "path";
import os from "os";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Check if dist folder exists
const distPath = path.join(__dirname, "../dist");
if (!fs.existsSync(distPath)) {
console.error("\x1b[31m%s\x1b[0m", "Error: 'dist' folder not found!");
console.log("\nPlease build the project first by running:");
console.log("\x1b[33m%s\x1b[0m", "npm run build");
console.log("\nThen try running the server again.\n");
process.exit(1);
}
const app = express();
app.use(
cors({
origin: (origin, callback) => {
return callback(null, true);
},
})
);
// Serve static files
app.use("/dist", express.static(distPath));
app.use("/assets", express.static(path.join(__dirname, "../samples/demo/assets")));
app.use("/demo/assets", express.static(path.join(__dirname, "../samples/demo/assets")));
// Routes
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "../samples/hello-world.html"));
});
app.get("/demo", (req, res) => {
res.sendFile(path.join(__dirname, "../samples/demo/index.html"));
});
app.get("/hello-world", (req, res) => {
res.sendFile(path.join(__dirname, "../samples/hello-world.html"));
});
app.get("/scenarios", (req, res) => {
res.sendFile(path.join(__dirname, "../samples/scenarios/customized-empty-container.html"));
});
app.get("/scenarios/customized-empty-container", (req, res) => {
res.sendFile(path.join(__dirname, "../samples/scenarios/customized-empty-container.html"));
});
app.get("/scenarios/use-file-input", (req, res) => {
res.sendFile(path.join(__dirname, "../samples/scenarios/use-file-input.html"));
});
// Allow upload feature
app.post("/upload", function (req, res) {
try {
// Create a new Formidable form
const form = formidable({
multiples: false,
uploadDir: path.join(__dirname, "uploaded"),
keepExtensions: true,
});
form.parse(req, (err, fields, files) => {
if (err) {
console.error(err);
return res.status(500).send("Error processing the file upload.");
}
const uploadedFile = files.uploadFile[0]; // Ensure the file field name matches the form
if (!uploadedFile) {
return res.status(400).send("No file uploaded.");
}
const sessionID = fields.sessionID || "default_session";
// Get current timestamp
let dt = new Date();
const fileSavePath = path.join(__dirname, "uploaded\\");
const newFileName = sessionID + "_" + dt.getTime() + "_" + uploadedFile.originalFilename;
const newFilePath = path.join(fileSavePath, newFileName);
// Move the uploaded file to the desired directory
fs.rename(uploadedFile.filepath, newFilePath, (err) => {
if (err) {
console.error(err);
return res.status(500).send("Error saving the file.");
}
console.log(`${uploadedFile.originalFilename} uploaded successfully!`);
res.send(`${uploadedFile.originalFilename}:UploadedFileName:${newFileName}`);
});
});
} catch (error) {
res.status(500).send("An error occurred during file upload.");
}
});
app.get("/download", (req, res) => {
try {
// Get query parameters
const fileName = req.query.fileName;
if (!fileName) {
return res.status(400).send('"fileName" required');
}
// Build the file path
const filePath = path.join(__dirname, "uploaded", fileName);
const fileExtension = path.extname(fileName);
// Check if the file exists
if (!fs.existsSync(filePath)) {
return res.status(404).send("File not found");
}
// Set the appropriate content type
let contentType;
switch (fileExtension) {
case ".bmp":
contentType = "image/bmp";
break;
case ".jpg":
contentType = "image/jpeg";
break;
case ".tif":
contentType = "image/tiff";
break;
case ".png":
contentType = "image/png";
break;
case ".pdf":
contentType = "application/pdf";
break;
default:
return res.status(400).send("Unsupported file type");
}
const serverFileName = fileName.match(/(.+)_(\d+)_(.+)$/);
const [, sessionID, uploadTime, realFileName] = serverFileName;
const encodedFileName = encodeURIComponent(realFileName).replace(/%20/g, " ");
// Set headers for file download
res.setHeader("Content-Type", contentType);
res.setHeader("Content-Disposition", `attachment; filename="${encodedFileName}"`);
// Send the file
res.sendFile(filePath, (err) => {
if (err) {
res.status(500).send("Error processing the file");
}
});
} catch (error) {
res.status(500).send("An error occurred while processing your request");
}
});
app.post("/delete", (req, res) => {
try {
// Get query parameters
const fileName = req.query.fileName;
if (!fileName) {
return res.status(400).send('"fileName" required');
}
// Build the file path
const filePath = path.join(__dirname, "uploaded", fileName);
// Check if the file exists
if (fs.existsSync(filePath)) {
const stats = fs.statSync(filePath);
// Remove "read-only" attribute if applicable
if ((stats.mode & fs.constants.S_IWUSR) === 0) {
fs.chmodSync(filePath, 0o666); // Make file writable
}
// Delete the file
fs.unlinkSync(filePath);
return res.send(`File ${fileName} deleted successfully.`);
} else {
return res.status(404).send("File not found");
}
} catch (err) {
console.error(err);
res.status(500).send("An error occurred while processing your request.");
}
});
let httpPort = 3000;
let httpsPort = 3001;
// redirect handling
app.use((req, res, next) => {
const host = req.get("Host"); // Get the host name from the request
// Skip redirection if it's localhost with the correct HTTP port
if (!req.secure && host !== `localhost:${httpPort}`) {
// Replace the HTTP port with HTTPS port in the host
const httpsHost = host.replace(`:${httpPort}`, `:${httpsPort}`);
return res.redirect(["https://", httpsHost, req.url].join(""));
}
next(); // Proceed to the next middleware or route
});
// HTTPS server configuration
const httpsOptions = {
key: fs.readFileSync(path.join(__dirname, "pem/key.pem")),
cert: fs.readFileSync(path.join(__dirname, "pem/cert.pem")),
};
// Create HTTPS server
const httpsServer = https.createServer(httpsOptions, app);
// Create HTTP server
const httpServer = http.createServer(app);
// Add error handlers before starting servers
httpServer.on("error", (error) => {
if (error.code === "EADDRINUSE") {
console.error(`\x1b[31mError: Port ${httpPort} is already in use\x1b[0m`);
console.log("\nTo fix this, you can:");
console.log(`1. Update the port manually by changing \x1b[33mhttpPort\x1b[0m in the code`);
console.log(`2. Close any other applications using port ${httpPort}`);
console.log(`3. Wait a few moments and try again - the port might be in a cleanup state\n`);
} else {
console.error("\x1b[31mHTTP Server error:\x1b[0m", error);
}
process.exit(1);
});
httpsServer.on("error", (error) => {
if (error.code === "EADDRINUSE") {
console.error(`\x1b[31mError: Port ${httpsPort} is already in use\x1b[0m`);
console.log("\nTo fix this, you can:");
console.log(`1. Update the port manually by changing \x1b[33mhttpsPort\x1b[0m in the code`);
console.log(`2. Close any other applications using port ${httpsPort}`);
console.log(`3. Wait a few moments and try again - the port might be in a cleanup state\n`);
} else {
console.error("\x1b[31mHTTP Server error:\x1b[0m", error);
}
process.exit(1);
});
// Start the servers
httpServer.listen(httpPort, () => {
console.log("\n\x1b[1m Dynamsoft Document Scanner Samples\x1b[0m\n");
console.log("\x1b[36m HTTP URLs:\x1b[0m");
console.log("\x1b[90m-------------------\x1b[0m");
console.log("\x1b[33m Hello World:\x1b[0m http://localhost:" + httpPort + "/hello-world");
console.log("\x1b[33m Demo:\x1b[0m http://localhost:" + httpPort + "/demo");
});
httpsServer.listen(httpsPort, "0.0.0.0", () => {
const networkInterfaces = os.networkInterfaces();
const ipv4Addresses = [];
Object.keys(networkInterfaces).forEach((interfaceName) => {
networkInterfaces[interfaceName].forEach((iface) => {
if (iface.family === "IPv4" && !iface.internal) {
ipv4Addresses.push(iface.address);
}
});
});
console.log("\n");
console.log("\x1b[36m HTTPS URLs:\x1b[0m");
console.log("\x1b[90m-------------------\x1b[0m");
ipv4Addresses.forEach((localIP) => {
console.log("\x1b[32m Hello World:\x1b[0m https://" + localIP + ":" + httpsPort + "/hello-world");
console.log("\x1b[32m Demo:\x1b[0m https://" + localIP + ":" + httpsPort + "/demo");
});
console.log("\n");
console.log("\x1b[90mPress Ctrl+C to stop the server\x1b[0m\n");
});