mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 10:49:24 +02:00
synchronisation: file upload
This commit is contained in:
parent
d5021fbfe1
commit
2f7d94d21e
4 changed files with 80 additions and 41 deletions
|
@ -28,6 +28,7 @@ import android.os.IBinder;
|
||||||
|
|
||||||
import org.json.simple.JSONValue;
|
import org.json.simple.JSONValue;
|
||||||
|
|
||||||
|
import org.geometerplus.zlibrary.core.filesystem.ZLPhysicalFile;
|
||||||
import org.geometerplus.zlibrary.core.network.*;
|
import org.geometerplus.zlibrary.core.network.*;
|
||||||
import org.geometerplus.zlibrary.ui.android.network.SQLiteCookieDatabase;
|
import org.geometerplus.zlibrary.ui.android.network.SQLiteCookieDatabase;
|
||||||
import org.geometerplus.fbreader.book.*;
|
import org.geometerplus.fbreader.book.*;
|
||||||
|
@ -84,7 +85,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
}
|
}
|
||||||
myProcessed.add(book);
|
myProcessed.add(book);
|
||||||
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]");
|
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]");
|
||||||
uploadBookToServer(book);
|
uploadBookToServer(book.File.getPhysicalFile());
|
||||||
}
|
}
|
||||||
System.err.println("BYE-BYE THREAD");
|
System.err.println("BYE-BYE THREAD");
|
||||||
ourSynchronizationThread = null;
|
ourSynchronizationThread = null;
|
||||||
|
@ -105,9 +106,10 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
return writer.toString();
|
return writer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static abstract class JsonRequest extends ZLNetworkRequest.PostWithBody {
|
private final static String DOMAIN = "demo.fbreader.org";
|
||||||
private final static String BASE_URL = "https://demo.fbreader.org/app/";
|
private final static String BASE_URL = "https://" + DOMAIN + "/app/";
|
||||||
|
|
||||||
|
private static abstract class JsonRequest extends ZLNetworkRequest.PostWithBody {
|
||||||
JsonRequest(String app, Object data) {
|
JsonRequest(String app, Object data) {
|
||||||
super(BASE_URL + app, toJSON(data), false);
|
super(BASE_URL + app, toJSON(data), false);
|
||||||
}
|
}
|
||||||
|
@ -126,18 +128,61 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
protected abstract void processResponse(Object response);
|
protected abstract void processResponse(Object response);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void uploadBookToServer(Book book) {
|
private static final class UploadRequest extends ZLNetworkRequest.FileUpload {
|
||||||
final UID uid = BookUtil.createUid(book.File.getPhysicalFile(), "SHA-1");
|
UploadRequest(File file) {
|
||||||
|
super(BASE_URL + "book.upload", file, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleStream(InputStream stream, int length) throws IOException, ZLNetworkException {
|
||||||
|
final BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
||||||
|
final StringBuilder buffer = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
buffer.append(line);
|
||||||
|
}
|
||||||
|
final Object response = JSONValue.parse(buffer.toString());
|
||||||
|
if (response instanceof Map && ((Map)response).isEmpty()) {
|
||||||
|
System.err.println("UPLOADED SUCCESSFULLY");
|
||||||
|
} else {
|
||||||
|
System.err.println("UPLOAD FAILURE: " + response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void uploadBookToServer(ZLPhysicalFile file) {
|
||||||
|
final UID uid = BookUtil.createUid(file, "SHA-1");
|
||||||
if (uid == null) {
|
if (uid == null) {
|
||||||
System.err.println("Failed: SHA-1 checksum not computed");
|
System.err.println("Failed: SHA-1 checksum not computed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.err.println("SHA-1: " + uid.Id);
|
final Map<String,Object> result = new HashMap<String,Object>();
|
||||||
myNetworkContext.performQuietly(new JsonRequest("books.by.hash", Collections.singletonMap("sha1", uid.Id)) {
|
final JsonRequest verificationRequest =
|
||||||
|
new JsonRequest("books.by.hash", Collections.singletonMap("sha1", uid.Id)) {
|
||||||
|
@Override
|
||||||
public void processResponse(Object response) {
|
public void processResponse(Object response) {
|
||||||
System.err.println("RESPONSE = " + response);
|
result.put("result", response);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
myNetworkContext.performQuietly(verificationRequest);
|
||||||
|
final String csrfToken = myNetworkContext.getCookieValue(DOMAIN, "csrftoken");
|
||||||
|
final Object response = result.get("result");
|
||||||
|
if (response == null) {
|
||||||
|
System.err.println("UNEXPECTED ERROR: NO RESPONSE");
|
||||||
|
} else if (!(response instanceof List)) {
|
||||||
|
System.err.println("UNEXPECTED RESPONSE: " + response);
|
||||||
|
} else if (!((List)response).isEmpty()) {
|
||||||
|
System.err.println("BOOK ALREADY UPLOADED");
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
final UploadRequest uploadRequest = new UploadRequest(file.javaFile());
|
||||||
|
uploadRequest.addHeader("Referer", verificationRequest.getURL());
|
||||||
|
uploadRequest.addHeader("X-CSRFToken", csrfToken);
|
||||||
|
myNetworkContext.perform(uploadRequest);
|
||||||
|
} catch (ZLNetworkException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -25,6 +25,7 @@ import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.http.client.CookieStore;
|
import org.apache.http.client.CookieStore;
|
||||||
|
import org.apache.http.cookie.Cookie;
|
||||||
|
|
||||||
public abstract class ZLNetworkContext implements ZLNetworkManager.BearerAuthenticator {
|
public abstract class ZLNetworkContext implements ZLNetworkManager.BearerAuthenticator {
|
||||||
private final ZLNetworkManager myManager = ZLNetworkManager.Instance();
|
private final ZLNetworkManager myManager = ZLNetworkManager.Instance();
|
||||||
|
@ -36,6 +37,15 @@ public abstract class ZLNetworkContext implements ZLNetworkManager.BearerAuthent
|
||||||
return myManager.CookieStore;
|
return myManager.CookieStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getCookieValue(String domain, String name) {
|
||||||
|
for (Cookie c : cookieStore().getCookies()) {
|
||||||
|
if (domain.equals(c.getDomain()) && name.equals(c.getName())) {
|
||||||
|
return c.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean performQuietly(ZLNetworkRequest request) {
|
public boolean performQuietly(ZLNetworkRequest request) {
|
||||||
try {
|
try {
|
||||||
perform(request);
|
perform(request);
|
||||||
|
|
|
@ -35,14 +35,13 @@ import org.apache.http.cookie.Cookie;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||||
import org.apache.http.entity.mime.MultipartEntity;
|
import org.apache.http.entity.mime.MultipartEntity;
|
||||||
import org.apache.http.entity.mime.content.InputStreamBody;
|
import org.apache.http.entity.mime.content.FileBody;
|
||||||
import org.apache.http.impl.client.*;
|
import org.apache.http.impl.client.*;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
import org.apache.http.params.*;
|
import org.apache.http.params.*;
|
||||||
import org.apache.http.protocol.HttpContext;
|
import org.apache.http.protocol.HttpContext;
|
||||||
import org.apache.http.protocol.BasicHttpContext;
|
import org.apache.http.protocol.BasicHttpContext;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
|
||||||
import org.geometerplus.zlibrary.core.options.ZLStringOption;
|
import org.geometerplus.zlibrary.core.options.ZLStringOption;
|
||||||
import org.geometerplus.zlibrary.core.util.MiscUtil;
|
import org.geometerplus.zlibrary.core.util.MiscUtil;
|
||||||
import org.geometerplus.zlibrary.core.util.ZLNetworkUtil;
|
import org.geometerplus.zlibrary.core.util.ZLNetworkUtil;
|
||||||
|
@ -346,10 +345,11 @@ public class ZLNetworkManager {
|
||||||
}
|
}
|
||||||
((HttpPost)httpRequest).setEntity(new UrlEncodedFormEntity(list, "utf-8"));
|
((HttpPost)httpRequest).setEntity(new UrlEncodedFormEntity(list, "utf-8"));
|
||||||
} else if (request instanceof ZLNetworkRequest.FileUpload) {
|
} else if (request instanceof ZLNetworkRequest.FileUpload) {
|
||||||
final ZLFile file = ((ZLNetworkRequest.FileUpload)request).File;
|
final ZLNetworkRequest.FileUpload uploadRequest = (ZLNetworkRequest.FileUpload)request;
|
||||||
|
final File file = ((ZLNetworkRequest.FileUpload)request).File;
|
||||||
httpRequest = new HttpPost(request.URL);
|
httpRequest = new HttpPost(request.URL);
|
||||||
final MultipartEntity data = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
|
final MultipartEntity data = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||||||
data.addPart("file", new InputStreamBody(file.getInputStream(), file.getPath()));
|
data.addPart("file", new FileBody(uploadRequest.File));
|
||||||
((HttpPost)httpRequest).setEntity(data);
|
((HttpPost)httpRequest).setEntity(data);
|
||||||
} else {
|
} else {
|
||||||
throw new ZLNetworkException(true, "Unknown request type");
|
throw new ZLNetworkException(true, "Unknown request type");
|
||||||
|
@ -364,13 +364,8 @@ public class ZLNetworkManager {
|
||||||
httpRequest.setHeader(header.getKey(), header.getValue());
|
httpRequest.setHeader(header.getKey(), header.getValue());
|
||||||
}
|
}
|
||||||
httpClient.setCredentialsProvider(new MyCredentialsProvider(httpRequest, request.isQuiet()));
|
httpClient.setCredentialsProvider(new MyCredentialsProvider(httpRequest, request.isQuiet()));
|
||||||
HttpResponse response = null;
|
final HttpResponse response = execute(httpClient, httpRequest, httpContext, authenticator);
|
||||||
IOException lastException = null;
|
|
||||||
for (int retryCounter = 0; retryCounter < 3 && entity == null; ++retryCounter) {
|
|
||||||
try {
|
|
||||||
response = execute(httpClient, httpRequest, httpContext, authenticator);
|
|
||||||
entity = response.getEntity();
|
entity = response.getEntity();
|
||||||
lastException = null;
|
|
||||||
if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
|
if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
|
||||||
final AuthState state = (AuthState)httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
|
final AuthState state = (AuthState)httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
|
||||||
if (state != null) {
|
if (state != null) {
|
||||||
|
@ -380,14 +375,6 @@ public class ZLNetworkManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
lastException = e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (lastException != null) {
|
|
||||||
throw lastException;
|
|
||||||
}
|
|
||||||
final int responseCode = response.getStatusLine().getStatusCode();
|
final int responseCode = response.getStatusLine().getStatusCode();
|
||||||
|
|
||||||
InputStream stream = null;
|
InputStream stream = null;
|
||||||
|
|
|
@ -19,13 +19,10 @@
|
||||||
|
|
||||||
package org.geometerplus.zlibrary.core.network;
|
package org.geometerplus.zlibrary.core.network;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
|
||||||
|
|
||||||
public abstract class ZLNetworkRequest {
|
public abstract class ZLNetworkRequest {
|
||||||
public static abstract class Get extends ZLNetworkRequest {
|
public static abstract class Get extends ZLNetworkRequest {
|
||||||
protected Get(String url) {
|
protected Get(String url) {
|
||||||
|
@ -63,9 +60,9 @@ public abstract class ZLNetworkRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static abstract class FileUpload extends ZLNetworkRequest {
|
public static abstract class FileUpload extends ZLNetworkRequest {
|
||||||
public final ZLFile File;
|
final File File;
|
||||||
|
|
||||||
protected FileUpload(String url, ZLFile file, boolean quiet) {
|
protected FileUpload(String url, File file, boolean quiet) {
|
||||||
super(url, quiet);
|
super(url, quiet);
|
||||||
File = file;
|
File = file;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue