From 2aac79c740789071ad9b773d25f20e103f0da86c Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 26 Mar 2018 21:10:55 +0200 Subject: [PATCH] Small fixes: - tests: test files updated after fix - tests: use nikiroo-utils for unzip - fix TempFiles usage in fanfix and tests - CBZ: load the included story file if any before the images --- src/be/nikiroo/fanfix/Instance.java | 19 +++ src/be/nikiroo/fanfix/Main.java | 8 ++ src/be/nikiroo/fanfix/output/Cbz.java | 5 +- src/be/nikiroo/fanfix/output/Epub.java | 75 ++++++------ src/be/nikiroo/fanfix/supported/Cbz.java | 115 ++++++++++++------ src/be/nikiroo/fanfix/supported/Epub.java | 13 +- .../nikiroo/fanfix/test/ConversionTest.java | 62 ++-------- src/be/nikiroo/fanfix/test/Test.java | 59 +++++---- test/expected/cbz.cbz | Bin 1409 -> 1409 bytes test/expected/epub.epub | Bin 6261 -> 6261 bytes 10 files changed, 199 insertions(+), 157 deletions(-) diff --git a/src/be/nikiroo/fanfix/Instance.java b/src/be/nikiroo/fanfix/Instance.java index ed22f62..ece8718 100644 --- a/src/be/nikiroo/fanfix/Instance.java +++ b/src/be/nikiroo/fanfix/Instance.java @@ -16,6 +16,7 @@ import be.nikiroo.fanfix.library.LocalLibrary; import be.nikiroo.fanfix.library.RemoteLibrary; import be.nikiroo.utils.Cache; import be.nikiroo.utils.IOUtils; +import be.nikiroo.utils.TempFiles; import be.nikiroo.utils.TraceHandler; import be.nikiroo.utils.resources.Bundles; @@ -35,6 +36,7 @@ public class Instance { private static File remoteDir; private static String configDir; private static TraceHandler tracer; + private static TempFiles tempFiles; static { // Before we can configure it: @@ -99,6 +101,12 @@ public class Instance { + coverDir)); coverDir = null; } + + try { + tempFiles = new TempFiles("fanfix"); + } catch (IOException e) { + tracer.error(new IOException("Cannot create temporary directory", e)); + } } /** @@ -304,6 +312,17 @@ public class Instance { } } + /** + * The facility to use temporary files in this program. + *

+ * MUST be closed at end of program. + * + * @return the facility + */ + public static TempFiles getTempFiles() { + return tempFiles; + } + /** * The configuration directory (will check, in order of preference, * {@link Bundles#getDirectory()}, the system properties, the environment diff --git a/src/be/nikiroo/fanfix/Main.java b/src/be/nikiroo/fanfix/Main.java index e1b9c65..42f3924 100644 --- a/src/be/nikiroo/fanfix/Main.java +++ b/src/be/nikiroo/fanfix/Main.java @@ -351,6 +351,14 @@ public class Main { } } + try { + Instance.getTempFiles().close(); + } catch (IOException e) { + Instance.getTraceHandler() + .error(new IOException( + "Cannot dispose of the temporary files", e)); + } + if (exitCode == 255) { syntax(false); } diff --git a/src/be/nikiroo/fanfix/output/Cbz.java b/src/be/nikiroo/fanfix/output/Cbz.java index 4a89d9b..490ba8f 100644 --- a/src/be/nikiroo/fanfix/output/Cbz.java +++ b/src/be/nikiroo/fanfix/output/Cbz.java @@ -6,6 +6,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; +import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Paragraph; import be.nikiroo.fanfix.data.Story; @@ -22,9 +23,7 @@ class Cbz extends BasicOutput { File target = new File(targetDir, targetName); - dir = File.createTempFile("fanfic-reader-cbz-dir", ".wip"); - dir.delete(); - dir.mkdir(); + dir = Instance.getTempFiles().createTempDir("fanfic-reader-cbz-dir"); try { // will also save the images! (except the cover -> false) BasicOutput diff --git a/src/be/nikiroo/fanfix/output/Epub.java b/src/be/nikiroo/fanfix/output/Epub.java index 64797d3..b56cc33 100644 --- a/src/be/nikiroo/fanfix/output/Epub.java +++ b/src/be/nikiroo/fanfix/output/Epub.java @@ -36,7 +36,7 @@ class Epub extends BasicOutput { String targetNameOrig = targetName; targetName += getDefaultExtension(false); - tmpDir = File.createTempFile("fanfic-reader-epub_", ".wip"); + tmpDir = Instance.getTempFiles().createTempDir("fanfic-reader-epub"); tmpDir.delete(); if (!tmpDir.mkdir()) { @@ -46,51 +46,54 @@ class Epub extends BasicOutput { super.process(story, targetDir, targetNameOrig); - // "Originals" - File data = new File(tmpDir, "DATA"); - data.mkdir(); - BasicOutput.getOutput(OutputType.TEXT, isWriteInfo(), isWriteCover()) - .process(story, data, targetNameOrig); - InfoCover.writeInfo(data, targetNameOrig, story.getMeta()); - IOUtils.writeSmallFile(data, "version", "3.0"); - - // zip/epub - File epub = new File(targetDir, targetName); - IOUtils.zip(tmpDir, epub, true); - - OutputStream out = new FileOutputStream(epub); + File epub = null; try { - ZipOutputStream zip = new ZipOutputStream(out); + // "Originals" + File data = new File(tmpDir, "DATA"); + data.mkdir(); + BasicOutput.getOutput(OutputType.TEXT, isWriteInfo(), + isWriteCover()).process(story, data, targetNameOrig); + InfoCover.writeInfo(data, targetNameOrig, story.getMeta()); + IOUtils.writeSmallFile(data, "version", "3.0"); + + // zip/epub + epub = new File(targetDir, targetName); + IOUtils.zip(tmpDir, epub, true); + + OutputStream out = new FileOutputStream(epub); 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); + ZipOutputStream zip = new ZipOutputStream(out); try { - IOUtils.write(in, zip); + // "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 { - in.close(); + zip.close(); } - IOUtils.deltree(mimetype); - zip.setLevel(ZipOutputStream.DEFLATED); - // - - IOUtils.zip(zip, "", tmpDir, true); } finally { - zip.close(); + out.close(); } } finally { - out.close(); + IOUtils.deltree(tmpDir); + tmpDir = null; } - IOUtils.deltree(tmpDir); - tmpDir = null; - return epub; } diff --git a/src/be/nikiroo/fanfix/supported/Cbz.java b/src/be/nikiroo/fanfix/supported/Cbz.java index 8ab2a52..f635a17 100644 --- a/src/be/nikiroo/fanfix/supported/Cbz.java +++ b/src/be/nikiroo/fanfix/supported/Cbz.java @@ -1,5 +1,6 @@ package be.nikiroo.fanfix.supported; +import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; @@ -14,6 +15,7 @@ import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.Paragraph; import be.nikiroo.fanfix.data.Story; +import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; @@ -67,59 +69,94 @@ class Cbz extends Epub { Story story = processMeta(url, false, true, pgMeta); pgMeta.done(); // 10% - story.setChapters(new ArrayList()); - Chapter chap = new Chapter(1, null); - story.getChapters().add(chap); - - ZipInputStream zipIn = new ZipInputStream(getInput()); + File tmpDir = Instance.getTempFiles().createTempDir("info-text"); + String basename = null; Map images = new HashMap(); - for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn - .getNextEntry()) { - if (!entry.isDirectory() - && entry.getName().startsWith(getDataPrefix())) { - String entryLName = entry.getName().toLowerCase(); - boolean imageEntry = false; - for (String ext : getImageExt(false)) { - if (entryLName.endsWith(ext)) { - imageEntry = true; + try { + ZipInputStream zipIn = new ZipInputStream(getInput()); + for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn + .getNextEntry()) { + if (!entry.isDirectory() + && entry.getName().startsWith(getDataPrefix())) { + String entryLName = entry.getName().toLowerCase(); + boolean imageEntry = false; + for (String ext : getImageExt(false)) { + if (entryLName.endsWith(ext)) { + imageEntry = true; + } } - } - if (imageEntry) { - String uuid = meta.getUuid() + "_" + entry.getName(); - try { - images.put(uuid, new Image(zipIn)); - } catch (Exception e) { - Instance.getTraceHandler().error(e); + if (imageEntry) { + String uuid = meta.getUuid() + "_" + entry.getName(); + try { + images.put(uuid, new Image(zipIn)); + } catch (Exception e) { + Instance.getTraceHandler().error(e); + } + + if (pg.getProgress() < 85) { + pg.add(1); + } + } else if (entryLName.endsWith(".info")) { + basename = entryLName.substring(0, entryLName.length() + - ".info".length()); + IOUtils.write(zipIn, new File(tmpDir, entryLName)); + } else if (entryLName.endsWith(".txt")) { + IOUtils.write(zipIn, new File(tmpDir, entryLName)); } + } + } - if (pg.getProgress() < 85) { - pg.add(1); - } + pg.setProgress(85); + + // ZIP order is not correct for us + List imagesList = new ArrayList(images.keySet()); + Collections.sort(imagesList); + + pg.setProgress(90); + + File txt = new File(tmpDir, basename + ".txt"); + if (!txt.exists()) { + basename = null; + } + if (basename != null) { + try { + BasicSupport support = BasicSupport.getSupport(txt.toURI() + .toURL()); + Story origStory = support.process(null); + story.setChapters(origStory.getChapters()); + story.setMeta(origStory.getMeta()); + } catch (Exception e) { + basename = null; } } - } - pg.setProgress(85); + if (basename == null) { + story.setChapters(new ArrayList()); + } - // ZIP order is not correct for us - List imagesList = new ArrayList(images.keySet()); - Collections.sort(imagesList); + if (!imagesList.isEmpty()) { + Chapter chap = new Chapter(story.getChapters().size() + 1, null); + story.getChapters().add(chap); - pg.setProgress(90); + for (String uuid : imagesList) { + try { + chap.getParagraphs().add( + new Paragraph(images.get(uuid))); + } catch (Exception e) { + Instance.getTraceHandler().error(e); + } + } + } - for (String uuid : imagesList) { - try { - chap.getParagraphs().add(new Paragraph(images.get(uuid))); - } catch (Exception e) { - Instance.getTraceHandler().error(e); + if (meta.getCover() == null && !images.isEmpty()) { + meta.setCover(images.get(imagesList.get(0))); + meta.setFakeCover(true); } - } - if (meta.getCover() == null && !images.isEmpty()) { - meta.setCover(images.get(imagesList.get(0))); - meta.setFakeCover(true); + } finally { + IOUtils.deltree(tmpDir); } pg.setProgress(100); diff --git a/src/be/nikiroo/fanfix/supported/Epub.java b/src/be/nikiroo/fanfix/supported/Epub.java index ce9bd06..794998e 100644 --- a/src/be/nikiroo/fanfix/supported/Epub.java +++ b/src/be/nikiroo/fanfix/supported/Epub.java @@ -28,6 +28,7 @@ import be.nikiroo.utils.StringUtils; */ class Epub extends InfoText { protected MetaData meta; + private File tmpDir; private File tmp; private String desc; @@ -93,8 +94,9 @@ class Epub extends InfoText { protected void preprocess(URL source, InputStream in) throws IOException { // Note: do NOT close this stream, as it would also close "in" ZipInputStream zipIn = new ZipInputStream(in); - tmp = File.createTempFile("fanfic-reader-parser_", ".tmp"); - File tmpInfo = new File(tmp + ".info"); + tmpDir = Instance.getTempFiles().createTempDir("fanfic-reader-parser"); + tmp = new File(tmpDir, "file.txt"); + File tmpInfo = new File(tmpDir, "file.info"); fakeSource = tmp.toURI().toURL(); Image cover = null; @@ -202,12 +204,11 @@ class Epub extends InfoText { @Override protected void close() { - if (tmp != null && tmp.exists()) { - if (!tmp.delete()) { - tmp.deleteOnExit(); - } + if (tmpDir != null) { + IOUtils.deltree(tmpDir); } + tmpDir = null; tmp = null; if (fakeIn != null) { diff --git a/src/be/nikiroo/fanfix/test/ConversionTest.java b/src/be/nikiroo/fanfix/test/ConversionTest.java index 4fe1653..51b6deb 100644 --- a/src/be/nikiroo/fanfix/test/ConversionTest.java +++ b/src/be/nikiroo/fanfix/test/ConversionTest.java @@ -1,29 +1,23 @@ package be.nikiroo.fanfix.test; import java.io.File; -import java.io.FileInputStream; import java.io.FilenameFilter; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.Main; import be.nikiroo.fanfix.output.BasicOutput; import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.TempFiles; import be.nikiroo.utils.TraceHandler; import be.nikiroo.utils.test.TestCase; import be.nikiroo.utils.test.TestLauncher; class ConversionTest extends TestLauncher { - private TempFiles tempFiles; private File testFile; private File expectedDir; private File resultDir; @@ -72,8 +66,6 @@ class ConversionTest extends TestLauncher { expectedDir = new File("test/expected/"); resultDir = new File("test/result/"); - tempFiles = new TempFiles("Fanfix-ConversionTest"); - skipCompare = new HashMap>(); skipCompare.put("epb.ncx", Arrays.asList(" expectedLines = Arrays.asList(IOUtils @@ -223,6 +215,11 @@ class ConversionTest extends TestLauncher { + name.substring(expectedDir.getAbsolutePath() .length()); } + + testCase.assertEquals(errMess + ": " + name + + ": the number of lines is not the same", + expectedLines.size(), resultLines.size()); + for (int j = 0; j < expectedLines.size(); j++) { String expectedLine = expectedLines.get(j); String resultLine = resultLines.get(j); @@ -250,39 +247,4 @@ class ConversionTest extends TestLauncher { } } } - - // TODO: remove and use IOUtils when updated - private static void unzip(File zipFile, File targetDirectory) - throws IOException { - if (targetDirectory.exists() && targetDirectory.isFile()) { - throw new IOException("Cannot unzip " + zipFile + " into " - + targetDirectory + ": it is not a directory"); - } - - targetDirectory.mkdir(); - if (!targetDirectory.exists()) { - throw new IOException("Cannot create target directory " - + targetDirectory); - } - - FileInputStream in = new FileInputStream(zipFile); - try { - ZipInputStream zipStream = new ZipInputStream(in); - try { - for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream - .getNextEntry()) { - File file = new File(targetDirectory, entry.getName()); - if (entry.isDirectory()) { - file.mkdirs(); - } else { - IOUtils.write(zipStream, file); - } - } - } finally { - zipStream.close(); - } - } finally { - in.close(); - } - } } diff --git a/src/be/nikiroo/fanfix/test/Test.java b/src/be/nikiroo/fanfix/test/Test.java index dd412ec..614cec1 100644 --- a/src/be/nikiroo/fanfix/test/Test.java +++ b/src/be/nikiroo/fanfix/test/Test.java @@ -8,6 +8,7 @@ import java.util.Properties; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.ConfigBundle; import be.nikiroo.utils.IOUtils; +import be.nikiroo.utils.TempFiles; import be.nikiroo.utils.resources.Bundles; import be.nikiroo.utils.test.TestLauncher; @@ -17,6 +18,11 @@ import be.nikiroo.utils.test.TestLauncher; * @author niki */ public class Test extends TestLauncher { + /** + * The temporary files handler. + */ + static TempFiles tempFiles; + /** * Create the Fanfix {@link TestLauncher}. * @@ -41,35 +47,42 @@ public class Test extends TestLauncher { * in case of I/O error */ static public void main(String[] args) throws IOException { - File tmpConfig = File.createTempFile("fanfix-config_", ".test"); - File tmpCache = File.createTempFile("fanfix-cache_", ".test"); - tmpConfig.delete(); - tmpConfig.mkdir(); - tmpCache.delete(); - tmpCache.mkdir(); - - FileOutputStream out = null; + int result = 0; + tempFiles = new TempFiles("fanfix-test"); try { - out = new FileOutputStream(new File(tmpConfig, "config.properties")); - Properties props = new Properties(); - props.setProperty("CACHE_DIR", tmpCache.getAbsolutePath()); - props.store(out, null); - } finally { - if (out != null) { - out.close(); + File tmpConfig = tempFiles.createTempDir("fanfix-config"); + File tmpCache = tempFiles.createTempDir("fanfix-cache"); + + FileOutputStream out = null; + try { + out = new FileOutputStream(new File(tmpConfig, + "config.properties")); + Properties props = new Properties(); + props.setProperty("CACHE_DIR", tmpCache.getAbsolutePath()); + props.store(out, null); + } finally { + if (out != null) { + out.close(); + } } - } - ConfigBundle config = new ConfigBundle(); - Bundles.setDirectory(tmpConfig.getAbsolutePath()); - config.updateFile(tmpConfig.getPath()); + ConfigBundle config = new ConfigBundle(); + Bundles.setDirectory(tmpConfig.getAbsolutePath()); + config.updateFile(tmpConfig.getPath()); - System.setProperty("CONFIG_DIR", tmpConfig.getAbsolutePath()); + System.setProperty("CONFIG_DIR", tmpConfig.getAbsolutePath()); - int result = new Test(args).launch(); + result = new Test(args).launch(); - IOUtils.deltree(tmpConfig); - IOUtils.deltree(tmpCache); + IOUtils.deltree(tmpConfig); + IOUtils.deltree(tmpCache); + } finally { + // Test temp files + tempFiles.close(); + + // This is usually done in Fanfix.Main: + Instance.getTempFiles().close(); + } System.exit(result); } diff --git a/test/expected/cbz.cbz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