X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FWebLibrary.java;h=9769fea4420f14719b7053cbf9baeb04ce7a5119;hb=8dc26a0ac5e5f1aa9296c5db71114dce19503973;hp=369eb23491cd6410cf45027f95a8a0612ee324d5;hpb=2048e74238ad24e52da623a92c31460e05ce495e;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/library/WebLibrary.java b/src/be/nikiroo/fanfix/library/WebLibrary.java index 369eb23..9769fea 100644 --- a/src/be/nikiroo/fanfix/library/WebLibrary.java +++ b/src/be/nikiroo/fanfix/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 @@ -104,16 +108,30 @@ public class WebLibrary extends BasicLibrary { this.rw = false; } + public Version getVersion() { + try { + InputStream in = download(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("/"); + download(WebLibraryUrls.INDEX_URL).close(); } catch (IOException e) { try { - download("/style.css"); + download("/style.css").close(); return Status.UNAUTHORIZED; } catch (IOException ioe) { - return Status.INVALID; + return Status.UNAVAILABLE; } } @@ -122,16 +140,29 @@ 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) { + InputStream in = download(WebLibraryUrls.getStoryUrlCover(luid)); + try { return new Image(in); + } finally { + in.close(); } + } + @Override + public Image getCustomSourceCover(final String source) throws IOException { + // TODO maybe global system in BasicLib ? + return null; + } + + @Override + public Image getCustomAuthorCover(final String author) throws IOException { + // TODO maybe global system in BasicLib ? return null; } @@ -147,10 +178,49 @@ public class WebLibrary extends BasicLibrary { throw new IOException("Not implemented yet"); } + @Override + public synchronized Story getStory(final String luid, Progress pg) + throws IOException { + + // TODO: pg + + Story story; + InputStream in = download(WebLibraryUrls.getStoryUrlJson(luid)); + try { + JSONObject json = new JSONObject(IOUtils.readSmallStream(in)); + story = JsonIO.toStory(json); + } finally { + in.close(); + } + + 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 = download( + WebLibraryUrls.getStoryUrl(luid, chapNum, number)); + try { + para.setContentImage(new Image(subin)); + } finally { + subin.close(); + } + } + + number++; + } + + chapNum++; + } + + return story; + } + @Override protected List getMetas(Progress pg) throws IOException { List metas = new ArrayList(); - InputStream in = download("/list/luids"); + InputStream in = download(WebLibraryUrls.LIST_URL_METADATA); JSONArray jsonArr = new JSONArray(IOUtils.readSmallStream(in)); for (int i = 0; i < jsonArr.length(); i++) { JSONObject json = jsonArr.getJSONObject(i); @@ -222,7 +292,7 @@ public class WebLibrary extends BasicLibrary { "Operation not supportorted on remote Libraries"); } - // starts with "/" + // starts with "/", never NULL private InputStream download(String path) throws IOException { URL url = new URL(host + ":" + port + path);