From: Niki Roo Date: Thu, 1 Mar 2018 18:45:12 +0000 (+0100) Subject: Fix epub compatibility + cover image ext X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=ecfb936ef1c22ff75a55d8fc80e9daf767a55f34;p=nikiroo-utils.git Fix epub compatibility + cover image ext --- diff --git a/src/be/nikiroo/fanfix/library/LocalLibrary.java b/src/be/nikiroo/fanfix/library/LocalLibrary.java index 5e0ad40..97863e7 100644 --- a/src/be/nikiroo/fanfix/library/LocalLibrary.java +++ b/src/be/nikiroo/fanfix/library/LocalLibrary.java @@ -205,7 +205,7 @@ public class LocalLibrary extends BasicLibrary { @Override public void setSourceCover(String source, String luid) { sourceCovers.put(source, getCover(luid)); - File cover = new File(getExpectedDir(source), ".cover.png"); + File cover = new File(getExpectedDir(source), ".cover"); try { Instance.getCache().saveAsImage(sourceCovers.get(source), cover, true); @@ -519,7 +519,7 @@ public class LocalLibrary extends BasicLibrary { */ void setSourceCover(String source, Image coverImage) { sourceCovers.put(source, coverImage); - File cover = new File(getExpectedDir(source), ".cover.png"); + File cover = new File(getExpectedDir(source), ".cover"); try { Instance.getCache().saveAsImage(sourceCovers.get(source), cover, true); diff --git a/src/be/nikiroo/fanfix/output/BasicOutput.java b/src/be/nikiroo/fanfix/output/BasicOutput.java index 5109cce..7ac8cdf 100644 --- a/src/be/nikiroo/fanfix/output/BasicOutput.java +++ b/src/be/nikiroo/fanfix/output/BasicOutput.java @@ -328,7 +328,7 @@ public abstract class BasicOutput { String chapterNameNum = String.format("%03d", 0); String paragraphNumber = String.format("%04d", 0); - imageName = paragraphNumber + "_" + chapterNameNum + ".png"; + imageName = paragraphNumber + "_" + chapterNameNum; if (story.getMeta() != null) { story.getMeta().setType("" + getType()); diff --git a/src/be/nikiroo/fanfix/output/Epub.java b/src/be/nikiroo/fanfix/output/Epub.java index 09a9944..2129758 100644 --- a/src/be/nikiroo/fanfix/output/Epub.java +++ b/src/be/nikiroo/fanfix/output/Epub.java @@ -2,10 +2,14 @@ package be.nikiroo.fanfix.output; import java.io.BufferedWriter; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.Config; @@ -53,6 +57,37 @@ class Epub extends BasicOutput { // zip/epub File epub = new File(targetDir, targetName); IOUtils.zip(tmpDir, epub, true); + + OutputStream out = new FileOutputStream(epub); + try { + ZipOutputStream zip = new ZipOutputStream(out); + try { + // "mimetype" MUST be the first element and not compressed + zip.setLevel(ZipOutputStream.STORED); + File mimetype = new File(tmpDir, "mimetype"); + IOUtils.writeSmallFile(tmpDir, "mimetype", + "application/epub+zip"); + ZipEntry entry = new ZipEntry("mimetype"); + entry.setExtra(new byte[] {}); + zip.putNextEntry(entry); + FileInputStream in = new FileInputStream(mimetype); + try { + IOUtils.write(in, zip); + } finally { + in.close(); + } + IOUtils.deltree(mimetype); + zip.setLevel(ZipOutputStream.DEFLATED); + // + + IOUtils.zip(zip, "", tmpDir, true); + } finally { + zip.close(); + } + } finally { + out.close(); + } + IOUtils.deltree(tmpDir); tmpDir = null; @@ -75,11 +110,8 @@ class Epub extends BasicOutput { File metaInf = new File(tmpDir, "META-INF"); metaInf.mkdirs(); - // "root" - IOUtils.writeSmallFile(tmpDir, "mimetype", "application/epub+zip"); - // META-INF - String containerContent = "\n" + String containerContent = "\n" + "\n" + "\t\n" + "\t\t\n" @@ -100,9 +132,7 @@ class Epub extends BasicOutput { // OPS/images if (story.getMeta() != null && story.getMeta().getCover() != null) { - String format = Instance.getConfig() - .getString(Config.IMAGE_FORMAT_COVER).toLowerCase(); - File file = new File(images, "cover." + format); + File file = new File(images, "cover"); Instance.getCache().saveAsImage(story.getMeta().getCover(), file, true); } @@ -110,7 +140,7 @@ class Epub extends BasicOutput { // OPS/* except chapters IOUtils.writeSmallFile(ops, "epb.ncx", generateNcx(story)); IOUtils.writeSmallFile(ops, "epb.opf", generateOpf(story)); - IOUtils.writeSmallFile(ops, "title.xml", generateTitleXml(story)); + IOUtils.writeSmallFile(ops, "title.xhtml", generateTitleXml(story)); // Resume if (story.getMeta() != null && story.getMeta().getResume() != null) { @@ -121,7 +151,7 @@ class Epub extends BasicOutput { @Override protected void writeChapterHeader(Chapter chap) throws IOException { String filename = String.format("%s%03d%s", "chapter-", - chap.getNumber(), ".xml"); + chap.getNumber(), ".xhtml"); writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(new File(tmpDir + File.separator + "OPS", filename)), "UTF-8")); @@ -135,9 +165,9 @@ class Epub extends BasicOutput { nameOrNum = chap.getName(); } - writer.write(""); - writer.write("\n"); - writer.write("\n"); + writer.append(""); + writer.append("\n"); + writer.append("\n"); writer.write("\n"); writer.write("\n " + StringUtils.xmlEscape(title) + ""); @@ -211,7 +241,7 @@ class Epub extends BasicOutput { File file = new File(images, getCurrentImageBestName(false)); Instance.getCache().saveAsImage(para.getContentImage(), file, nextParaIsCover); - writer.write(" "); break; } @@ -270,7 +300,7 @@ class Epub extends BasicOutput { title = meta.getTitle(); } - builder.append(""); + builder.append(""); builder.append("\n"); builder.append("\n"); @@ -298,7 +328,7 @@ class Epub extends BasicOutput { builder.append("\n "); builder.append("\n Title Page"); builder.append("\n "); - builder.append("\n "); + builder.append("\n "); builder.append("\n "); int navPoint = 2; // 1 is above @@ -335,7 +365,7 @@ class Epub extends BasicOutput { builder.append("\n "); builder.append("\n " + name + ""); builder.append("\n "); - builder.append("\n "); + builder.append("\n "); builder.append("\n \n"); } @@ -362,9 +392,8 @@ class Epub extends BasicOutput { lang = meta.getLang(); } - builder.append(""); - builder.append("\n"); + builder.append(""); + builder.append("\n"); builder.append("\n "); builder.append("\n " + StringUtils.xmlEscape(title) @@ -382,20 +411,20 @@ class Epub extends BasicOutput { builder.append("\n " + StringUtils.xmlEscape(source) + ""); builder.append("\n Not for commercial use."); - builder.append("\n " + builder.append("\n " + StringUtils.xmlEscape(uuid) + ""); builder.append("\n " + StringUtils.xmlEscape(lang) + ""); builder.append("\n "); builder.append("\n "); builder.append("\n "); - builder.append("\n "); + builder.append("\n "); for (int i = 0; i <= story.getChapters().size(); i++) { String name = String.format("%s%03d", "chapter-", i); builder.append("\n "); + + ".xhtml\" media-type=\"application/xhtml+xml\"/>"); } builder.append("\n "); @@ -453,9 +482,9 @@ class Epub extends BasicOutput { String format = Instance.getConfig() .getString(Config.IMAGE_FORMAT_COVER).toLowerCase(); - builder.append(""); - builder.append("\n"); - builder.append("\n"); + builder.append(""); + builder.append("\n"); + builder.append("\n"); builder.append("\n"); builder.append("\n " + StringUtils.xmlEscape(title) + ""); builder.append("\n "); @@ -466,7 +495,8 @@ class Epub extends BasicOutput { builder.append("\n
" + StringUtils.xmlEscape(tags) + "
"); builder.append("\n
"); - builder.append("\n "); + builder.append("\n \"cover"); builder.append("\n
"); builder.append("\n
" + StringUtils.xmlEscape(author) + "
");