|
|
@@ -115,25 +115,36 @@ RouterUtils.prototype.onPageNotFound = function(res) {
|
|
|
|
|
|
RouterUtils.prototype.staticServe = async function(res, filePath) {
|
|
|
return new Promise((ok, ko) => {
|
|
|
- const stream = fs.createReadStream(filePath);
|
|
|
- const fileSize = fs.statSync(filePath)?.size || undefined;
|
|
|
- if (!stream || !fileSize) {
|
|
|
- console.error("RouterUtils::staticGet", filePath, err);
|
|
|
- this.httpResponse(res, 500, "Internal Server Error");
|
|
|
- return ko(err);
|
|
|
+ try {
|
|
|
+ const stream = fs.createReadStream(filePath);
|
|
|
+ let onError = false;
|
|
|
+ stream.once('error', err => {
|
|
|
+ ko(err);
|
|
|
+ onError = true;
|
|
|
+ });
|
|
|
+ const fileSize = fs.statSync(filePath)?.size || undefined;
|
|
|
+ if (!stream || !fileSize || onError) {
|
|
|
+ console.error("RouterUtils::staticGet", filePath, err);
|
|
|
+ this.httpResponse(res, 500, "Internal Server Error");
|
|
|
+ return ko(err);
|
|
|
+ }
|
|
|
+ res.writeHead(200, {
|
|
|
+ "Content-Type": mime.contentType(path.basename(filePath)),
|
|
|
+ "Content-Length": fileSize
|
|
|
+ });
|
|
|
+ stream.pipe(res);
|
|
|
+ stream.once('end', () => ok());
|
|
|
+ } catch (err) {
|
|
|
+ ko(err);
|
|
|
}
|
|
|
- res.writeHead(200, {
|
|
|
- "Content-Type": mime.contentType(path.basename(filePath)),
|
|
|
- "Content-Length": fileSize
|
|
|
- });
|
|
|
- stream.pipe(res);
|
|
|
- stream.once('end', () => ok());
|
|
|
});
|
|
|
}
|
|
|
|
|
|
RouterUtils.prototype.staticGet = function(app, url, staticResources) {
|
|
|
app.router.get(url, (req, res) => {
|
|
|
- app.routerUtils.staticServe(res, staticResources);
|
|
|
+ app.routerUtils.staticServe(res, staticResources).catch(err => {
|
|
|
+ app.routerUtils.onPageNotFound(res);
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@@ -153,20 +164,9 @@ RouterUtils.prototype.decodeUrlComponent = function(input) {
|
|
|
return RouterUtils.decodeUrlComponent(input);
|
|
|
}
|
|
|
|
|
|
-RouterUtils.prototype.commonRenderInfos = function(endpointName) {
|
|
|
- let context = {
|
|
|
- page_title: CONFIG.sitename,
|
|
|
- endpoints: [],
|
|
|
- currentEndpoint: endpointName
|
|
|
+RouterUtils.prototype.commonRenderInfos = function() {
|
|
|
+ return {
|
|
|
};
|
|
|
- for (let endpoint in CONFIG.endpoints)
|
|
|
- context.endpoints.push({
|
|
|
- name: endpoint,
|
|
|
- address: '/dashboard/'+this.encodeUrlComponent(endpoint),
|
|
|
- selected: endpoint === endpointName,
|
|
|
- icon: CONFIG.endpoints[endpoint].icon
|
|
|
- });
|
|
|
- return context;
|
|
|
}
|
|
|
|
|
|
module.exports = { RouterUtils: RouterUtils, encodeUrlComponent: RouterUtils.encodeUrlComponent, decodeUrlComponent: RouterUtils.decodeUrlComponent };
|