mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
ZIP extraction error handling (#4002)
* Improve error handling in extractFileFromZipBuffer function * Add warning logging to NovelAI image upscaling
This commit is contained in:
@@ -394,7 +394,8 @@ router.post('/generate-image', async (request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!upscaleResult.ok) {
|
if (!upscaleResult.ok) {
|
||||||
throw new Error('NovelAI returned an error.');
|
const text = await upscaleResult.text();
|
||||||
|
throw new Error('NovelAI returned an error.', { cause: text });
|
||||||
}
|
}
|
||||||
|
|
||||||
const upscaledArchiveBuffer = await upscaleResult.arrayBuffer();
|
const upscaledArchiveBuffer = await upscaleResult.arrayBuffer();
|
||||||
@@ -408,7 +409,7 @@ router.post('/generate-image', async (request, response) => {
|
|||||||
|
|
||||||
return response.send(upscaledBase64);
|
return response.send(upscaledBase64);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('NovelAI generated an image, but upscaling failed. Returning original image.');
|
console.warn('NovelAI generated an image, but upscaling failed. Returning original image.', error);
|
||||||
return response.send(originalBase64);
|
return response.send(originalBase64);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
73
src/util.js
73
src/util.js
@@ -207,35 +207,58 @@ export function formatBytes(bytes) {
|
|||||||
* @returns {Promise<Buffer|null>} Buffer containing the extracted file. Null if the file was not found.
|
* @returns {Promise<Buffer|null>} Buffer containing the extracted file. Null if the file was not found.
|
||||||
*/
|
*/
|
||||||
export async function extractFileFromZipBuffer(archiveBuffer, fileExtension) {
|
export async function extractFileFromZipBuffer(archiveBuffer, fileExtension) {
|
||||||
return await new Promise((resolve, reject) => yauzl.fromBuffer(Buffer.from(archiveBuffer), { lazyEntries: true }, (err, zipfile) => {
|
return await new Promise((resolve) => {
|
||||||
if (err) reject(err);
|
try {
|
||||||
|
yauzl.fromBuffer(Buffer.from(archiveBuffer), { lazyEntries: true }, (err, zipfile) => {
|
||||||
|
if (err) {
|
||||||
|
console.warn(`Error opening ZIP file: ${err.message}`);
|
||||||
|
return resolve(null);
|
||||||
|
}
|
||||||
|
|
||||||
zipfile.readEntry();
|
zipfile.readEntry();
|
||||||
zipfile.on('entry', (entry) => {
|
|
||||||
if (entry.fileName.endsWith(fileExtension) && !entry.fileName.startsWith('__MACOSX')) {
|
zipfile.on('entry', (entry) => {
|
||||||
console.info(`Extracting ${entry.fileName}`);
|
if (entry.fileName.endsWith(fileExtension) && !entry.fileName.startsWith('__MACOSX')) {
|
||||||
zipfile.openReadStream(entry, (err, readStream) => {
|
console.info(`Extracting ${entry.fileName}`);
|
||||||
if (err) {
|
zipfile.openReadStream(entry, (err, readStream) => {
|
||||||
reject(err);
|
if (err) {
|
||||||
|
console.warn(`Error opening read stream: ${err.message}`);
|
||||||
|
return zipfile.readEntry();
|
||||||
|
} else {
|
||||||
|
const chunks = [];
|
||||||
|
readStream.on('data', (chunk) => {
|
||||||
|
chunks.push(chunk);
|
||||||
|
});
|
||||||
|
|
||||||
|
readStream.on('end', () => {
|
||||||
|
const buffer = Buffer.concat(chunks);
|
||||||
|
resolve(buffer);
|
||||||
|
zipfile.readEntry(); // Continue to the next entry
|
||||||
|
});
|
||||||
|
|
||||||
|
readStream.on('error', (err) => {
|
||||||
|
console.warn(`Error reading stream: ${err.message}`);
|
||||||
|
zipfile.readEntry();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
const chunks = [];
|
zipfile.readEntry();
|
||||||
readStream.on('data', (chunk) => {
|
|
||||||
chunks.push(chunk);
|
|
||||||
});
|
|
||||||
|
|
||||||
readStream.on('end', () => {
|
|
||||||
const buffer = Buffer.concat(chunks);
|
|
||||||
resolve(buffer);
|
|
||||||
zipfile.readEntry(); // Continue to the next entry
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
zipfile.readEntry();
|
zipfile.on('error', (err) => {
|
||||||
}
|
console.warn('ZIP processing error', err);
|
||||||
});
|
resolve(null);
|
||||||
zipfile.on('end', () => resolve(null));
|
});
|
||||||
}));
|
|
||||||
|
zipfile.on('end', () => resolve(null));
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Failed to process ZIP buffer', error);
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user