Added Android code
[wl-app.git] / Android / r2-streamer / r2-parser / src / main / java / org / readium / r2_streamer / parser / CBZParser.java
1 package org.readium.r2_streamer.parser;
2
3 import org.readium.r2_streamer.model.container.Container;
4 import org.readium.r2_streamer.model.publication.EpubPublication;
5 import org.readium.r2_streamer.model.publication.link.Link;
6
7 /**
8  * Class to handle parsing of the .cbz (Comic book archive)
9  * ref => https://en.wikipedia.org/wiki/Comic_book_archive
10  *
11  * @author gautam chibde on 5/6/17.
12  */
13
14 public class CBZParser {
15     private static final String TAG = CBZParser.class.getSimpleName();
16
17     /**
18      * function converts all the images inside the .cbz file into
19      * link and addes them to spine and linkMap
20      *
21      * @param container   contains implementation for getting raw data from file.
22      * @param publication The `Publication` object resulting from the parsing.
23      */
24     public static void parseCBZ(Container container, EpubPublication publication) {
25
26         publication.internalData.put("type", "cbz");
27         // since all the image files are inside zip rootpath is kept empty
28         publication.internalData.put("rootfile", "");
29
30         for (String name : container.listFiles()) {
31             Link link = new Link();
32             link.typeLink = getMediaType(name);
33             link.href = name;
34             // Add the book images to the spine element
35             publication.spines.add(link);
36             // Add to the resource linkMap for ResourceHandler to publish on the server
37             publication.linkMap.put(name, link);
38         }
39     }
40
41     /**
42      * Returns the mimetype depending on the file format
43      *
44      * @param name file name
45      * @return mimetype of the input file
46      */
47     private static String getMediaType(String name) {
48         if (name.contains(".jpg") || name.contains("jpeg")) {
49             return "image/jpeg";
50         } else if (name.contains("png")) {
51             return "image/png";
52         } else {
53             return "";
54         }
55     }
56 }