X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=library%2FWebLibrary.java;h=ac1349e7ac51b3015050a4725a28265ef13a211a;hp=369eb23491cd6410cf45027f95a8a0612ee324d5;hb=159e970c23d9634d39bb8cf2a194f7088f4d59ab;hpb=5f3671e17febc5b7f6abbfc62c66c4045d47ec8d diff --git a/library/WebLibrary.java b/library/WebLibrary.java index 369eb23..ac1349e 100644 --- a/library/WebLibrary.java +++ b/library/WebLibrary.java @@ -13,12 +13,16 @@ import org.json.JSONArray; import org.json.JSONObject; import be.nikiroo.fanfix.Instance; +import be.nikiroo.fanfix.data.Chapter; import be.nikiroo.fanfix.data.JsonIO; import be.nikiroo.fanfix.data.MetaData; +import be.nikiroo.fanfix.data.Paragraph; +import be.nikiroo.fanfix.data.Paragraph.ParagraphType; import be.nikiroo.fanfix.data.Story; import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.Image; import be.nikiroo.utils.Progress; +import be.nikiroo.utils.Version; /** * This {@link BasicLibrary} will access a remote server to list the available @@ -99,21 +103,39 @@ public class WebLibrary extends BasicLibrary { this.host = host; this.port = port; + } - // TODO: not supported yet - this.rw = false; + /** + * Return the version of the program running server-side. + *

+ * Never returns NULL. + * + * @return the version or an empty {@link Version} if not known + */ + public Version getVersion() { + try { + InputStream in = post(WebLibraryUrls.VERSION_URL); + try { + return new Version(IOUtils.readSmallStream(in)); + } finally { + in.close(); + } + } catch (IOException e) { + } + + return new Version(); } @Override public Status getStatus() { try { - download("/"); + post(WebLibraryUrls.INDEX_URL).close(); } catch (IOException e) { try { - download("/style.css"); + post("/style.css").close(); return Status.UNAUTHORIZED; } catch (IOException ioe) { - return Status.INVALID; + return Status.UNAVAILABLE; } } @@ -122,35 +144,124 @@ public class WebLibrary extends BasicLibrary { @Override public String getLibraryName() { - return (rw ? "[READ-ONLY] " : "") + host + ":" + port; + return (rw ? "[READ-ONLY] " : "") + host + ":" + port + " (" + + getVersion() + ")"; } @Override public Image getCover(String luid) throws IOException { - InputStream in = download("/story/" + luid + "/cover"); - if (in != null) { - return new Image(in); + InputStream in = post(WebLibraryUrls.getStoryUrlCover(luid)); + try { + Image img = new Image(in); + if (img.getSize() > 0) { + return img; + } + + return null; + } finally { + in.close(); + } + } + + @Override + public Image getCustomSourceCover(String source) throws IOException { + InputStream in = post(WebLibraryUrls.getCoverUrlSource(source)); + try { + Image img = new Image(in); + if (img.getSize() > 0) { + return img; + } + + return null; + } finally { + in.close(); } + } - return null; + @Override + public Image getCustomAuthorCover(String author) throws IOException { + InputStream in = post(WebLibraryUrls.getCoverUrlAuthor(author)); + try { + Image img = new Image(in); + if (img.getSize() > 0) { + return img; + } + + return null; + } finally { + in.close(); + } } @Override public void setSourceCover(String source, String luid) throws IOException { - // TODO Auto-generated method stub - throw new IOException("Not implemented yet"); + Map post = new HashMap(); + post.put("luid", luid); + post(WebLibraryUrls.getCoverUrlSource(source), post).close(); } @Override public void setAuthorCover(String author, String luid) throws IOException { - // TODO Auto-generated method stub - throw new IOException("Not implemented yet"); + Map post = new HashMap(); + post.put("luid", luid); + post(WebLibraryUrls.getCoverUrlAuthor(author), post).close(); + } + + @Override + public synchronized Story getStory(final String luid, Progress pg) + throws IOException { + if (pg == null) { + pg = new Progress(); + } + + Story story; + InputStream in = post(WebLibraryUrls.getStoryUrlJson(luid)); + try { + JSONObject json = new JSONObject(IOUtils.readSmallStream(in)); + story = JsonIO.toStory(json); + } finally { + in.close(); + } + + int max = 0; + for (Chapter chap : story) { + max += chap.getParagraphs().size(); + } + pg.setMinMax(0, max); + + story.getMeta().setCover(getCover(luid)); + int chapNum = 1; + for (Chapter chap : story) { + int number = 1; + for (Paragraph para : chap) { + if (para.getType() == ParagraphType.IMAGE) { + InputStream subin = post( + WebLibraryUrls.getStoryUrl(luid, chapNum, number)); + try { + Image img = new Image(subin); + if (img.getSize() > 0) { + para.setContentImage(img); + } + } finally { + subin.close(); + } + } + + pg.add(1); + number++; + } + + chapNum++; + } + + pg.done(); + return story; } @Override protected List getMetas(Progress pg) throws IOException { List metas = new ArrayList(); - InputStream in = download("/list/luids"); + InputStream in = post(WebLibraryUrls.LIST_URL_METADATA); JSONArray jsonArr = new JSONArray(IOUtils.readSmallStream(in)); for (int i = 0; i < jsonArr.length(); i++) { JSONObject json = jsonArr.getJSONObject(i); @@ -163,8 +274,9 @@ public class WebLibrary extends BasicLibrary { @Override // Could work (more slowly) without it public MetaData imprt(final URL url, Progress pg) throws IOException { - if (true) - throw new IOException("Not implemented yet"); + if (pg == null) { + pg = new Progress(); + } // Import the file locally if it is actually a file @@ -174,8 +286,46 @@ public class WebLibrary extends BasicLibrary { // Import it remotely if it is an URL - // TODO - return super.imprt(url, pg); + try { + String luid = null; + + Map post = new HashMap(); + post.put("url", url.toString()); + InputStream in = post(WebLibraryUrls.IMPRT_URL_IMPORT, post); + try { + luid = IOUtils.readSmallStream(in); + } finally { + in.close(); + } + + Progress subPg = null; + do { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + } + + in = post(WebLibraryUrls.getImprtProgressUrl(luid)); + try { + subPg = JsonIO.toProgress( + new JSONObject(IOUtils.readSmallStream(in))); + } catch (Exception e) { + subPg = null; + } finally { + in.close(); + } + } while (subPg != null); + + in = post(WebLibraryUrls.getStoryUrlMetadata(luid)); + try { + return JsonIO.toMetaData( + new JSONObject(IOUtils.readSmallStream(in))); + } finally { + in.close(); + } + } finally { + pg.done(); + } } @Override @@ -183,8 +333,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 @@ -200,7 +366,7 @@ public class WebLibrary extends BasicLibrary { // The following methods are only used by Save and Delete in BasicLibrary: @Override - protected int getNextId() { + protected String getNextId() { throw new java.lang.InternalError("Should not have been called"); } @@ -222,11 +388,19 @@ public class WebLibrary extends BasicLibrary { "Operation not supportorted on remote Libraries"); } - // starts with "/" - private InputStream download(String path) throws IOException { + // starts with "/", never NULL + private InputStream post(String path) throws IOException { + return post(path, null); + } + + // starts with "/", never NULL + private InputStream post(String path, Map post) + throws IOException { URL url = new URL(host + ":" + port + path); - Map post = new HashMap(); + if (post == null) { + post = new HashMap(); + } post.put("login", subkey); post.put("password", key);