X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FLocalLibrary.java;fp=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FLocalLibrary.java;h=e9c67f3866d41bca6487b36b2e5db20be163dc97;hb=3a0605e6a05e6d9dee6b596d5f9b193408470164;hp=9ebff72c2151d7d6f6e7e0f6cf86fafc42911aed;hpb=d31cd8c7320256b97654cc1cdd827843b0407c9e;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/library/LocalLibrary.java b/src/be/nikiroo/fanfix/library/LocalLibrary.java index 9ebff72..e9c67f3 100644 --- a/src/be/nikiroo/fanfix/library/LocalLibrary.java +++ b/src/be/nikiroo/fanfix/library/LocalLibrary.java @@ -187,7 +187,7 @@ public class LocalLibrary extends BasicLibrary { throws IOException { File newDir = getExpectedDir(meta.getSource()); if (!newDir.exists()) { - newDir.mkdir(); + newDir.mkdirs(); } List relatedFiles = getRelatedFiles(meta.getLuid()); @@ -225,7 +225,7 @@ public class LocalLibrary extends BasicLibrary { return img; } - File coverDir = new File(baseDir, source); + File coverDir = getExpectedDir(source); if (coverDir.isDirectory()) { File cover = new File(coverDir, ".cover.png"); if (cover.exists()) { @@ -295,19 +295,21 @@ public class LocalLibrary extends BasicLibrary { if (meta != null && meta.getType().equals(expectedType)) { File from = otherLocalLibrary.getExpectedDir(meta.getSource()); File to = this.getExpectedDir(meta.getSource()); - List sources = otherLocalLibrary.getRelatedFiles(luid); - if (!sources.isEmpty()) { - pg.setMinMax(0, sources.size()); + List relatedFiles = otherLocalLibrary + .getRelatedFiles(luid); + if (!relatedFiles.isEmpty()) { + pg.setMinMax(0, relatedFiles.size()); } - for (File source : sources) { - File target = new File(source.getAbsolutePath().replace( - from.getAbsolutePath(), to.getAbsolutePath())); - if (!source.equals(target)) { + for (File relatedFile : relatedFiles) { + File target = new File(relatedFile.getAbsolutePath() + .replace(from.getAbsolutePath(), + to.getAbsolutePath())); + if (!relatedFile.equals(target)) { target.getParentFile().mkdirs(); InputStream in = null; try { - in = new FileInputStream(source); + in = new FileInputStream(relatedFile); IOUtils.write(in, target); } catch (IOException e) { if (in != null) { @@ -399,7 +401,22 @@ public class LocalLibrary extends BasicLibrary { * @return the target directory */ private File getExpectedDir(String source) { - String sanitizedSource = source.replaceAll("[^a-zA-Z0-9._+-]", "_"); + String sanitizedSource = source.replaceAll("[^a-zA-Z0-9._+/-]", "_"); + + while (sanitizedSource.startsWith("/")) { + if (sanitizedSource.length() > 1) { + sanitizedSource = sanitizedSource.substring(1); + } else { + sanitizedSource = ""; + } + } + + sanitizedSource = sanitizedSource.replace("/", File.separator); + + if (sanitizedSource.isEmpty()) { + sanitizedSource = "EMPTY"; + } + return new File(baseDir, sanitizedSource); } @@ -498,49 +515,11 @@ public class LocalLibrary extends BasicLibrary { pg.addProgress(pgDirs, 100); for (File dir : dirs) { - File[] infoFiles = dir.listFiles(new FileFilter() { - @Override - public boolean accept(File file) { - return file != null - && file.getPath().toLowerCase() - .endsWith(".info"); - } - }); - - Progress pgFiles = new Progress(0, infoFiles.length); + Progress pgFiles = new Progress(); pgDirs.addProgress(pgFiles, 100); pgDirs.setName("Loading from: " + dir.getName()); - for (File infoFile : infoFiles) { - pgFiles.setName(infoFile.getName()); - try { - MetaData meta = InfoReader - .readMeta(infoFile, false); - try { - int id = Integer.parseInt(meta.getLuid()); - if (id > lastId) { - lastId = id; - } - - stories.put(meta, new File[] { infoFile, - getTargetFile(meta, infoFile) }); - } catch (Exception e) { - // not normal!! - throw new IOException( - "Cannot understand the LUID of " - + infoFile + ": " - + meta.getLuid(), e); - } - } catch (IOException e) { - // We should not have not-supported files in the - // library - Instance.getTraceHandler().error( - new IOException( - "Cannot load file from library: " - + infoFile, e)); - } - pgFiles.add(1); - } + addToStories(pgFiles, dir); pgFiles.setName(null); } @@ -552,4 +531,58 @@ public class LocalLibrary extends BasicLibrary { pg.done(); return stories; } + + private void addToStories(Progress pgFiles, File dir) { + File[] infoFilesAndSubdirs = dir.listFiles(new FileFilter() { + @Override + public boolean accept(File file) { + boolean info = file != null && file.isFile() + && file.getPath().toLowerCase().endsWith(".info"); + boolean dir = file != null && file.isDirectory(); + return info || dir; + } + }); + + if (pgFiles != null) { + pgFiles.setMinMax(0, infoFilesAndSubdirs.length); + } + + for (File infoFileOrSubdir : infoFilesAndSubdirs) { + if (pgFiles != null) { + pgFiles.setName(infoFileOrSubdir.getName()); + } + + if (infoFileOrSubdir.isDirectory()) { + addToStories(null, infoFileOrSubdir); + } else { + try { + MetaData meta = InfoReader + .readMeta(infoFileOrSubdir, false); + try { + int id = Integer.parseInt(meta.getLuid()); + if (id > lastId) { + lastId = id; + } + + stories.put(meta, new File[] { infoFileOrSubdir, + getTargetFile(meta, infoFileOrSubdir) }); + } catch (Exception e) { + // not normal!! + throw new IOException("Cannot understand the LUID of " + + infoFileOrSubdir + ": " + meta.getLuid(), e); + } + } catch (IOException e) { + // We should not have not-supported files in the + // library + Instance.getTraceHandler().error( + new IOException("Cannot load file from library: " + + infoFileOrSubdir, e)); + } + } + + if (pgFiles != null) { + pgFiles.add(1); + } + } + } }