X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FWebLibrary.java;h=9af38e2b6bd1c7ccb565ca6f244e31ef42f40746;hp=796533080fba899b60a553f5ac90b80d71d56f58;hb=0a264fbe3d5a43516006052574a5f322d9d38897;hpb=e4b1b70c5388573bda8ae28f9391ee3831e53feb diff --git a/src/be/nikiroo/fanfix/library/WebLibrary.java b/src/be/nikiroo/fanfix/library/WebLibrary.java index 7965330..9af38e2 100644 --- a/src/be/nikiroo/fanfix/library/WebLibrary.java +++ b/src/be/nikiroo/fanfix/library/WebLibrary.java @@ -103,11 +103,15 @@ 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); @@ -122,6 +126,26 @@ public class WebLibrary extends BasicLibrary { return new Version(); } + /** + * Stop the server. + * + * @throws IOException + * in case of I/O errors + */ + public void stop() throws IOException { + try { + post(WebLibraryUrls.EXIT_URL, null).close(); + } catch (Exception e) { + try { + Thread.sleep(200); + } catch (InterruptedException e1) { + } + if (getStatus() != Status.UNAVAILABLE) { + throw new IOException("Cannot exit the library", e); + } + } + } + @Override public Status getStatus() { try { @@ -148,7 +172,13 @@ public class WebLibrary extends BasicLibrary { public Image getCover(String luid) throws IOException { InputStream in = post(WebLibraryUrls.getStoryUrlCover(luid)); try { - return new Image(in); + Image img = new Image(in); + if (img.getSize() > 0) { + img.close(); + return img; + } + + return null; } finally { in.close(); } @@ -158,7 +188,13 @@ public class WebLibrary extends BasicLibrary { public Image getCustomSourceCover(String source) throws IOException { InputStream in = post(WebLibraryUrls.getCoverUrlSource(source)); try { - return new Image(in); + Image img = new Image(in); + if (img.getSize() > 0) { + img.close(); + return img; + } + + return null; } finally { in.close(); } @@ -168,7 +204,13 @@ public class WebLibrary extends BasicLibrary { public Image getCustomAuthorCover(String author) throws IOException { InputStream in = post(WebLibraryUrls.getCoverUrlAuthor(author)); try { - return new Image(in); + Image img = new Image(in); + if (img.getSize() > 0) { + img.close(); + return img; + } + + return null; } finally { in.close(); } @@ -191,8 +233,9 @@ public class WebLibrary extends BasicLibrary { @Override public synchronized Story getStory(final String luid, Progress pg) throws IOException { - - // TODO: pg + if (pg == null) { + pg = new Progress(); + } Story story; InputStream in = post(WebLibraryUrls.getStoryUrlJson(luid)); @@ -203,6 +246,12 @@ public class WebLibrary extends BasicLibrary { 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) { @@ -212,18 +261,23 @@ public class WebLibrary extends BasicLibrary { InputStream subin = post( WebLibraryUrls.getStoryUrl(luid, chapNum, number)); try { - para.setContentImage(new Image(subin)); + Image img = new Image(subin); + if (img.getSize() > 0) { + para.setContentImage(img); + } } finally { subin.close(); } } + pg.add(1); number++; } chapNum++; } + pg.done(); return story; } @@ -243,8 +297,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 @@ -254,8 +309,49 @@ 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))); + pg.setName(subPg.getName()); + pg.setMinMax(subPg.getMin(), subPg.getMax()); + pg.setProgress(subPg.getProgress()); + } 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 @@ -263,8 +359,29 @@ 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 + public synchronized void delete(String luid) throws IOException { + post(WebLibraryUrls.getDeleteUrlStory(luid), null).close(); } @Override @@ -280,7 +397,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"); }