Small fixes:
authorNiki Roo <niki@nikiroo.be>
Mon, 26 Mar 2018 19:10:55 +0000 (21:10 +0200)
committerNiki Roo <niki@nikiroo.be>
Mon, 26 Mar 2018 19:10:55 +0000 (21:10 +0200)
- 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
src/be/nikiroo/fanfix/Main.java
src/be/nikiroo/fanfix/output/Cbz.java
src/be/nikiroo/fanfix/output/Epub.java
src/be/nikiroo/fanfix/supported/Cbz.java
src/be/nikiroo/fanfix/supported/Epub.java
src/be/nikiroo/fanfix/test/ConversionTest.java
src/be/nikiroo/fanfix/test/Test.java
test/expected/cbz.cbz
test/expected/epub.epub

index ed22f6217b13fd57bb28953f188c59f98f2321a4..ece87186b7021a11118d8c42ed324c51a8374333 100644 (file)
@@ -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.
+        * <p>
+        * <b>MUST</b> 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
index e1b9c650c328b91a47c57d4b2e4f97c38262c265..42f3924daa811bdb1bb6e121d38c01b651bf828b 100644 (file)
@@ -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);
                }
index 4a89d9b92a54551845ab57dc653f79dd17229d01..490ba8f04fe936e8f25417f7e829a5ed075045c9 100644 (file)
@@ -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
index 64797d395f1442d0d49220f4072bb7df832e9634..b56cc339285e4f64c83401b5ba79cfe50bb068fc 100644 (file)
@@ -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;
        }
 
index 8ab2a5202044ec850bcb29b979bdec0f0483fd17..f635a17867a57695f4db038cfb894cdd0ec45e49 100644 (file)
@@ -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>());
-               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<String, Image> images = new HashMap<String, Image>();
-               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<String> imagesList = new ArrayList<String>(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<Chapter>());
+                       }
 
-               // ZIP order is not correct for us
-               List<String> imagesList = new ArrayList<String>(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);
index ce9bd06f7a7b15efc358fac017b1313cc03763ed..794998e0d75c24ed36288aec9dcb7c846ecd93fd 100644 (file)
@@ -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) {
index 4fe1653d4a81f08965dc8e5f4b54b5b0fb7c4e83..51b6deb4f817e6a2f451ead2d7b73f554a7795af 100644 (file)
@@ -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<String, List<String>>();
                skipCompare.put("epb.ncx",
                                Arrays.asList("         <meta name=\"dtb:uid\" content="));
@@ -86,7 +78,6 @@ class ConversionTest extends TestLauncher {
 
        @Override
        protected void stop() throws Exception {
-               tempFiles.close();
        }
 
        private TestCase getTestFor(final BasicOutput.OutputType type) {
@@ -108,7 +99,8 @@ class ConversionTest extends TestLauncher {
 
                                // Cross-checks:
                                for (BasicOutput.OutputType crossType : realTypes) {
-                                       File crossDir = tempFiles.createTempDir("cross-result");
+                                       File crossDir = Test.tempFiles
+                                                       .createTempDir("cross-result");
                                        generate(this, target, crossDir, crossType);
                                        compareFiles(this, resultDir, crossDir, crossType,
                                                        "Cross compare " + crossType + " generated from "
@@ -204,12 +196,12 @@ class ConversionTest extends TestLauncher {
 
                        if (expected.getName().endsWith(".cbz")
                                        || expected.getName().endsWith(".epub")) {
-                               File tmpExpected = tempFiles.createTempDir(expected.getName()
-                                               + "[zip-content]");
-                               File tmpResult = tempFiles.createTempDir(result.getName()
+                               File tmpExpected = Test.tempFiles.createTempDir(expected
+                                               .getName() + "[zip-content]");
+                               File tmpResult = Test.tempFiles.createTempDir(result.getName()
                                                + "[zip-content]");
-                               unzip(expected, tmpExpected);
-                               unzip(result, tmpResult);
+                               IOUtils.unzip(expected, tmpExpected);
+                               IOUtils.unzip(result, tmpResult);
                                compareFiles(testCase, tmpExpected, tmpResult, null, errMess);
                        } else {
                                List<String> 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();
-               }
-       }
 }
index dd412ec4ba6ae75cdd0bae436e166bcde3100b4a..614cec14ea2609f45bc9af7c2b09e6a48f7445a3 100644 (file)
@@ -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);
        }
index 472c7d39698210086394a96a9c00014319645580..569c3950e185a009d712f6aadfa46d9948e1aacf 100644 (file)
Binary files a/test/expected/cbz.cbz and b/test/expected/cbz.cbz differ
index f12424ee68e89051eb19b34e7b2ff3a2f6e3ace6..83d52361a278f83c4a2584966a0275c74528f095 100644 (file)
Binary files a/test/expected/epub.epub and b/test/expected/epub.epub differ