| 1 | package be.nikiroo.fanfix.supported; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.net.URL; |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Collections; |
| 7 | import java.util.HashMap; |
| 8 | import java.util.List; |
| 9 | import java.util.Map; |
| 10 | import java.util.zip.ZipEntry; |
| 11 | import java.util.zip.ZipInputStream; |
| 12 | |
| 13 | import be.nikiroo.fanfix.Instance; |
| 14 | import be.nikiroo.fanfix.data.Chapter; |
| 15 | import be.nikiroo.fanfix.data.Paragraph; |
| 16 | import be.nikiroo.fanfix.data.Story; |
| 17 | import be.nikiroo.utils.Image; |
| 18 | import be.nikiroo.utils.Progress; |
| 19 | |
| 20 | /** |
| 21 | * Support class for CBZ files (works better with CBZ created with this program, |
| 22 | * as they have some metadata available). |
| 23 | * |
| 24 | * @author niki |
| 25 | */ |
| 26 | class Cbz extends Epub { |
| 27 | @Override |
| 28 | protected boolean supports(URL url) { |
| 29 | return url.toString().toLowerCase().endsWith(".cbz"); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | public String getSourceName() { |
| 34 | return "cbz"; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | protected String getDataPrefix() { |
| 39 | return ""; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | protected boolean requireInfo() { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected boolean isImagesDocumentByDefault() { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected boolean getCover() { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public Story process(URL url, Progress pg) throws IOException { |
| 59 | if (pg == null) { |
| 60 | pg = new Progress(); |
| 61 | } else { |
| 62 | pg.setMinMax(0, 100); |
| 63 | } |
| 64 | |
| 65 | Progress pgMeta = new Progress(); |
| 66 | pg.addProgress(pgMeta, 10); |
| 67 | Story story = processMeta(url, false, true, pgMeta); |
| 68 | pgMeta.done(); // 10% |
| 69 | |
| 70 | story.setChapters(new ArrayList<Chapter>()); |
| 71 | Chapter chap = new Chapter(1, null); |
| 72 | story.getChapters().add(chap); |
| 73 | |
| 74 | ZipInputStream zipIn = new ZipInputStream(getInput()); |
| 75 | |
| 76 | Map<String, Image> images = new HashMap<String, Image>(); |
| 77 | for (ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn |
| 78 | .getNextEntry()) { |
| 79 | if (!entry.isDirectory() |
| 80 | && entry.getName().startsWith(getDataPrefix())) { |
| 81 | String entryLName = entry.getName().toLowerCase(); |
| 82 | boolean imageEntry = false; |
| 83 | for (String ext : getImageExt(false)) { |
| 84 | if (entryLName.endsWith(ext)) { |
| 85 | imageEntry = true; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (imageEntry) { |
| 90 | String uuid = meta.getUuid() + "_" + entry.getName(); |
| 91 | try { |
| 92 | images.put(uuid, new Image(zipIn)); |
| 93 | } catch (Exception e) { |
| 94 | Instance.getTraceHandler().error(e); |
| 95 | } |
| 96 | |
| 97 | if (pg.getProgress() < 85) { |
| 98 | pg.add(1); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | pg.setProgress(85); |
| 105 | |
| 106 | // ZIP order is not correct for us |
| 107 | List<String> imagesList = new ArrayList<String>(images.keySet()); |
| 108 | Collections.sort(imagesList); |
| 109 | |
| 110 | pg.setProgress(90); |
| 111 | |
| 112 | for (String uuid : imagesList) { |
| 113 | try { |
| 114 | chap.getParagraphs().add(new Paragraph(images.get(uuid))); |
| 115 | } catch (Exception e) { |
| 116 | Instance.getTraceHandler().error(e); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (meta.getCover() == null && !images.isEmpty()) { |
| 121 | meta.setCover(images.get(imagesList.get(0))); |
| 122 | meta.setFakeCover(true); |
| 123 | } |
| 124 | |
| 125 | pg.setProgress(100); |
| 126 | return story; |
| 127 | } |
| 128 | } |