From 089e354e5efc0de39caa4df2f3987d573b71dcbc Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Thu, 14 May 2020 11:21:23 +0200 Subject: [PATCH] weblib: change STA --- src/be/nikiroo/fanfix/library/WebLibrary.java | 20 +++++++++- .../fanfix/library/WebLibraryServer.java | 37 +++++++++++++++++++ .../fanfix/library/WebLibraryServerHtml.java | 10 ++++- .../fanfix/library/WebLibraryUrls.java | 23 ++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/be/nikiroo/fanfix/library/WebLibrary.java b/src/be/nikiroo/fanfix/library/WebLibrary.java index 79653308..040acd07 100644 --- a/src/be/nikiroo/fanfix/library/WebLibrary.java +++ b/src/be/nikiroo/fanfix/library/WebLibrary.java @@ -263,8 +263,24 @@ public class WebLibrary extends BasicLibrary { protected synchronized void changeSTA(final String luid, final String newSource, final String newTitle, final String newAuthor, Progress pg) throws IOException { - // TODO - super.changeSTA(luid, newSource, newTitle, newAuthor, pg); + MetaData meta = getInfo(luid); + if (meta != null) { + if (!meta.getSource().equals(newSource)) { + Map post = new HashMap(); + post.put("value", newSource); + post(WebLibraryUrls.getStoryUrlSource(luid), post).close(); + } + if (!meta.getTitle().equals(newTitle)) { + Map post = new HashMap(); + post.put("value", newTitle); + post(WebLibraryUrls.getStoryUrlTitle(luid), post).close(); + } + if (!meta.getAuthor().equals(newAuthor)) { + Map post = new HashMap(); + post.put("value", newAuthor); + post(WebLibraryUrls.getStoryUrlAuthor(luid), post).close(); + } + } } @Override diff --git a/src/be/nikiroo/fanfix/library/WebLibraryServer.java b/src/be/nikiroo/fanfix/library/WebLibraryServer.java index dfbceeeb..55e8ed3d 100644 --- a/src/be/nikiroo/fanfix/library/WebLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/WebLibraryServer.java @@ -288,6 +288,43 @@ public class WebLibraryServer extends WebLibraryServerHtml { return newInputStreamResponse(mimeType, in); } + // /story/luid/source + // /story/luid/title + // /story/luid/author + @Override + protected Response setStoryPart(String uri, String value, + WLoginResult login) throws IOException { + String[] uriParts = uri.split("/"); + int off = 2; // "" and "story" + + if (uriParts.length < off + 2) { + return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST, + NanoHTTPD.MIME_PLAINTEXT, "Invalid story part request"); + } + + String luid = uriParts[off + 0]; + String type = uriParts[off + 1]; + + if (!Arrays.asList("source", "title", "author").contains(type)) { + return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST, + NanoHTTPD.MIME_PLAINTEXT, + "Invalid SET story part: " + type); + } + + if (meta(luid, login) != null) { + BasicLibrary lib = Instance.getInstance().getLibrary(); + if ("source".equals(type)) { + lib.changeSource(luid, value, null); + } else if ("title".equals(type)) { + lib.changeTitle(luid, value, null); + } else if ("author".equals(type)) { + lib.changeAuthor(luid, value, null); + } + } + + return newInputStreamResponse(NanoHTTPD.MIME_PLAINTEXT, null); + } + @Override protected Response getCover(String uri, WLoginResult login) throws IOException { diff --git a/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java b/src/be/nikiroo/fanfix/library/WebLibraryServerHtml.java index e79d6f5e..77b8efed 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 setStoryPart(String uri, String value, + WLoginResult login) throws IOException; + abstract protected Response getCover(String uri, WLoginResult login) throws IOException; @@ -178,7 +181,12 @@ abstract class WebLibraryServerHtml implements Runnable { } else if (WebLibraryUrls.isListUrl(uri)) { rep = getList(uri, login); } else if (WebLibraryUrls.isStoryUrl(uri)) { - rep = getStoryPart(uri, login); + String value = params.get("value"); + if (value != null) { + rep = setStoryPart(uri, value, login); + } else { + rep = getStoryPart(uri, login); + } } else if (WebLibraryUrls.isViewUrl(uri)) { rep = getViewer(cookies, uri, login); } else if (WebLibraryUrls.LOGOUT_URL.equals(uri)) { diff --git a/src/be/nikiroo/fanfix/library/WebLibraryUrls.java b/src/be/nikiroo/fanfix/library/WebLibraryUrls.java index 2f367318..c1d1cf21 100644 --- a/src/be/nikiroo/fanfix/library/WebLibraryUrls.java +++ b/src/be/nikiroo/fanfix/library/WebLibraryUrls.java @@ -18,6 +18,14 @@ class WebLibraryUrls { + "{luid}/cover"; static private final String STORY_URL_JSON = STORY_URL_BASE + "{luid}/json"; + // GET/SET ("value" param -> set STA to this value) + static private final String STORY_URL_SOURCE = STORY_URL_BASE + + "{luid}/source"; + static private final String STORY_URL_TITLE = STORY_URL_BASE + + "{luid}/title"; + static private final String STORY_URL_AUTHOR = STORY_URL_BASE + + "{luid}/author"; + static private final String LIST_URL_BASE = "/list/"; static public final String LIST_URL_METADATA = LIST_URL_BASE + "metadata"; @@ -57,6 +65,21 @@ class WebLibraryUrls { .replace("{luid}", luid); } + static public String getStoryUrlSource(String luid) { + return STORY_URL_SOURCE // + .replace("{luid}", luid); + } + + static public String getStoryUrlTitle(String luid) { + return STORY_URL_TITLE// + .replace("{luid}", luid); + } + + static public String getStoryUrlAuthor(String luid) { + return STORY_URL_AUTHOR // + .replace("{luid}", luid); + } + static public boolean isSupportedUrl(String url) { return INDEX_URL.equals(url) || VERSION_URL.equals(url) || LOGOUT_URL.equals(url) || isViewUrl(url) || isStoryUrl(url) -- 2.27.0