fix downloads

This commit is contained in:
Daniel Bulant 2021-05-22 11:37:58 +02:00
parent 542443ae7d
commit d42df90792

View file

@ -42,7 +42,6 @@
hashes.push(hash); hashes.push(hash);
} }
console.log(URLs);
text = "Found " + URLs.length + " pages"; text = "Found " + URLs.length + " pages";
const file = streamSaver.createWriteStream(`${manga.title.en} ${chapter.data.attributes.chapter}.epub`, { const file = streamSaver.createWriteStream(`${manga.title.en} ${chapter.data.attributes.chapter}.epub`, {
writableStrategy: undefined, // (optional) writableStrategy: undefined, // (optional)
@ -60,7 +59,6 @@
writer.write(data); writer.write(data);
} }
if(final) { if(final) {
console.log("Finished generating zip.");
writer.close(); writer.close();
} }
}; };
@ -78,13 +76,6 @@
</rootfiles> </rootfiles>
</container>`), true); </container>`), true);
/**
* @param {string} string
*/
function format(string) {
return string.substr(0, string.lastIndexOf(".")).replace(/-/g, "");
}
const opf = new ZipPassThrough("OEBPS/content.opf"); const opf = new ZipPassThrough("OEBPS/content.opf");
zip.add(opf); zip.add(opf);
opf.push(enc.encode(`<?xml version="1.0"?> opf.push(enc.encode(`<?xml version="1.0"?>
@ -96,8 +87,9 @@
<dc:language>en</dc:language> <dc:language>en</dc:language>
<dc:creator>Unknown</dc:creator> <dc:creator>Unknown</dc:creator>
<dc:identifier id="bookid">https://manga.danbulant.eu/${mangaId}/${chapter.data.id}</dc:identifier> <dc:identifier id="bookid">https://manga.danbulant.eu/${mangaId}/${chapter.data.id}</dc:identifier>
<dc:type>Image</dc:type>
<meta property="dcterms:modified">${chapter.data.attributes.updatedAt.toString().split("+")[0]}</meta> <meta property="dcterms:modified">${chapter.data.attributes.updatedAt.toString().split("+")[0]}Z</meta>
<meta property="rendition:layout">pre-paginated</meta> <meta property="rendition:layout">pre-paginated</meta>
<meta property="rendition:orientation">portrait</meta> <meta property="rendition:orientation">portrait</meta>
<meta property="rendition:spread">landscape</meta> <meta property="rendition:spread">landscape</meta>
@ -105,47 +97,51 @@
<manifest> <manifest>
<item id="fallback" href="fallback.xhtml" media-type="application/xhtml+xml" /> <item id="fallback" href="fallback.xhtml" media-type="application/xhtml+xml" />
${hashes.map(t => ` <item id="${format(t)}" href="${t}" fallback="fallback" media-type="image/${t.substr(t.lastIndexOf(".") + 1) === "jpg" ? "jpeg" : "png"}"/>`).join("\n")} ${hashes.map((t, i) => ` <item id="i${i}" href="${t}" fallback="fallback" media-type="image/${t.substr(t.lastIndexOf(".") + 1) === "jpg" ? "jpeg" : "png"}"/>`).join("\n")}
${hashes.map((t, i) => ` <item id="p${i}" href="${i}.xhtml" media-type="application/xhtml+xml" />`).join("\n")}
<item id="ncxtoc" href="toc.ncx" media-type="application/x-dtbncx+xml"/> <item id="ncxtoc" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
</manifest> </manifest>
<spine toc="ncxtoc"> <spine toc="ncxtoc">
${hashes.map((t, i) => ` <itemref idref="${format(t)}" properties="page-spread-${i % 2 ? "right" : "left"}" />`).join("\n")} ${hashes.map((t, i) => ` <itemref idref="p${i}" linear="yes" />`).join("\n")}
<itemref idref="fallback" linear="no" />
</spine> </spine>
</package>`), true); </package>`), true);
const ncx = new ZipPassThrough("OEBPS/toc.ncx"); const ncx = new ZipPassThrough("OEBPS/toc.ncx");
zip.add(ncx); zip.add(ncx);
ncx.push(enc.encode(`<?xml version="1.0" encoding="utf-8" standalone="no"?> ncx.push(enc.encode(`<?xml version="1.0" encoding="utf-8"?>
<ncx:ncx xmlns:ncx="http://www.daisy.org/z3986/2005/ncx/" version="2005-1"> <!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
<ncx:head> <ncx xmlns:ncx="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
<ncx:meta name="dtb:depth" content="-1"/> <head>
<ncx:meta name="dtb:totalPageCount" content="0"/> <meta name="dtb:depth" content="1"/>
<ncx:meta name="dtb:maxPageNumber" content="0"/> <meta name="dtb:totalPageCount" content="0"/>
</ncx:head> <meta name="dtb:maxPageNumber" content="0"/>
<ncx:docTitle> </head>
<ncx:text>${manga.title.en} ${chapter.data.attributes.chapter}</ncx:text> <docTitle>
</ncx:docTitle> <text>${manga.title.en} ${chapter.data.attributes.chapter}</text>
<ncx:docAuthor> </docTitle>
<ncx:text>Unknown</ncx:text> <docAuthor>
</ncx:docAuthor> <text>Unknown</text>
</docAuthor>
<ncx:navMap> <navMap>
<ncx:navPoint id="p01" playOrder="1"> ${hashes.map((t, i) => `
<ncx:navLabel> <navPoint id="p${i}" playOrder="${i + 1}">
<ncx:text>${manga.title.en} ${chapter.data.attributes.chapter}</ncx:text> <navLabel>
</ncx:navLabel> <text>${manga.title.en} ${chapter.data.attributes.chapter} ${i}</text>
<ncx:content src="${hashes[0]}"/> </navLabel>
</ncx:navPoint> <content src="${i}.xhtml"/>
</ncx:navMap> </navPoint>
</ncx:ncx>`), true); `).join("\n")}
</navMap>
</ncx>`), true);
const fallback = new ZipPassThrough("OEBPS/fallback.xhtml"); const fallback = new ZipPassThrough("OEBPS/fallback.xhtml");
zip.add(fallback); zip.add(fallback);
fallback.push(enc.encode(`<?xml version="1.0" encoding="UTF-8"?> fallback.push(enc.encode(`<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="file:/C:/EPub/epub-revision/build/30/schema/epub-nav-30.rnc" type="application/relax-ng-compact-syntax"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head> <head>
<title>${manga.title.en} ${chapter.data.attributes.chapter}</title> <title>${manga.title.en} ${chapter.data.attributes.chapter}</title>
@ -153,6 +149,15 @@
<body> <body>
<h2>This book cannot be opened on this device or using this program</h2> <h2>This book cannot be opened on this device or using this program</h2>
<p>We're sorry</p> <p>We're sorry</p>
<nav epub:type="toc">
<h1>Chapter list</h1>
<ol>
<li>
<a href="p0.xhtml">Chapter ${chapter.data.attributes.chapter}</a>
</li>
</ol>
</nav>
</body> </body>
</html>`), true); </html>`), true);
@ -165,6 +170,19 @@
const image = new ZipPassThrough("OEBPS/" + hash); const image = new ZipPassThrough("OEBPS/" + hash);
zip.add(image); zip.add(image);
image.push(new Uint8Array(await res.arrayBuffer()), true); image.push(new Uint8Array(await res.arrayBuffer()), true);
const textContent = new ZipPassThrough("OEBPS/" + i + ".xhtml");
zip.add(textContent);
textContent.push(enc.encode(`<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Page ${i + 1}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="EPB-UUID" content=""/>
</head>
<body>
<img style="margin:auto;height:100%;" src="${hash}" />
</body>
</html>`), true);
} }
zip.end(); zip.end();