From 159e970c23d9634d39bb8cf2a194f7088f4d59ab Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Thu, 14 May 2020 12:54:13 +0200 Subject: [PATCH] merge from master --- library/WebLibrary.java | 28 +++++++++++++++--- library/WebLibraryServer.java | 48 +++++++++++++++++++++++++------ library/WebLibraryServerHtml.java | 5 ++++ library/WebLibraryUrls.java | 12 ++++++++ 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/library/WebLibrary.java b/library/WebLibrary.java index 9d1c773..ac1349e 100644 --- a/library/WebLibrary.java +++ b/library/WebLibrary.java @@ -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(); } diff --git a/library/WebLibraryServer.java b/library/WebLibraryServer.java index 5cda3b5..810cc4d 100644 --- a/library/WebLibraryServer.java +++ b/library/WebLibraryServer.java @@ -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 metas(WLoginResult login) throws IOException { BasicLibrary lib = Instance.getInstance().getLibrary(); diff --git a/library/WebLibraryServerHtml.java b/library/WebLibraryServerHtml.java index a534b4c..e96edef 100644 --- a/library/WebLibraryServerHtml.java +++ b/library/WebLibraryServerHtml.java @@ -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: " diff --git a/library/WebLibraryUrls.java b/library/WebLibraryUrls.java index 41f1c82..176b4eb 100644 --- a/library/WebLibraryUrls.java +++ b/library/WebLibraryUrls.java @@ -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); + } } -- 2.27.0