mirror of
https://github.com/zadam/trilium.git
synced 2026-05-07 05:47:26 +02:00
fix(import/zip): ZIPs without language encoding flag importing garbage (closes #3013)
This commit is contained in:
BIN
apps/server/src/services/import/samples/utf8-filename.zip
Normal file
BIN
apps/server/src/services/import/samples/utf8-filename.zip
Normal file
Binary file not shown.
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user