weblib: change STA
authorNiki Roo <niki@nikiroo.be>
Thu, 14 May 2020 09:21:23 +0000 (11:21 +0200)
committerNiki Roo <niki@nikiroo.be>
Thu, 14 May 2020 09:21:23 +0000 (11:21 +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 796533080fba899b60a553f5ac90b80d71d56f58..040acd07f1505acb49845d4ad153a3661dbae8b6 100644 (file)
@@ -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<String, String> post = new HashMap<String, String>();
+                               post.put("value", newSource);
+                               post(WebLibraryUrls.getStoryUrlSource(luid), post).close();
+                       }
+                       if (!meta.getTitle().equals(newTitle)) {
+                               Map<String, String> post = new HashMap<String, String>();
+                               post.put("value", newTitle);
+                               post(WebLibraryUrls.getStoryUrlTitle(luid), post).close();
+                       }
+                       if (!meta.getAuthor().equals(newAuthor)) {
+                               Map<String, String> post = new HashMap<String, String>();
+                               post.put("value", newAuthor);
+                               post(WebLibraryUrls.getStoryUrlAuthor(luid), post).close();
+                       }
+               }
        }
 
        @Override
index dfbceeeb7a19ce09cdbd398fa45a36789a758811..55e8ed3d146658f5529471eb645d3caa26c1fe77 100644 (file)
@@ -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 {
index e79d6f5ea4ad0a736dbaea826447d61682b50ad6..77b8efede1a4eb9580219a74cf9d6aee62157469 100644 (file)
@@ -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)) {
index 2f36731871991449f63fead0265d8c5f79a9e499..c1d1cf21f3621ee4a89c99d2b96508fe3a3d1cea 100644 (file)
@@ -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)