| 1 | package be.nikiroo.fanfix.library; |
| 2 | |
| 3 | import java.awt.image.BufferedImage; |
| 4 | import java.io.File; |
| 5 | import java.io.FileFilter; |
| 6 | import java.io.FileInputStream; |
| 7 | import java.io.IOException; |
| 8 | import java.io.InputStream; |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.HashMap; |
| 11 | import java.util.List; |
| 12 | import java.util.Map; |
| 13 | |
| 14 | import javax.imageio.ImageIO; |
| 15 | |
| 16 | import be.nikiroo.fanfix.Instance; |
| 17 | import be.nikiroo.fanfix.bundles.Config; |
| 18 | import be.nikiroo.fanfix.data.MetaData; |
| 19 | import be.nikiroo.fanfix.data.Story; |
| 20 | import be.nikiroo.fanfix.output.BasicOutput; |
| 21 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
| 22 | import be.nikiroo.fanfix.output.InfoCover; |
| 23 | import be.nikiroo.fanfix.supported.InfoReader; |
| 24 | import be.nikiroo.utils.IOUtils; |
| 25 | import be.nikiroo.utils.ImageUtils; |
| 26 | import be.nikiroo.utils.MarkableFileInputStream; |
| 27 | import be.nikiroo.utils.Progress; |
| 28 | |
| 29 | /** |
| 30 | * This {@link BasicLibrary} will store the stories locally on disk. |
| 31 | * |
| 32 | * @author niki |
| 33 | */ |
| 34 | public class LocalLibrary extends BasicLibrary { |
| 35 | private int lastId; |
| 36 | private Map<MetaData, File[]> stories; // Files: [ infoFile, TargetFile ] |
| 37 | private Map<String, BufferedImage> sourceCovers; |
| 38 | |
| 39 | private File baseDir; |
| 40 | private OutputType text; |
| 41 | private OutputType image; |
| 42 | |
| 43 | /** |
| 44 | * Create a new {@link LocalLibrary} with the given back-end directory. |
| 45 | * |
| 46 | * @param baseDir |
| 47 | * the directory where to find the {@link Story} objects |
| 48 | */ |
| 49 | public LocalLibrary(File baseDir) { |
| 50 | this(baseDir, Instance.getConfig().getString( |
| 51 | Config.NON_IMAGES_DOCUMENT_TYPE), Instance.getConfig() |
| 52 | .getString(Config.IMAGES_DOCUMENT_TYPE), false); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Create a new {@link LocalLibrary} with the given back-end directory. |
| 57 | * |
| 58 | * @param baseDir |
| 59 | * the directory where to find the {@link Story} objects |
| 60 | * @param text |
| 61 | * the {@link OutputType} to use for non-image documents |
| 62 | * @param image |
| 63 | * the {@link OutputType} to use for image documents |
| 64 | * @param defaultIsHtml |
| 65 | * if the given text or image is invalid, use HTML by default (if |
| 66 | * not, it will be INFO_TEXT/CBZ by default) |
| 67 | */ |
| 68 | public LocalLibrary(File baseDir, String text, String image, |
| 69 | boolean defaultIsHtml) { |
| 70 | this(baseDir, OutputType.valueOfAllOkUC(text, |
| 71 | defaultIsHtml ? OutputType.HTML : OutputType.INFO_TEXT), |
| 72 | OutputType.valueOfAllOkUC(image, |
| 73 | defaultIsHtml ? OutputType.HTML : OutputType.CBZ)); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Create a new {@link LocalLibrary} with the given back-end directory. |
| 78 | * |
| 79 | * @param baseDir |
| 80 | * the directory where to find the {@link Story} objects |
| 81 | * @param text |
| 82 | * the {@link OutputType} to use for non-image documents |
| 83 | * @param image |
| 84 | * the {@link OutputType} to use for image documents |
| 85 | */ |
| 86 | public LocalLibrary(File baseDir, OutputType text, OutputType image) { |
| 87 | this.baseDir = baseDir; |
| 88 | this.text = text; |
| 89 | this.image = image; |
| 90 | |
| 91 | this.lastId = 0; |
| 92 | this.stories = null; |
| 93 | this.sourceCovers = new HashMap<String, BufferedImage>(); |
| 94 | |
| 95 | baseDir.mkdirs(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected List<MetaData> getMetas(Progress pg) { |
| 100 | return new ArrayList<MetaData>(getStories(pg).keySet()); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public File getFile(String luid, Progress pg) { |
| 105 | File[] files = getStories(pg).get(getInfo(luid)); |
| 106 | if (files != null) { |
| 107 | return files[1]; |
| 108 | } |
| 109 | |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public BufferedImage getCover(String luid) { |
| 115 | MetaData meta = getInfo(luid); |
| 116 | if (meta != null) { |
| 117 | File[] files = getStories(null).get(meta); |
| 118 | if (files != null) { |
| 119 | File infoFile = files[0]; |
| 120 | |
| 121 | try { |
| 122 | meta = InfoReader.readMeta(infoFile, true); |
| 123 | return meta.getCover(); |
| 124 | } catch (IOException e) { |
| 125 | Instance.syserr(e); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | protected void clearCache() { |
| 135 | stories = null; |
| 136 | sourceCovers = new HashMap<String, BufferedImage>(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | protected synchronized int getNextId() { |
| 141 | getStories(null); // make sure lastId is set |
| 142 | return ++lastId; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected void doDelete(String luid) throws IOException { |
| 147 | for (File file : getRelatedFiles(luid)) { |
| 148 | // TODO: throw an IOException if we cannot delete the files? |
| 149 | IOUtils.deltree(file); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | protected Story doSave(Story story, Progress pg) throws IOException { |
| 155 | MetaData meta = story.getMeta(); |
| 156 | |
| 157 | File expectedTarget = getExpectedFile(meta); |
| 158 | expectedTarget.getParentFile().mkdirs(); |
| 159 | |
| 160 | BasicOutput it = BasicOutput.getOutput(getOutputType(meta), true); |
| 161 | it.process(story, expectedTarget.getPath(), pg); |
| 162 | |
| 163 | return story; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | protected synchronized void saveMeta(MetaData meta, Progress pg) |
| 168 | throws IOException { |
| 169 | File newDir = getExpectedDir(meta.getSource()); |
| 170 | if (!newDir.exists()) { |
| 171 | newDir.mkdir(); |
| 172 | } |
| 173 | |
| 174 | List<File> relatedFiles = getRelatedFiles(meta.getLuid()); |
| 175 | for (File relatedFile : relatedFiles) { |
| 176 | // TODO: this is not safe at all. |
| 177 | // We should copy all the files THEN delete them |
| 178 | // Maybe also adding some rollback cleanup if possible |
| 179 | if (relatedFile.getName().endsWith(".info")) { |
| 180 | try { |
| 181 | String name = relatedFile.getName().replaceFirst( |
| 182 | "\\.info$", ""); |
| 183 | InfoCover.writeInfo(newDir, name, meta); |
| 184 | relatedFile.delete(); |
| 185 | } catch (IOException e) { |
| 186 | Instance.syserr(e); |
| 187 | } |
| 188 | } else { |
| 189 | relatedFile.renameTo(new File(newDir, relatedFile.getName())); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | clearCache(); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public BufferedImage getSourceCover(String source) { |
| 198 | if (!sourceCovers.containsKey(source)) { |
| 199 | sourceCovers.put(source, super.getSourceCover(source)); |
| 200 | } |
| 201 | |
| 202 | return sourceCovers.get(source); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void setSourceCover(String source, String luid) { |
| 207 | sourceCovers.put(source, getCover(luid)); |
| 208 | File cover = new File(getExpectedDir(source), ".cover.png"); |
| 209 | try { |
| 210 | ImageIO.write(sourceCovers.get(source), "png", cover); |
| 211 | } catch (IOException e) { |
| 212 | Instance.syserr(e); |
| 213 | sourceCovers.remove(source); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void imprt(BasicLibrary other, String luid, Progress pg) |
| 219 | throws IOException { |
| 220 | if (pg == null) { |
| 221 | pg = new Progress(); |
| 222 | } |
| 223 | |
| 224 | // Check if we can simply copy the files instead of the whole process |
| 225 | if (other instanceof LocalLibrary) { |
| 226 | LocalLibrary otherLocalLibrary = (LocalLibrary) other; |
| 227 | |
| 228 | MetaData meta = otherLocalLibrary.getInfo(luid); |
| 229 | String expectedType = "" |
| 230 | + (meta != null && meta.isImageDocument() ? image : text); |
| 231 | if (meta != null && meta.getType().equals(expectedType)) { |
| 232 | File from = otherLocalLibrary.getExpectedDir(meta.getSource()); |
| 233 | File to = this.getExpectedDir(meta.getSource()); |
| 234 | List<File> sources = otherLocalLibrary.getRelatedFiles(luid); |
| 235 | if (!sources.isEmpty()) { |
| 236 | pg.setMinMax(0, sources.size()); |
| 237 | } |
| 238 | |
| 239 | for (File source : sources) { |
| 240 | File target = new File(source.getAbsolutePath().replace( |
| 241 | from.getAbsolutePath(), to.getAbsolutePath())); |
| 242 | if (!source.equals(target)) { |
| 243 | target.getParentFile().mkdirs(); |
| 244 | InputStream in = null; |
| 245 | try { |
| 246 | in = new FileInputStream(source); |
| 247 | IOUtils.write(in, target); |
| 248 | } catch (IOException e) { |
| 249 | if (in != null) { |
| 250 | try { |
| 251 | in.close(); |
| 252 | } catch (Exception ee) { |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | pg.done(); |
| 257 | throw e; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | pg.add(1); |
| 262 | } |
| 263 | |
| 264 | clearCache(); |
| 265 | pg.done(); |
| 266 | return; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | super.imprt(other, luid, pg); |
| 271 | |
| 272 | clearCache(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Return the {@link OutputType} for this {@link Story}. |
| 277 | * |
| 278 | * @param meta |
| 279 | * the {@link Story} {@link MetaData} |
| 280 | * |
| 281 | * @return the type |
| 282 | */ |
| 283 | private OutputType getOutputType(MetaData meta) { |
| 284 | if (meta != null && meta.isImageDocument()) { |
| 285 | return image; |
| 286 | } |
| 287 | |
| 288 | return text; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get the target {@link File} related to the given <tt>.info</tt> |
| 293 | * {@link File} and {@link MetaData}. |
| 294 | * |
| 295 | * @param meta |
| 296 | * the meta |
| 297 | * @param infoFile |
| 298 | * the <tt>.info</tt> {@link File} |
| 299 | * |
| 300 | * @return the target {@link File} |
| 301 | */ |
| 302 | private File getTargetFile(MetaData meta, File infoFile) { |
| 303 | // Replace .info with whatever is needed: |
| 304 | String path = infoFile.getPath(); |
| 305 | path = path.substring(0, path.length() - ".info".length()); |
| 306 | String newExt = getOutputType(meta).getDefaultExtension(true); |
| 307 | |
| 308 | return new File(path + newExt); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * The target (full path) where the {@link Story} related to this |
| 313 | * {@link MetaData} should be located on disk for a new {@link Story}. |
| 314 | * |
| 315 | * @param key |
| 316 | * the {@link Story} {@link MetaData} |
| 317 | * |
| 318 | * @return the target |
| 319 | */ |
| 320 | private File getExpectedFile(MetaData key) { |
| 321 | String title = key.getTitle(); |
| 322 | if (title == null) { |
| 323 | title = ""; |
| 324 | } |
| 325 | title = title.replaceAll("[^a-zA-Z0-9._+-]", "_"); |
| 326 | return new File(getExpectedDir(key.getSource()), key.getLuid() + "_" |
| 327 | + title); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * The directory (full path) where the new {@link Story} related to this |
| 332 | * {@link MetaData} should be located on disk. |
| 333 | * |
| 334 | * @param type |
| 335 | * the type (source) |
| 336 | * |
| 337 | * @return the target directory |
| 338 | */ |
| 339 | private File getExpectedDir(String type) { |
| 340 | String source = type.replaceAll("[^a-zA-Z0-9._+-]", "_"); |
| 341 | return new File(baseDir, source); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Return the list of files/directories on disk for this {@link Story}. |
| 346 | * <p> |
| 347 | * If the {@link Story} is not found, and empty list is returned. |
| 348 | * |
| 349 | * @param luid |
| 350 | * the {@link Story} LUID |
| 351 | * |
| 352 | * @return the list of {@link File}s |
| 353 | * |
| 354 | * @throws IOException |
| 355 | * if the {@link Story} was not found |
| 356 | */ |
| 357 | private List<File> getRelatedFiles(String luid) throws IOException { |
| 358 | List<File> files = new ArrayList<File>(); |
| 359 | |
| 360 | MetaData meta = getInfo(luid); |
| 361 | if (meta == null) { |
| 362 | throw new IOException("Story not found: " + luid); |
| 363 | } |
| 364 | |
| 365 | File infoFile = getStories(null).get(meta)[0]; |
| 366 | File targetFile = getStories(null).get(meta)[1]; |
| 367 | |
| 368 | files.add(infoFile); |
| 369 | files.add(targetFile); |
| 370 | |
| 371 | String readerExt = getOutputType(meta).getDefaultExtension(true); |
| 372 | String fileExt = getOutputType(meta).getDefaultExtension(false); |
| 373 | |
| 374 | String path = targetFile.getAbsolutePath(); |
| 375 | if (readerExt != null && !readerExt.equals(fileExt)) { |
| 376 | path = path.substring(0, path.length() - readerExt.length()) |
| 377 | + fileExt; |
| 378 | File relatedFile = new File(path); |
| 379 | |
| 380 | if (relatedFile.exists()) { |
| 381 | files.add(relatedFile); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | String coverExt = "." |
| 386 | + Instance.getConfig().getString(Config.IMAGE_FORMAT_COVER); |
| 387 | File coverFile = new File(path + coverExt); |
| 388 | if (!coverFile.exists()) { |
| 389 | coverFile = new File(path.substring(0, |
| 390 | path.length() - fileExt.length()) |
| 391 | + coverExt); |
| 392 | } |
| 393 | |
| 394 | if (coverFile.exists()) { |
| 395 | files.add(coverFile); |
| 396 | } |
| 397 | |
| 398 | return files; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Fill the list of stories by reading the content of the local directory |
| 403 | * {@link LocalLibrary#baseDir}. |
| 404 | * <p> |
| 405 | * Will use a cached list when possible (see |
| 406 | * {@link BasicLibrary#clearCache()}). |
| 407 | * |
| 408 | * @param pg |
| 409 | * the optional {@link Progress} |
| 410 | * |
| 411 | * @return the list of stories |
| 412 | */ |
| 413 | private synchronized Map<MetaData, File[]> getStories(Progress pg) { |
| 414 | if (pg == null) { |
| 415 | pg = new Progress(); |
| 416 | } else { |
| 417 | pg.setMinMax(0, 100); |
| 418 | } |
| 419 | |
| 420 | if (stories == null) { |
| 421 | stories = new HashMap<MetaData, File[]>(); |
| 422 | |
| 423 | lastId = 0; |
| 424 | |
| 425 | File[] dirs = baseDir.listFiles(new FileFilter() { |
| 426 | @Override |
| 427 | public boolean accept(File file) { |
| 428 | return file != null && file.isDirectory(); |
| 429 | } |
| 430 | }); |
| 431 | |
| 432 | Progress pgDirs = new Progress(0, 100 * dirs.length); |
| 433 | pg.addProgress(pgDirs, 100); |
| 434 | |
| 435 | for (File dir : dirs) { |
| 436 | File[] infoFiles = dir.listFiles(new FileFilter() { |
| 437 | @Override |
| 438 | public boolean accept(File file) { |
| 439 | return file != null |
| 440 | && file.getPath().toLowerCase() |
| 441 | .endsWith(".info"); |
| 442 | } |
| 443 | }); |
| 444 | |
| 445 | Progress pgFiles = new Progress(0, infoFiles.length); |
| 446 | pgDirs.addProgress(pgFiles, 100); |
| 447 | pgDirs.setName("Loading from: " + dir.getName()); |
| 448 | |
| 449 | String source = null; |
| 450 | for (File infoFile : infoFiles) { |
| 451 | pgFiles.setName(infoFile.getName()); |
| 452 | try { |
| 453 | MetaData meta = InfoReader.readMeta(infoFile, false); |
| 454 | source = meta.getSource(); |
| 455 | try { |
| 456 | int id = Integer.parseInt(meta.getLuid()); |
| 457 | if (id > lastId) { |
| 458 | lastId = id; |
| 459 | } |
| 460 | |
| 461 | stories.put(meta, new File[] { infoFile, |
| 462 | getTargetFile(meta, infoFile) }); |
| 463 | } catch (Exception e) { |
| 464 | // not normal!! |
| 465 | throw new IOException( |
| 466 | "Cannot understand the LUID of " |
| 467 | + infoFile |
| 468 | + ": " |
| 469 | + (meta == null ? "[meta is NULL]" |
| 470 | : meta.getLuid()), e); |
| 471 | } |
| 472 | } catch (IOException e) { |
| 473 | // We should not have not-supported files in the |
| 474 | // library |
| 475 | Instance.syserr(new IOException( |
| 476 | "Cannot load file from library: " + infoFile, e)); |
| 477 | } |
| 478 | pgFiles.add(1); |
| 479 | } |
| 480 | |
| 481 | File cover = new File(dir, ".cover.png"); |
| 482 | if (cover.exists()) { |
| 483 | try { |
| 484 | InputStream in = new MarkableFileInputStream( |
| 485 | new FileInputStream(cover)); |
| 486 | try { |
| 487 | sourceCovers.put(source, ImageUtils.fromStream(in)); |
| 488 | } finally { |
| 489 | in.close(); |
| 490 | } |
| 491 | } catch (IOException e) { |
| 492 | Instance.syserr(e); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | pgFiles.setName(null); |
| 497 | } |
| 498 | |
| 499 | pgDirs.setName("Loading directories"); |
| 500 | } |
| 501 | |
| 502 | return stories; |
| 503 | } |
| 504 | } |