fix: TXT file correctly read and imported on desktop apps, closes #756 (#757)

This commit is contained in:
Huang Xin
2025-03-30 02:11:41 +08:00
committed by GitHub
parent 4acc0d8e12
commit ffd179bb06
23 changed files with 110 additions and 49 deletions
+3 -11
View File
@@ -1,3 +1,4 @@
import { getBaseFilename } from './book';
import { partialMD5 } from './md5';
interface Metadata {
@@ -38,12 +39,12 @@ export class TxtToEpubConverter {
public async convert(options: Txt2EpubOptions): Promise<ConversionResult> {
const { file: txtFile, author: providedAuthor, language: providedLanguage } = options;
const fileContent = await this.readFileAsArrayBuffer(txtFile);
const fileContent = await txtFile.arrayBuffer();
const detectedEncoding = this.detectEncoding(fileContent) || 'utf-8';
const decoder = new TextDecoder(detectedEncoding);
const txtContent = decoder.decode(fileContent).trim();
const bookTitle = this.extractBookTitle(txtFile.name);
const bookTitle = this.extractBookTitle(getBaseFilename(txtFile.name));
const fileName = `${bookTitle}.epub`;
const fileHeader = txtContent.slice(0, 1024);
@@ -358,15 +359,6 @@ export class TxtToEpubConverter {
return 'en';
}
private readFileAsArrayBuffer(file: File): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as ArrayBuffer);
reader.onerror = reject;
reader.readAsArrayBuffer(file);
});
}
private extractBookTitle(filename: string): string {
const match = filename.match(/《([^》]+)》/);
return match ? match[1]! : filename.split('.')[0]!;