Skip to content

Commit

Permalink
修复EPUB格式
Browse files Browse the repository at this point in the history
fix Mimetype file entry is missing or is not the first file in the archive.
  • Loading branch information
yingziwu committed Apr 28, 2023
1 parent 573f55a commit cddc65e
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/save/epub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class EPUB extends Options {

public constructor(book: Book, streamZip: boolean, options?: SaveOptions) {
super();
const self = this;
this.book = book;
this.chapters = this.book.chapters;

Expand All @@ -236,9 +237,33 @@ export class EPUB extends Options {
"application/epub+zip"
);

log.debug("[save-epub]保存epub基本文件");
saveEpubMimetype();

if (options) {
Object.assign(this, options);
}

async function saveEpubMimetype() {
// mimetype
await self.epubZip.file(
"mimetype",
new Blob(["application/epub+zip"]),
true
);
// container.xml
await self.epubZip.file(
"META-INF/container.xml",
new Blob([
`<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>`,
])
);
}
}

private static genChapterHtmlFile(chapterObj: Chapter) {
Expand Down Expand Up @@ -269,8 +294,6 @@ export class EPUB extends Options {
public async saveEpub() {
const self = this;

log.debug("[save-epub]保存epub基本文件");
await saveEpubMimetype();
log.debug("[save-epub]保存样式文件");
await saveStyle();
log.debug("[save-epub]更新Metadata");
Expand All @@ -290,27 +313,6 @@ export class EPUB extends Options {

await this.epubZip.generateAsync();

async function saveEpubMimetype() {
// mimetype
await self.epubZip.file(
"mimetype",
new Blob(["application/epub+zip"]),
true
);
// container.xml
await self.epubZip.file(
"META-INF/container.xml",
new Blob([
`<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>`,
])
);
}

async function saveStyle() {
await self.epubZip.file(
"OEBPS/style.css",
Expand Down

0 comments on commit cddc65e

Please sign in to comment.