X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FWebLibrary.java;h=42f7ea5ccadd4eb40d6b86cfd1f6842abab0db50;hb=e14f67abf357c8db74aa230faf8922f93f59c7d6;hp=6f2bfda3a9c111e7e70ffa1c4598ed57c716024a;hpb=6673ec5924d57a37c4f995f9c81c50a187b51c63;p=fanfix.git
diff --git a/src/be/nikiroo/fanfix/library/WebLibrary.java b/src/be/nikiroo/fanfix/library/WebLibrary.java
index 6f2bfda..42f7ea5 100644
--- a/src/be/nikiroo/fanfix/library/WebLibrary.java
+++ b/src/be/nikiroo/fanfix/library/WebLibrary.java
@@ -103,14 +103,18 @@ 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 = download(WebLibraryUrls.VERSION_URL);
+ InputStream in = post(WebLibraryUrls.VERSION_URL);
try {
return new Version(IOUtils.readSmallStream(in));
} finally {
@@ -125,10 +129,10 @@ public class WebLibrary extends BasicLibrary {
@Override
public Status getStatus() {
try {
- download(WebLibraryUrls.INDEX_URL).close();
+ post(WebLibraryUrls.INDEX_URL).close();
} catch (IOException e) {
try {
- download("/style.css").close();
+ post("/style.css").close();
return Status.UNAUTHORIZED;
} catch (IOException ioe) {
return Status.UNAVAILABLE;
@@ -146,9 +150,14 @@ public class WebLibrary extends BasicLibrary {
@Override
public Image getCover(String luid) throws IOException {
- InputStream in = download(WebLibraryUrls.getStoryUrlCover(luid));
+ InputStream in = post(WebLibraryUrls.getStoryUrlCover(luid));
try {
- return new Image(in);
+ Image img = new Image(in);
+ if (img.getSize() > 0) {
+ return img;
+ }
+
+ return null;
} finally {
in.close();
}
@@ -156,9 +165,14 @@ public class WebLibrary extends BasicLibrary {
@Override
public Image getCustomSourceCover(String source) throws IOException {
- InputStream in = download(WebLibraryUrls.getCoverUrlSource(source));
+ InputStream in = post(WebLibraryUrls.getCoverUrlSource(source));
try {
- return new Image(in);
+ Image img = new Image(in);
+ if (img.getSize() > 0) {
+ return img;
+ }
+
+ return null;
} finally {
in.close();
}
@@ -166,9 +180,14 @@ public class WebLibrary extends BasicLibrary {
@Override
public Image getCustomAuthorCover(String author) throws IOException {
- InputStream in = download(WebLibraryUrls.getCoverUrlAuthor(author));
+ InputStream in = post(WebLibraryUrls.getCoverUrlAuthor(author));
try {
- return new Image(in);
+ Image img = new Image(in);
+ if (img.getSize() > 0) {
+ return img;
+ }
+
+ return null;
} finally {
in.close();
}
@@ -176,24 +195,27 @@ public class WebLibrary extends BasicLibrary {
@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 {
-
- // TODO: pg
+ if (pg == null) {
+ pg = new Progress();
+ }
Story story;
- InputStream in = download(WebLibraryUrls.getStoryUrlJson(luid));
+ InputStream in = post(WebLibraryUrls.getStoryUrlJson(luid));
try {
JSONObject json = new JSONObject(IOUtils.readSmallStream(in));
story = JsonIO.toStory(json);
@@ -201,34 +223,45 @@ 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) {
int number = 1;
for (Paragraph para : chap) {
if (para.getType() == ParagraphType.IMAGE) {
- InputStream subin = download(
+ 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;
}
@Override
protected List getMetas(Progress pg) throws IOException {
List metas = new ArrayList();
- InputStream in = download(WebLibraryUrls.LIST_URL_METADATA);
+ 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);
@@ -241,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
@@ -252,8 +286,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
@@ -261,8 +336,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
@@ -278,7 +374,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");
}
@@ -301,10 +397,18 @@ public class WebLibrary extends BasicLibrary {
}
// starts with "/", never NULL
- private InputStream download(String path) throws IOException {
+ 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);