add get custom covers
authorNiki Roo <niki@nikiroo.be>
Thu, 14 May 2020 08:44:19 +0000 (10:44 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 14 May 2020 08:44:19 +0000 (10:44 +0200)
src/be/nikiroo/fanfix/library/WebLibrary.java
src/be/nikiroo/fanfix/library/WebLibraryServer.java
src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java
src/be/nikiroo/fanfix/library/WebLibraryUrls.java

index 9769fea4420f14719b7053cbf9baeb04ce7a5119..6f2bfda3a9c111e7e70ffa1c4598ed57c716024a 100644 (file)
@@ -155,15 +155,23 @@ public class WebLibrary extends BasicLibrary {
        }
 
        @Override
-       public Image getCustomSourceCover(final String source) throws IOException {
-               // TODO maybe global system in BasicLib ?
-               return null;
+       public Image getCustomSourceCover(String source) throws IOException {
+               InputStream in = download(WebLibraryUrls.getCoverUrlSource(source));
+               try {
+                       return new Image(in);
+               } finally {
+                       in.close();
+               }
        }
 
        @Override
-       public Image getCustomAuthorCover(final String author) throws IOException {
-               // TODO maybe global system in BasicLib ?
-               return null;
+       public Image getCustomAuthorCover(String author) throws IOException {
+               InputStream in = download(WebLibraryUrls.getCoverUrlAuthor(author));
+               try {
+                       return new Image(in);
+               } finally {
+                       in.close();
+               }
        }
 
        @Override
index 3ccdc3d00ec9a4f6e0feeda95682b18363a55c11..a45fdec5b793036067534a1d63b5713934b6cb4e 100644 (file)
@@ -183,17 +183,17 @@ public class WebLibraryServer extends WebLibraryServerHtml {
        // /story/luid/json <-- json, whole chapter (no images)
        @Override
        protected Response getStoryPart(String uri, WLoginResult login) {
-               String[] cover = uri.split("/");
+               String[] uriParts = uri.split("/");
                int off = 2;
 
-               if (cover.length < off + 2) {
+               if (uriParts.length < off + 2) {
                        return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
                                        NanoHTTPD.MIME_PLAINTEXT, null);
                }
 
-               String luid = cover[off + 0];
-               String chapterStr = cover[off + 1];
-               String imageStr = cover.length < off + 3 ? null : cover[off + 2];
+               String luid = uriParts[off + 0];
+               String chapterStr = uriParts[off + 1];
+               String imageStr = uriParts.length < off + 3 ? null : uriParts[off + 2];
 
                // 1-based (0 = desc)
                int chapter = 0;
@@ -229,7 +229,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                InputStream in = null;
                try {
                        if ("cover".equals(chapterStr)) {
-                               Image img = cover(luid, login);
+                               Image img = storyCover(luid, login);
                                if (img != null) {
                                        in = img.newInputStream();
                                }
@@ -288,6 +288,46 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                return newInputStreamResponse(mimeType, in);
        }
 
+       @Override
+       protected Response getCover(String uri, WLoginResult login)
+                       throws IOException {
+               String[] uriParts = uri.split("/");
+               int off = 2; // "" and "cover"
+
+               if (uriParts.length < off + 2) {
+                       return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
+                                       NanoHTTPD.MIME_PLAINTEXT, "Invalid cover request");
+               }
+
+               String type = uriParts[off + 0];
+               String id = uriParts[off + 1];
+
+               InputStream in = null;
+
+               if ("cover".equals(type)) {
+                       Image img = storyCover(id, login);
+                       if (img != null) {
+                               in = img.newInputStream();
+                       }
+               } else if ("author".equals(type)) {
+                       Image img = authorCover(id, login);
+                       if (img != null) {
+                               in = img.newInputStream();
+                       }
+               } else if ("source".equals(type)) {
+                       Image img = sourceCover(id, login);
+                       if (img != null) {
+                               in = img.newInputStream();
+                       }
+               } else {
+                       return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
+                                       NanoHTTPD.MIME_PLAINTEXT, "Invalid cover type: " + type);
+               }
+
+               // TODO: get correct image type
+               return newInputStreamResponse("image/png", in);
+       }
+
        @Override
        protected List<MetaData> metas(WLoginResult login) throws IOException {
                BasicLibrary lib = Instance.getInstance().getLibrary();
@@ -349,7 +389,8 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                return meta;
        }
 
-       private Image cover(String luid, WLoginResult login) throws IOException {
+       private Image storyCover(String luid, WLoginResult login)
+                       throws IOException {
                MetaData meta = meta(luid, login);
                if (meta != null) {
                        BasicLibrary lib = Instance.getInstance().getLibrary();
@@ -359,6 +400,39 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                return null;
        }
 
+       private Image authorCover(String author, WLoginResult login)
+                       throws IOException {
+               Image img = null;
+
+               List<MetaData> metas = new MetaResultList(metas(login)).filter(null,
+                               author, null);
+               if (metas.size() > 0) {
+                       BasicLibrary lib = Instance.getInstance().getLibrary();
+                       img = lib.getCustomAuthorCover(author);
+                       if (img == null)
+                               img = lib.getCover(metas.get(0).getLuid());
+               }
+
+               return img;
+
+       }
+
+       private Image sourceCover(String source, WLoginResult login)
+                       throws IOException {
+               Image img = null;
+
+               List<MetaData> metas = new MetaResultList(metas(login)).filter(source,
+                               null, null);
+               if (metas.size() > 0) {
+                       BasicLibrary lib = Instance.getInstance().getLibrary();
+                       img = lib.getCustomSourceCover(source);
+                       if (img == null)
+                               img = lib.getCover(metas.get(0).getLuid());
+               }
+
+               return img;
+       }
+
        private boolean isAllowed(MetaData meta, WLoginResult login) {
                MetaResultList one = new MetaResultList(Arrays.asList(meta));
                if (login.isWl() && !whitelist.isEmpty()) {
index c28188cde5467746b21e5f5f9905b9aeede978db..6489fdf75880e6d3488803d88fa4722091811ecc 100644 (file)
@@ -47,6 +47,9 @@ abstract class WebLibraryServerHtml implements Runnable {
 
        abstract protected Response getStoryPart(String uri, WLoginResult login);
 
+       abstract protected Response getCover(String uri, WLoginResult login)
+                       throws IOException;
+
        abstract protected List<MetaData> metas(WLoginResult login)
                        throws IOException;
 
@@ -162,6 +165,8 @@ abstract class WebLibraryServerHtml implements Runnable {
                                                                rep = newFixedLengthResponse(Status.OK,
                                                                                MIME_PLAINTEXT,
                                                                                Version.getCurrentVersion().toString());
+                                                       } else if (WebLibraryUrls.isCoverUrl(uri)) {
+                                                               rep = getCover(uri, login);
                                                        } else if (WebLibraryUrls.isListUrl(uri)) {
                                                                rep = getList(uri, login);
                                                        } else if (WebLibraryUrls.isStoryUrl(uri)) {
@@ -218,7 +223,9 @@ abstract class WebLibraryServerHtml implements Runnable {
                        }
                };
 
-               if (ssf != null) {
+               if (ssf != null)
+
+               {
                        getTraceHandler().trace("Install SSL on the web server...");
                        server.makeSecure(ssf, null);
                        getTraceHandler().trace("Done.");
index be4cf256bd154297e51a65ce3bdb78a52b96ef55..0dd426004d38def06e52854bede41a5d00ca1371 100644 (file)
@@ -22,6 +22,15 @@ class WebLibraryUrls {
 
        static public final String LIST_URL_METADATA = LIST_URL_BASE + "metadata";
 
+       // GET/POST
+       static private final String COVER_URL_BASE = "/cover/";
+       static private final String COVER_URL_STORY = COVER_URL_BASE
+                       + "story/{luid}";
+       static private final String COVER_URL_AUTHOR = COVER_URL_BASE
+                       + "author/{author}";
+       static private final String COVER_URL_SOURCE = COVER_URL_BASE
+                       + "source/{source}";
+
        static public String getViewUrl(String luid, Integer chap, Integer para) {
                return VIEWER_URL //
                                .replace("{luid}", luid) //
@@ -50,7 +59,22 @@ class WebLibraryUrls {
        static public boolean isSupportedUrl(String url) {
                return INDEX_URL.equals(url) || VERSION_URL.equals(url)
                                || LOGOUT_URL.equals(url) || isViewUrl(url) || isStoryUrl(url)
-                               || isListUrl(url);
+                               || isListUrl(url) || isCoverUrl(url);
+       }
+
+       static public String getCoverUrlStory(String luid) {
+               return COVER_URL_STORY //
+                               .replace("{luid}", luid);
+       }
+
+       static public String getCoverUrlSource(String source) {
+               return COVER_URL_SOURCE //
+                               .replace("{source}", source);
+       }
+
+       static public String getCoverUrlAuthor(String author) {
+               return COVER_URL_AUTHOR //
+                               .replace("{author}", author);
        }
 
        static public boolean isViewUrl(String url) {
@@ -64,4 +88,8 @@ class WebLibraryUrls {
        static public boolean isListUrl(String url) {
                return url != null && url.startsWith(LIST_URL_BASE);
        }
+
+       static public boolean isCoverUrl(String url) {
+               return url != null && url.startsWith(COVER_URL_BASE);
+       }
 }