weblib: fixes
authorNiki Roo <niki@nikiroo.be>
Thu, 14 May 2020 10:52:21 +0000 (12:52 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 14 May 2020 10:52:21 +0000 (12:52 +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 9d1c773aae698457d920b5094668eebb74605202..ac1349e7ac51b3015050a4725a28265ef13a211a 100644 (file)
@@ -152,7 +152,12 @@ public class WebLibrary extends BasicLibrary {
        public Image getCover(String luid) throws IOException {
                InputStream in = post(WebLibraryUrls.getStoryUrlCover(luid));
                try {
-                       return new Image(in);
+                       Image img = new Image(in);
+                       if (img.getSize() > 0) {
+                               return img;
+                       }
+
+                       return null;
                } finally {
                        in.close();
                }
@@ -162,7 +167,12 @@ public class WebLibrary extends BasicLibrary {
        public Image getCustomSourceCover(String source) throws IOException {
                InputStream in = post(WebLibraryUrls.getCoverUrlSource(source));
                try {
-                       return new Image(in);
+                       Image img = new Image(in);
+                       if (img.getSize() > 0) {
+                               return img;
+                       }
+
+                       return null;
                } finally {
                        in.close();
                }
@@ -172,7 +182,12 @@ public class WebLibrary extends BasicLibrary {
        public Image getCustomAuthorCover(String author) throws IOException {
                InputStream in = post(WebLibraryUrls.getCoverUrlAuthor(author));
                try {
-                       return new Image(in);
+                       Image img = new Image(in);
+                       if (img.getSize() > 0) {
+                               return img;
+                       }
+
+                       return null;
                } finally {
                        in.close();
                }
@@ -223,7 +238,10 @@ public class WebLibrary extends BasicLibrary {
                                        InputStream subin = post(
                                                        WebLibraryUrls.getStoryUrl(luid, chapNum, number));
                                        try {
-                                               para.setContentImage(new Image(subin));
+                                               Image img = new Image(subin);
+                                               if (img.getSize() > 0) {
+                                                       para.setContentImage(img);
+                                               }
                                        } finally {
                                                subin.close();
                                        }
@@ -291,6 +309,8 @@ public class WebLibrary extends BasicLibrary {
                                try {
                                        subPg = JsonIO.toProgress(
                                                        new JSONObject(IOUtils.readSmallStream(in)));
+                               } catch (Exception e) {
+                                       subPg = null;
                                } finally {
                                        in.close();
                                }
index 5cda3b5d712e6aa062ca87e9b16b4d157a40752f..810cc4dbfbf6bafe532c9f2ae72fd73889a27e5e 100644 (file)
@@ -31,10 +31,6 @@ import be.nikiroo.utils.Progress;
 
 public class WebLibraryServer extends WebLibraryServerHtml {
        class WLoginResult extends LoginResult {
-               private boolean rw;
-               private boolean wl;
-               private boolean bl;
-
                public WLoginResult(boolean badLogin, boolean badCookie) {
                        super(badLogin, badCookie);
                }
@@ -43,9 +39,6 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                                boolean wl, boolean bl) {
                        super(who, key, subkey, (rw ? "|rw" : "") + (wl ? "|wl" : "")
                                        + (bl ? "|bl" : "") + "|");
-                       this.rw = rw;
-                       this.wl = wl;
-                       this.bl = bl;
                }
 
                public WLoginResult(String cookie, String who, String key,
@@ -306,6 +299,11 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                                        NanoHTTPD.MIME_PLAINTEXT, "Invalid story part request");
                }
 
+               if (!login.isRw()) {
+                       return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
+                                       NanoHTTPD.MIME_PLAINTEXT, "SET story part not allowed");
+               }
+
                String luid = uriParts[off + 0];
                String type = uriParts[off + 1];
 
@@ -381,6 +379,11 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                                        NanoHTTPD.MIME_PLAINTEXT, "Invalid cover request");
                }
 
+               if (!login.isRw()) {
+                       return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
+                                       NanoHTTPD.MIME_PLAINTEXT, "Cover request not allowed");
+               }
+
                String type = uriParts[off + 0];
                String id = uriParts[off + 1];
 
@@ -402,6 +405,11 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        throws IOException {
                final BasicLibrary lib = Instance.getInstance().getLibrary();
 
+               if (!login.isRw()) {
+                       return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
+                                       NanoHTTPD.MIME_PLAINTEXT, "Import not allowed");
+               }
+
                final URL url = new URL(urlStr);
                final Progress pg = new Progress();
                final String luid = lib.getNextId();
@@ -425,8 +433,6 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        }
                }, "Import story: " + urlStr).start();
 
-               lib.imprt(url, pg);
-
                return NanoHTTPD.newFixedLengthResponse(Status.OK,
                                NanoHTTPD.MIME_PLAINTEXT, luid);
        }
@@ -455,6 +461,30 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                return newInputStreamResponse(NanoHTTPD.MIME_PLAINTEXT, null);
        }
 
+       @Override
+       protected Response delete(String uri, WLoginResult login)
+                       throws IOException {
+               String[] uriParts = uri.split("/");
+               int off = 2; // "" and "delete"
+
+               if (uriParts.length < off + 1) {
+                       return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST,
+                                       NanoHTTPD.MIME_PLAINTEXT, "Invalid delete request");
+               }
+
+               if (!login.isRw()) {
+                       return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
+                                       NanoHTTPD.MIME_PLAINTEXT, "Delete not allowed");
+               }
+
+               String luid = uriParts[off + 0];
+
+               BasicLibrary lib = Instance.getInstance().getLibrary();
+               lib.delete(luid);
+
+               return newInputStreamResponse(NanoHTTPD.MIME_PLAINTEXT, null);
+       }
+
        @Override
        protected List<MetaData> metas(WLoginResult login) throws IOException {
                BasicLibrary lib = Instance.getInstance().getLibrary();
index a534b4cdafbbdd8ec1c4c6b373bb7fc2953ec58c..e96edef95584b5f18a63f62ab630c75abe92f6b0 100644 (file)
@@ -67,6 +67,9 @@ abstract class WebLibraryServerHtml implements Runnable {
 
        protected abstract Response imprtProgress(String uri, WLoginResult login);
 
+       protected abstract Response delete(String uri, WLoginResult login)
+                       throws IOException;
+
        public WebLibraryServerHtml(boolean secure) throws IOException {
                Integer port = Instance.getInstance().getConfig()
                                .getInteger(Config.SERVER_PORT);
@@ -205,6 +208,8 @@ abstract class WebLibraryServerHtml implements Runnable {
                                                                } else {
                                                                        rep = imprtProgress(uri, login);
                                                                }
+                                                       } else if (WebLibraryUrls.isDeleteUrl(uri)) {
+                                                               rep = delete(uri, login);
                                                        } else {
                                                                getTraceHandler().error(
                                                                                "Supported URL was not processed: "
index 41f1c82feb17c2312ddc3868f5fd9176f3886c59..176b4ebce368e31ebce00f04d8b27f07a69f83c3 100644 (file)
@@ -38,6 +38,9 @@ class WebLibraryUrls {
        static private final String IMPRT_URL_PROGRESS = IMPRT_URL_BASE + "{luid}";
        static public final String IMPRT_URL_IMPORT = IMPRT_URL_BASE + "import";
 
+       static private final String DELETE_URL_BASE = "/delete/";
+       static private final String DELETE_URL_STORY = DELETE_URL_BASE + "{luid}";
+
        // 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/";
@@ -119,6 +122,11 @@ class WebLibraryUrls {
                                .replace("{author}", author);
        }
 
+       static public String getDeleteUrlStory(String luid) {
+               return DELETE_URL_STORY //
+                               .replace("{luid}", luid);
+       }
+
        static public boolean isViewUrl(String url) {
                return url != null && url.startsWith(VIEWER_URL_BASE);
        }
@@ -138,4 +146,8 @@ class WebLibraryUrls {
        static public boolean isImprtUrl(String url) {
                return url != null && url.startsWith(IMPRT_URL_BASE);
        }
+
+       static public boolean isDeleteUrl(String url) {
+               return url != null && url.startsWith(DELETE_URL_BASE);
+       }
 }