update from master
authorNiki Roo <niki@nikiroo.be>
Fri, 15 May 2020 11:03:20 +0000 (13:03 +0200)
committerNiki Roo <niki@nikiroo.be>
Fri, 15 May 2020 11:03:20 +0000 (13:03 +0200)
15 files changed:
Main.java
library/LocalLibrary.java
library/WebLibrary.java
library/WebLibraryServer.java
library/WebLibraryServerHtml.java
library/WebLibraryUrls.java
searchable/Fanfiction.java
searchable/MangaLel.java
supported/BasicSupportHelper.java
supported/BasicSupportImages.java
supported/BasicSupport_Deprecated.java
supported/Cbz.java
supported/Epub.java
supported/FimfictionApi.java
supported/MangaLel.java

index e48f4f53ec7ab7d1951ea4760cacdb64afbccc51..35365444682489d81adf7b25ed864416d6c47abe 100644 (file)
--- a/Main.java
+++ b/Main.java
@@ -643,10 +643,36 @@ public class Main {
                                break;
                        case STOP_SERVER:
                                // Can be given via "--remote XX XX XX"
-                               if (key == null)
-                                       key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY);
-                               if (port == null)
+                               if (key == null) {
+                                       key = Instance.getInstance().getConfig()
+                                                       .getString(Config.SERVER_KEY);
+                                       
+                                       // If a subkey in RW mode exists, use it
+                                       for (String subkey : Instance.getInstance().getConfig()
+                                                       .getList(Config.SERVER_ALLOWED_SUBKEYS,
+                                                                       new ArrayList<String>())) {
+                                               if ((subkey + "|").contains("|rw|")) {
+                                                       key = key + "|" + subkey;
+                                                       break;
+                                               }
+                                       }
+                               }
+                               
+                               if (port == null) {
                                        port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT);
+                               }
+                               
+                               if (host == null) {
+                                       String mode = Instance.getInstance().getConfig()
+                                                       .getString(Config.SERVER_MODE, "fanfix");
+                                       if ("http".equals(mode)) {
+                                               host = "http://localhost";
+                                       } else if ("https".equals(mode)) {
+                                               host = "https://localhost";
+                                       } else if ("fanfix".equals(mode)) {
+                                               host = "fanfix://localhost";
+                                       }
+                               }
 
                                if (port == null) {
                                        System.err.println("No port given nor configured in the config file");
@@ -1069,7 +1095,7 @@ public class Main {
         * @param key
         *            the key to contact the Fanfix server
         * @param host
-        *            the host on which it runs (NULL means localhost)
+        *            the host on which it runs
         * @param port
         *            the port on which it runs
         *            
@@ -1078,7 +1104,7 @@ public class Main {
         * @throws SSLException
         *             when the key was not accepted
         */
-       private void stopServer(String key, String host, Integer port)
+       private void stopServer(String key, String host, int port)
                        throws IOException, SSLException {
                if (host.startsWith("http://") || host.startsWith("https://")) {
                        new WebLibrary(key, host, port).stop();
index 7220a3951de137f99a4a11ecc7e5f5b119abb3c1..f655d4d03bb43b5df69d00fc8b30b2adbad6c113 100644 (file)
@@ -254,7 +254,13 @@ public class LocalLibrary extends BasicLibrary {
                                        in = new FileInputStream(cover);
                                        try {
                                                synchronized (lock) {
-                                                       sourceCovers.put(source, new Image(in));
+                                                       Image img = new Image(in);
+                                                       if (img.getSize() == 0) {
+                                                               img.close();
+                                                               throw new IOException(
+                                                                               "Empty image not accepted");
+                                                       }
+                                                       sourceCovers.put(source, img);
                                                }
                                        } finally {
                                                in.close();
@@ -298,7 +304,13 @@ public class LocalLibrary extends BasicLibrary {
                                in = new FileInputStream(cover);
                                try {
                                        synchronized (lock) {
-                                               authorCovers.put(author, new Image(in));
+                                               Image img = new Image(in);
+                                               if (img.getSize() == 0) {
+                                                       img.close();
+                                                       throw new IOException(
+                                                                       "Empty image not accepted");
+                                               }
+                                               authorCovers.put(author, img);
                                        }
                                } finally {
                                        in.close();
index 7566877cebd48343b47c1764ed31045ee8de78dd..9af38e2b6bd1c7ccb565ca6f244e31ef42f40746 100644 (file)
@@ -133,7 +133,17 @@ public class WebLibrary extends BasicLibrary {
         *             in case of I/O errors
         */
        public void stop() throws IOException {
-               post(WebLibraryUrls.EXIT_URL, null).close();
+               try {
+                       post(WebLibraryUrls.EXIT_URL, null).close();
+               } catch (Exception e) {
+                       try {
+                               Thread.sleep(200);
+                       } catch (InterruptedException e1) {
+                       }
+                       if (getStatus() != Status.UNAVAILABLE) {
+                               throw new IOException("Cannot exit the library", e);
+                       }
+               }
        }
 
        @Override
@@ -164,6 +174,7 @@ public class WebLibrary extends BasicLibrary {
                try {
                        Image img = new Image(in);
                        if (img.getSize() > 0) {
+                               img.close();
                                return img;
                        }
 
@@ -179,6 +190,7 @@ public class WebLibrary extends BasicLibrary {
                try {
                        Image img = new Image(in);
                        if (img.getSize() > 0) {
+                               img.close();
                                return img;
                        }
 
@@ -194,6 +206,7 @@ public class WebLibrary extends BasicLibrary {
                try {
                        Image img = new Image(in);
                        if (img.getSize() > 0) {
+                               img.close();
                                return img;
                        }
 
index 50dc3d942dcf39aeeadeb68d88c58a714e7551cf..22f36e91f5729a7bfef72fe9c98d2a7fa157a6ec 100644 (file)
@@ -110,7 +110,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
                                        NanoHTTPD.MIME_PLAINTEXT, "Server is already exiting...");
                }
-               
+
                exiting = true;
                Instance.getInstance().getTraceHandler().trace("Exiting");
 
@@ -131,6 +131,20 @@ public class WebLibraryServer extends WebLibraryServerHtml {
 
                doStop();
 
+               new Thread(new Runnable() {
+                       @Override
+                       public void run() {
+                               try {
+                                       Thread.sleep(1500);
+                               } catch (InterruptedException e) {
+                               }
+
+                               Instance.getInstance().getTraceHandler()
+                                               .trace("Exit timeout: force-quit");
+                               System.exit(0);
+                       }
+               }, "Exit program after timeout of 1500 ms").start();
+
                return NanoHTTPD.newFixedLengthResponse(Status.OK,
                                NanoHTTPD.MIME_PLAINTEXT, "Exited");
        }
@@ -341,7 +355,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
                                        NanoHTTPD.MIME_PLAINTEXT, "SET story part not allowed");
                }
-               
+
                if (exiting) {
                        return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
                                        NanoHTTPD.MIME_PLAINTEXT, "Server is exiting...");
@@ -426,7 +440,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
                                        NanoHTTPD.MIME_PLAINTEXT, "Cover request not allowed");
                }
-               
+
                if (exiting) {
                        return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
                                        NanoHTTPD.MIME_PLAINTEXT, "Server is exiting...");
@@ -457,7 +471,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
                                        NanoHTTPD.MIME_PLAINTEXT, "Import not allowed");
                }
-               
+
                if (exiting) {
                        return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
                                        NanoHTTPD.MIME_PLAINTEXT, "Server is exiting...");
@@ -529,7 +543,7 @@ public class WebLibraryServer extends WebLibraryServerHtml {
                        return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN,
                                        NanoHTTPD.MIME_PLAINTEXT, "Delete not allowed");
                }
-               
+
                if (exiting) {
                        return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE,
                                        NanoHTTPD.MIME_PLAINTEXT, "Server is exiting...");
index 7351d0dd3665bceffb58b02574345dfcb498d940..8895fb83a07b0eb6ff7116b3998a7a89a2acfb15 100644 (file)
@@ -173,13 +173,14 @@ abstract class WebLibraryServerHtml implements Runnable {
                                }
 
                                Response rep = null;
-                               if (!login.isSuccess() && WebLibraryUrls.isSupportedUrl(uri)) {
+                               if (!login.isSuccess()
+                                               && WebLibraryUrls.isSupportedUrl(uri, true)) {
                                        rep = loginPage(login, uri);
                                }
 
                                if (rep == null) {
                                        try {
-                                               if (WebLibraryUrls.isSupportedUrl(uri)) {
+                                               if (WebLibraryUrls.isSupportedUrl(uri, false)) {
                                                        if (WebLibraryUrls.INDEX_URL.equals(uri)) {
                                                                rep = root(session, cookies, login);
                                                        } else if (WebLibraryUrls.VERSION_URL.equals(uri)) {
index c02fc382341481ed9c8a95a74a16faf13544648b..2b757798c5aec05c044e29b4971a7043a1f59baa 100644 (file)
@@ -103,11 +103,16 @@ class WebLibraryUrls {
                                .replace("{luid}", luid);
        }
 
-       static public boolean isSupportedUrl(String url) {
-               return INDEX_URL.equals(url) || VERSION_URL.equals(url)
-                               || LOGOUT_URL.equals(url) || EXIT_URL.equals(url)
-                               || isViewUrl(url) || isStoryUrl(url) || isListUrl(url)
-                               || isCoverUrl(url) || isImprtUrl(url) || isDeleteUrl(url);
+       static public boolean isSupportedUrl(String url,
+                       boolean requiresLoginOnly) {
+               if (requiresLoginOnly) {
+                       return INDEX_URL.equals(url) || LOGOUT_URL.equals(url)
+                                       || EXIT_URL.equals(url) || isViewUrl(url) || isStoryUrl(url)
+                                       || isListUrl(url) || isCoverUrl(url) || isImprtUrl(url)
+                                       || isDeleteUrl(url);
+               }
+
+               return isSupportedUrl(url, true) || VERSION_URL.equals(url);
        }
 
        static public String getCoverUrlStory(String luid) {
index e2fba1ff404f17d2a23bc18defbc2b03d7a425e3..cbc927bcc2d981b028311be59fa2d1ba7c152542 100644 (file)
@@ -287,7 +287,13 @@ class Fanfiction extends BasicSearchable {
                                        try {
                                                InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(), true);
                                                try {
-                                                       meta.setCover(new Image(in));
+                                                       Image img = new Image(in);
+                                                       if (img.getSize() == 0) {
+                                                               img.close();
+                                                               throw new IOException(
+                                                                               "Empty image not accepted");
+                                                       }
+                                                       meta.setCover(img);
                                                } finally {
                                                        in.close();
                                                }
index 5ba21a0e1ce25b799dc0b9a6d14d3a447135ef01..354edb435ef281a0dab3a31f55218f63c2ab6586 100644 (file)
@@ -140,7 +140,13 @@ class MangaLel extends BasicSearchable {
                                                        InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(),
                                                                        true);
                                                        try {
-                                                               meta.setCover(new Image(in));
+                                                               Image ii = new Image(in);
+                                                               if (ii.getSize() == 0) {
+                                                                       ii.close();
+                                                                       throw new IOException(
+                                                                                       "Empty image not accepted");
+                                                               }
+                                                               meta.setCover(ii);
                                                        } finally {
                                                                in.close();
                                                        }
index 7768052cafc758094e38c1968f5919f6b853b8bc..f3c30bc9657dfe1a70555d8104cd591ad3bf1523 100644 (file)
@@ -85,7 +85,13 @@ public class BasicSupportHelper {
                        InputStream in = null;
                        try {
                                in = Instance.getInstance().getCache().open(url, support, true);
-                               return new Image(in);
+                               Image img = new Image(in);
+                               if (img.getSize() == 0) {
+                                       img.close();
+                                       throw new IOException(
+                                                       "Empty image not accepted");
+                               }
+                               return img;
                        } catch (IOException e) {
                        } finally {
                                if (in != null) {
index 576cb17e78bb3abfe845a1b053df47618636e5c6..f56b50c2b67a6dd9f5a4b1e0de572767d0a42dde 100644 (file)
@@ -55,7 +55,13 @@ public class BasicSupportImages {
                        InputStream in = null;
                        try {
                                in = Instance.getInstance().getCache().open(url, support, true);
-                               return new Image(in);
+                               Image img = new Image(in);
+                               if (img.getSize() == 0) {
+                                       img.close();
+                                       throw new IOException(
+                                                       "Empty image not accepted");
+                               }
+                               return img;
                        } catch (IOException e) {
                        } finally {
                                if (in != null) {
index a50ee3cf48a962a38b5ebe52b33184c956169ffd..40ff3fc027c7e52beca3426017e1a6a58972fbbe 100644 (file)
@@ -651,7 +651,13 @@ public abstract class BasicSupport_Deprecated extends BasicSupport {
                        InputStream in = null;
                        try {
                                in = Instance.getInstance().getCache().open(url, getSupport(url), true);
-                               return new Image(in);
+                               Image img = new Image(in);
+                               if (img.getSize() == 0) {
+                                       img.close();
+                                       throw new IOException(
+                                                       "Empty image not accepted");
+                               }
+                               return img;
                        } catch (IOException e) {
                        } finally {
                                if (in != null) {
index 7fe496d97ea8acb9e8869bb41c37d6723e19a636..a5391d07575e9f6102ab531e4a756c1be718f711 100644 (file)
@@ -97,7 +97,13 @@ class Cbz extends Epub {
                                        if (imageEntry) {
                                                String uuid = meta.getUuid() + "_" + entry.getName();
                                                try {
-                                                       images.put(uuid, new Image(zipIn));
+                                                       Image img = new Image(zipIn);
+                                                       if (img.getSize() == 0) {
+                                                               img.close();
+                                                               throw new IOException(
+                                                                               "Empty image not accepted");
+                                                       }
+                                                       images.put(uuid, img);
                                                } catch (Exception e) {
                                                        Instance.getInstance().getTraceHandler().error(e);
                                                }
index 965a27affda88bfe8919e744b80962f58c3aa465..783d724e36aaa84e822f1fb5f4c6590371a4a8fd 100644 (file)
@@ -128,7 +128,13 @@ class Epub extends InfoText {
                                                // Cover
                                                if (getCover() && cover == null) {
                                                        try {
-                                                               cover = new Image(zipIn);
+                                                               Image img = new Image(zipIn);
+                                                               if (img.getSize() == 0) {
+                                                                       img.close();
+                                                                       throw new IOException(
+                                                                                       "Empty image not accepted");
+                                                               }
+                                                               cover = img;
                                                        } catch (Exception e) {
                                                                Instance.getInstance().getTraceHandler()
                                                                                .error(e);
index 43d01d19494f616c6811e5757da4a6f2e14c83cc..e6dd6118721b93aec36c0d7c734faad1c6d9a95b 100644 (file)
@@ -147,7 +147,13 @@ class FimfictionApi extends BasicSupport {
                        try {
                                InputStream in = Instance.getInstance().getCache().open(coverImageUrl, null, true);
                                try {
-                                       meta.setCover(new Image(in));
+                                       Image img = new Image(in);
+                                       if (img.getSize() == 0) {
+                                               img.close();
+                                               throw new IOException(
+                                                               "Empty image not accepted");
+                                       }
+                                       meta.setCover(img);
                                } finally {
                                        in.close();
                                }
index de0b871331ef313b6be628c382c9487f281b5c3e..5910a371aedf7272e546ec7170b508516879ced1 100644 (file)
@@ -138,11 +138,17 @@ class MangaLel extends BasicSupport {
                        if (img != null) {
                                String coverUrl = img.absUrl("src");
 
-                               InputStream coverIn;
                                try {
-                                       coverIn = Instance.getInstance().getCache().open(new URL(coverUrl), this, true);
+                                       InputStream coverIn = Instance.getInstance().getCache()
+                                                       .open(new URL(coverUrl), this, true);
                                        try {
-                                               return new Image(coverIn);
+                                               Image ii = new Image(coverIn);
+                                               if (ii.getSize() == 0) {
+                                                       ii.close();
+                                                       throw new IOException("Empty image not accepted");
+                                               }
+                                               
+                                               return ii;
                                        } finally {
                                                coverIn.close();
                                        }