weblib: set custom covers
authorNiki Roo <niki@nikiroo.be>
Thu, 14 May 2020 09:03:55 +0000 (11:03 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 14 May 2020 09:03:55 +0000 (11:03 +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 6f2bfda3a9c111e7e70ffa1c4598ed57c716024a..796533080fba899b60a553f5ac90b80d71d56f58 100644 (file)
@@ -110,7 +110,7 @@ public class WebLibrary extends BasicLibrary {
 
        public Version getVersion() {
                try {
-                       InputStream in = download(WebLibraryUrls.VERSION_URL);
+                       InputStream in = post(WebLibraryUrls.VERSION_URL);
                        try {
                                return new Version(IOUtils.readSmallStream(in));
                        } finally {
@@ -125,10 +125,10 @@ public class WebLibrary extends BasicLibrary {
        @Override
        public Status getStatus() {
                try {
-                       download(WebLibraryUrls.INDEX_URL).close();
+                       post(WebLibraryUrls.INDEX_URL).close();
                } catch (IOException e) {
                        try {
-                               download("/style.css").close();
+                               post("/style.css").close();
                                return Status.UNAUTHORIZED;
                        } catch (IOException ioe) {
                                return Status.UNAVAILABLE;
@@ -146,7 +146,7 @@ public class WebLibrary extends BasicLibrary {
 
        @Override
        public Image getCover(String luid) throws IOException {
-               InputStream in = download(WebLibraryUrls.getStoryUrlCover(luid));
+               InputStream in = post(WebLibraryUrls.getStoryUrlCover(luid));
                try {
                        return new Image(in);
                } finally {
@@ -156,7 +156,7 @@ public class WebLibrary extends BasicLibrary {
 
        @Override
        public Image getCustomSourceCover(String source) throws IOException {
-               InputStream in = download(WebLibraryUrls.getCoverUrlSource(source));
+               InputStream in = post(WebLibraryUrls.getCoverUrlSource(source));
                try {
                        return new Image(in);
                } finally {
@@ -166,7 +166,7 @@ public class WebLibrary extends BasicLibrary {
 
        @Override
        public Image getCustomAuthorCover(String author) throws IOException {
-               InputStream in = download(WebLibraryUrls.getCoverUrlAuthor(author));
+               InputStream in = post(WebLibraryUrls.getCoverUrlAuthor(author));
                try {
                        return new Image(in);
                } finally {
@@ -176,14 +176,16 @@ public class WebLibrary extends BasicLibrary {
 
        @Override
        public void setSourceCover(String source, String luid) throws IOException {
-               // TODO Auto-generated method stub
-               throw new IOException("Not implemented yet");
+               Map<String, String> post = new HashMap<String, String>();
+               post.put("luid", luid);
+               post(WebLibraryUrls.getCoverUrlSource(source), post).close();
        }
 
        @Override
        public void setAuthorCover(String author, String luid) throws IOException {
-               // TODO Auto-generated method stub
-               throw new IOException("Not implemented yet");
+               Map<String, String> post = new HashMap<String, String>();
+               post.put("luid", luid);
+               post(WebLibraryUrls.getCoverUrlAuthor(author), post).close();
        }
 
        @Override
@@ -193,7 +195,7 @@ public class WebLibrary extends BasicLibrary {
                // TODO: pg
 
                Story story;
-               InputStream in = download(WebLibraryUrls.getStoryUrlJson(luid));
+               InputStream in = post(WebLibraryUrls.getStoryUrlJson(luid));
                try {
                        JSONObject json = new JSONObject(IOUtils.readSmallStream(in));
                        story = JsonIO.toStory(json);
@@ -207,7 +209,7 @@ public class WebLibrary extends BasicLibrary {
                        int number = 1;
                        for (Paragraph para : chap) {
                                if (para.getType() == ParagraphType.IMAGE) {
-                                       InputStream subin = download(
+                                       InputStream subin = post(
                                                        WebLibraryUrls.getStoryUrl(luid, chapNum, number));
                                        try {
                                                para.setContentImage(new Image(subin));
@@ -228,7 +230,7 @@ public class WebLibrary extends BasicLibrary {
        @Override
        protected List<MetaData> getMetas(Progress pg) throws IOException {
                List<MetaData> metas = new ArrayList<MetaData>();
-               InputStream in = download(WebLibraryUrls.LIST_URL_METADATA);
+               InputStream in = post(WebLibraryUrls.LIST_URL_METADATA);
                JSONArray jsonArr = new JSONArray(IOUtils.readSmallStream(in));
                for (int i = 0; i < jsonArr.length(); i++) {
                        JSONObject json = jsonArr.getJSONObject(i);
@@ -301,10 +303,18 @@ public class WebLibrary extends BasicLibrary {
        }
 
        // starts with "/", never NULL
-       private InputStream download(String path) throws IOException {
+       private InputStream post(String path) throws IOException {
+               return post(path, null);
+       }
+
+       // starts with "/", never NULL
+       private InputStream post(String path, Map<String, String> post)
+                       throws IOException {
                URL url = new URL(host + ":" + port + path);
 
-               Map<String, String> post = new HashMap<String, String>();
+               if (post == null) {
+                       post = new HashMap<String, String>();
+               }
                post.put("login", subkey);
                post.put("password", key);
 
index a45fdec5b793036067534a1d63b5713934b6cb4e..dfbceeeb7a19ce09cdbd398fa45a36789a758811 100644 (file)
@@ -304,30 +304,58 @@ public class WebLibraryServer extends WebLibraryServerHtml {
 
                InputStream in = null;
 
-               if ("cover".equals(type)) {
+               if ("story".equals(type)) {
                        Image img = storyCover(id, login);
                        if (img != null) {
                                in = img.newInputStream();
                        }
-               } else if ("author".equals(type)) {
-                       Image img = authorCover(id, login);
+               } else if ("source".equals(type)) {
+                       Image img = sourceCover(id, login);
                        if (img != null) {
                                in = img.newInputStream();
                        }
-               } else if ("source".equals(type)) {
-                       Image img = sourceCover(id, login);
+               } else if ("author".equals(type)) {
+                       Image img = authorCover(id, login);
                        if (img != null) {
                                in = img.newInputStream();
                        }
                } else {
                        return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
-                                       NanoHTTPD.MIME_PLAINTEXT, "Invalid cover type: " + type);
+                                       NanoHTTPD.MIME_PLAINTEXT,
+                                       "Invalid GET cover type: " + type);
                }
 
                // TODO: get correct image type
                return newInputStreamResponse("image/png", in);
        }
 
+       @Override
+       protected Response setCover(String uri, String luid, 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];
+
+               if ("source".equals(type)) {
+                       sourceCover(id, login, luid);
+               } else if ("author".equals(type)) {
+                       authorCover(id, login, luid);
+               } else {
+                       return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
+                                       NanoHTTPD.MIME_PLAINTEXT,
+                                       "Invalid SET cover type: " + type);
+               }
+
+               return newInputStreamResponse(NanoHTTPD.MIME_PLAINTEXT, null);
+       }
+
        @Override
        protected List<MetaData> metas(WLoginResult login) throws IOException {
                BasicLibrary lib = Instance.getInstance().getLibrary();
@@ -417,6 +445,18 @@ public class WebLibraryServer extends WebLibraryServerHtml {
 
        }
 
+       private void authorCover(String author, WLoginResult login, String luid)
+                       throws IOException {
+               if (meta(luid, login) != null) {
+                       List<MetaData> metas = new MetaResultList(metas(login)).filter(null,
+                                       author, null);
+                       if (metas.size() > 0) {
+                               BasicLibrary lib = Instance.getInstance().getLibrary();
+                               lib.setAuthorCover(author, luid);
+                       }
+               }
+       }
+
        private Image sourceCover(String source, WLoginResult login)
                        throws IOException {
                Image img = null;
@@ -433,6 +473,18 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                return img;
        }
 
+       private void sourceCover(String source, WLoginResult login, String luid)
+                       throws IOException {
+               if (meta(luid, login) != null) {
+                       List<MetaData> metas = new MetaResultList(metas(login))
+                                       .filter(source, null, null);
+                       if (metas.size() > 0) {
+                               BasicLibrary lib = Instance.getInstance().getLibrary();
+                               lib.setSourceCover(source, luid);
+                       }
+               }
+       }
+
        private boolean isAllowed(MetaData meta, WLoginResult login) {
                MetaResultList one = new MetaResultList(Arrays.asList(meta));
                if (login.isWl() && !whitelist.isEmpty()) {
index 6489fdf75880e6d3488803d88fa4722091811ecc..e79d6f5ea4ad0a736dbaea826447d61682b50ad6 100644 (file)
@@ -50,6 +50,9 @@ abstract class WebLibraryServerHtml implements Runnable {
        abstract protected Response getCover(String uri, WLoginResult login)
                        throws IOException;
 
+       abstract protected Response setCover(String uri, String luid,
+                       WLoginResult login) throws IOException;
+
        abstract protected List<MetaData> metas(WLoginResult login)
                        throws IOException;
 
@@ -166,7 +169,12 @@ abstract class WebLibraryServerHtml implements Runnable {
                                                                                MIME_PLAINTEXT,
                                                                                Version.getCurrentVersion().toString());
                                                        } else if (WebLibraryUrls.isCoverUrl(uri)) {
-                                                               rep = getCover(uri, login);
+                                                               String luid = params.get("luid");
+                                                               if (luid != null) {
+                                                                       rep = setCover(uri, luid, login);
+                                                               } else {
+                                                                       rep = getCover(uri, login);
+                                                               }
                                                        } else if (WebLibraryUrls.isListUrl(uri)) {
                                                                rep = getList(uri, login);
                                                        } else if (WebLibraryUrls.isStoryUrl(uri)) {
index 0dd426004d38def06e52854bede41a5d00ca1371..2f36731871991449f63fead0265d8c5f79a9e499 100644 (file)
@@ -22,7 +22,8 @@ class WebLibraryUrls {
 
        static public final String LIST_URL_METADATA = LIST_URL_BASE + "metadata";
 
-       // GET/POST
+       // GET/SET ("luid" param -> set cover to the cover of this story -- not ok
+       // for /cover/story/)
        static private final String COVER_URL_BASE = "/cover/";
        static private final String COVER_URL_STORY = COVER_URL_BASE
                        + "story/{luid}";