fix(import/zip): ZIPs without language encoding flag importing garbage (closes #3013)

This commit is contained in:
Elian Doran
2026-04-07 17:01:34 +03:00
parent 28b2547229
commit d21557069c
3 changed files with 13 additions and 1 deletions

View File

@@ -71,6 +71,11 @@ describe("processNoteContent", () => {
expect(content).toContain(`<img src="api/images/${bananaNote!.noteId}/banana.jpeg`);
});
it("can import ZIP with UTF-8 filenames without language encoding flag", async () => {
const { importedNote } = await testImport("utf8-filename.zip");
expect(importedNote.title).toBe("测试");
});
it("can import old geomap notes", async () => {
const { importedNote } = await testImport("geomap.zip");
expect(importedNote.type).toBe("book");

View File

@@ -659,13 +659,20 @@ export function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise
export function readZipFile(buffer: Buffer, processEntryCallback: (zipfile: yauzl.ZipFile, entry: yauzl.Entry) => Promise<void>) {
return new Promise<void>((res, rej) => {
yauzl.fromBuffer(buffer, { lazyEntries: true, validateEntrySizes: false }, (err, zipfile) => {
yauzl.fromBuffer(buffer, { lazyEntries: true, validateEntrySizes: false, decodeStrings: false }, (err, zipfile) => {
if (err) rej(err);
if (!zipfile) throw new Error("Unable to read zip file.");
zipfile.readEntry();
zipfile.on("entry", async (entry) => {
try {
// yauzl with decodeStrings: false returns fileName as a Buffer.
// We decode as UTF-8 to handle ZIP files that use UTF-8 filenames
// without setting the general purpose bit flag 11 (language encoding flag).
if (Buffer.isBuffer(entry.fileName)) {
entry.fileName = (entry.fileName as Buffer).toString("utf-8");
}
await processEntryCallback(zipfile, entry);
} catch (e) {
rej(e);