Skip to content

Commit e19488e

Browse files
committed
fix: Additional safety checks around image file handler
1 parent f56fb64 commit e19488e

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/back/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,8 +1453,7 @@ async function onFileServerRequestImages(pathname: string, url: URL, req: http.I
14531453
}
14541454
res.writeHead(400);
14551455
res.end();
1456-
}
1457-
else if (req.method === 'GET' || req.method === 'HEAD') {
1456+
} else if (req.method === 'GET' || req.method === 'HEAD') {
14581457
req.on('error', (err) => {
14591458
log.error('Launcher', `Error serving Game image - ${err}`);
14601459
res.writeHead(500);
@@ -1482,7 +1481,7 @@ async function onFileServerRequestImages(pathname: string, url: URL, req: http.I
14821481
.catch(async (err) => {
14831482
if (err.code !== 'ENOENT') {
14841483
// Can't read file
1485-
res.writeHead(404);
1484+
res.writeHead(500);
14861485
res.end();
14871486
} else {
14881487
// File missing
@@ -1511,7 +1510,7 @@ async function onFileServerRequestImages(pathname: string, url: URL, req: http.I
15111510
req.once('close', () => { item.cancelled = true; });
15121511
updateFileServerDownloadQueue()
15131512
.catch((err) => {
1514-
log.error('Launcher', 'Somethign really broke in updateFileServerDownloadQueue: ' + err);
1513+
log.error('Launcher', 'Something really broke in updateFileServerDownloadQueue: ' + err);
15151514
});
15161515
}
15171516
}
@@ -1658,9 +1657,22 @@ async function updateFileServerDownloadQueue() {
16581657

16591658
const imageFolder = path.join(state.config.flashpointPath, state.preferences.imageFolderPath);
16601659
const filePath = path.join(imageFolder, item.subPath);
1660+
const dirPath = path.dirname(filePath);
16611661

1662-
await fs.ensureDir(path.dirname(filePath));
1663-
await fs.promises.writeFile(filePath, imageData, 'binary');
1662+
try {
1663+
await fs.ensureDir(dirPath);
1664+
} catch {
1665+
log.error('Images', 'Failed to create folder for on-demand image: ' + dirPath);
1666+
item.res.writeHead(500);
1667+
return;
1668+
}
1669+
try {
1670+
await fs.promises.writeFile(filePath, imageData, 'binary');
1671+
} catch {
1672+
log.error('Images', 'Failed to save file for on-demand image: ' + filePath);
1673+
item.res.writeHead(500);
1674+
return;
1675+
}
16641676

16651677
item.res.writeHead(200);
16661678
item.res.write(imageData);
@@ -1675,7 +1687,9 @@ async function updateFileServerDownloadQueue() {
16751687
}
16761688

16771689
async function removeFileServerDownloadItem(item: ImageDownloadItem): Promise<void> {
1678-
item.res.end();
1690+
if (!item.res.writableEnded && !item.res.destroyed) {
1691+
item.res.end();
1692+
}
16791693

16801694
// Remove item from current
16811695
const index = state.fileServerDownloads.current.indexOf(item);

0 commit comments

Comments
 (0)