From 6673ec5924d57a37c4f995f9c81c50a187b51c63 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Thu, 14 May 2020 10:44:19 +0200 Subject: [PATCH] add get custom covers --- src/be/nikiroo/fanfix/library/WebLibrary.java | 20 +++-- .../fanfix/library/WebLibraryServer.java | 88 +++++++++++++++++-- .../fanfix/library/WebLibraryServerHtml.java | 9 +- .../fanfix/library/WebLibraryUrls.java | 30 ++++++- 4 files changed, 132 insertions(+), 15 deletions(-) diff --git a/src/be/nikiroo/fanfix/library/WebLibrary.java b/src/be/nikiroo/fanfix/library/WebLibrary.java index 9769fea..6f2bfda 100644 --- a/src/be/nikiroo/fanfix/library/WebLibrary.java +++ b/src/be/nikiroo/fanfix/library/WebLibrary.java @@ -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 diff --git a/src/be/nikiroo/fanfix/library/WebLibraryServer.java b/src/be/nikiroo/fanfix/library/WebLibraryServer.java index 3ccdc3d..a45fdec 100644 --- a/src/be/nikiroo/fanfix/library/WebLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/WebLibraryServer.java @@ -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 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 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 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()) { diff --git a/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java b/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java index c28188c..6489fdf 100644 --- a/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java +++ b/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java @@ -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 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."); diff --git a/src/be/nikiroo/fanfix/library/WebLibraryUrls.java b/src/be/nikiroo/fanfix/library/WebLibraryUrls.java index be4cf25..0dd4260 100644 --- a/src/be/nikiroo/fanfix/library/WebLibraryUrls.java +++ b/src/be/nikiroo/fanfix/library/WebLibraryUrls.java @@ -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); + } } -- 2.27.0