mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 06:27:38 +00:00
fix(webui): handling of file serving (#1383)
This commit is contained in:
+21
-4
@@ -8,7 +8,8 @@ import {
|
|||||||
storeList,
|
storeList,
|
||||||
updateStores
|
updateStores
|
||||||
} from '../store/model';
|
} from '../store/model';
|
||||||
import {join, normalize} from 'path';
|
import {isAbsolute, join, normalize, relative} from 'path';
|
||||||
|
import {logger} from '../logger';
|
||||||
|
|
||||||
const approot = join(__dirname, '../../');
|
const approot = join(__dirname, '../../');
|
||||||
const webroot = join(approot, './web');
|
const webroot = join(approot, './web');
|
||||||
@@ -33,15 +34,30 @@ function sendFile(
|
|||||||
path = normalize(`./${path}`);
|
path = normalize(`./${path}`);
|
||||||
|
|
||||||
const fsPath = join(relativeTo, path);
|
const fsPath = join(relativeTo, path);
|
||||||
|
|
||||||
|
const relPath = relative(relativeTo, fsPath);
|
||||||
|
if (!relPath || relPath.startsWith('..') || isAbsolute(relPath)) {
|
||||||
|
sendError(response, 'Bad request', 400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const stream = createReadStream(fsPath);
|
const stream = createReadStream(fsPath);
|
||||||
stream.on('error', (error) => {
|
|
||||||
sendError(response, error.message);
|
let responseDead = false;
|
||||||
|
stream.on('error', (err) => {
|
||||||
|
responseDead = true;
|
||||||
|
logger.error(`Error in WebUI stream ${err.message}`);
|
||||||
|
sendError(response, 'Not found', 404);
|
||||||
});
|
});
|
||||||
|
|
||||||
const pathSplit = path.split('.');
|
const pathSplit = path.split('.');
|
||||||
const ext = pathSplit[pathSplit.length - 1].toLowerCase();
|
const ext = pathSplit[pathSplit.length - 1].toLowerCase();
|
||||||
|
|
||||||
|
if (responseDead) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
response.setHeader(
|
response.setHeader(
|
||||||
'Content-Type',
|
'Content-Type',
|
||||||
contentTypeMap[ext] ?? contentTypeMap.txt
|
contentTypeMap[ext] ?? contentTypeMap.txt
|
||||||
@@ -50,7 +66,8 @@ function sendFile(
|
|||||||
stream.on('end', () => response.end());
|
stream.on('end', () => response.end());
|
||||||
stream.pipe(response);
|
stream.pipe(response);
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
sendError(response, (error as Error).message);
|
logger.error(`Error in WebUI ${(error as Error).message}`);
|
||||||
|
sendError(response, 'Internal server error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user