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;
private static File remoteDir;
private static String configDir;
private static TraceHandler tracer;
+ private static TempFiles tempFiles;
static {
// Before we can configure it:
+ coverDir));
coverDir = null;
}
+
+ try {
+ tempFiles = new TempFiles("fanfix");
+ } catch (IOException e) {
+ tracer.error(new IOException("Cannot create temporary directory", e));
+ }
}
/**
}
}
+ /**
+ * 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
}
}
+ try {
+ Instance.getTempFiles().close();
+ } catch (IOException e) {
+ Instance.getTraceHandler()
+ .error(new IOException(
+ "Cannot dispose of the temporary files", e));
+ }
+
if (exitCode == 255) {
syntax(false);
}
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;
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
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()) {
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;
}
package be.nikiroo.fanfix.supported;
+import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
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;
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);
*/
class Epub extends InfoText {
protected MetaData meta;
+ private File tmpDir;
private File tmp;
private String desc;
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;
@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) {
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;
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="));
@Override
protected void stop() throws Exception {
- tempFiles.close();
}
private TestCase getTestFor(final BasicOutput.OutputType type) {
// 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 "
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
+ 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);
}
}
}
-
- // 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();
- }
- }
}
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;
* @author niki
*/
public class Test extends TestLauncher {
+ /**
+ * The temporary files handler.
+ */
+ static TempFiles tempFiles;
+
/**
* Create the Fanfix {@link 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);
}