From: Niki Roo Date: Mon, 26 Mar 2018 19:10:55 +0000 (+0200) Subject: Small fixes: X-Git-Tag: fanfix-1.7.0~10 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=2aac79c740789071ad9b773d25f20e103f0da86c;p=fanfix.git 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 --- 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 b/test/expected/cbz.cbz index 472c7d3..569c395 100644 Binary files a/test/expected/cbz.cbz and b/test/expected/cbz.cbz differ diff --git a/test/expected/epub.epub b/test/expected/epub.epub index f12424e..83d5236 100644 Binary files a/test/expected/epub.epub and b/test/expected/epub.epub differ