use of com.ichi2.libanki.sync.BasicHttpSyncer in project Anki-Android by Ramblurr.
the class Connection method doInBackgroundUpgradeDecks.
private Payload doInBackgroundUpgradeDecks(Payload data) {
// Enable http request canceller
mCancelCallback = new CancelCallback();
String path = (String) data.data[0];
File ankiDir = new File(path);
if (!ankiDir.isDirectory()) {
data.success = false;
data.data = new Object[] { "wrong anki directory" };
return data;
}
// step 1: gather all .anki files into a zip, without media.
// we must store them as 1.anki, 2.anki and provide a map so we don't run into
// encoding issues with the zip file.
File[] fileList = ankiDir.listFiles(new OldAnkiDeckFilter());
List<String> corruptFiles = new ArrayList<String>();
JSONObject map = new JSONObject();
byte[] buf = new byte[1024];
String zipFilename = path + "/upload.zip";
String colFilename = path + AnkiDroidApp.COLLECTION_PATH;
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilename));
int n = 1;
for (File f : fileList) {
String deckPath = f.getAbsolutePath();
// set journal mode to delete
try {
AnkiDb d = AnkiDatabaseManager.getDatabase(deckPath);
} catch (SQLiteDatabaseCorruptException e) {
// ignore invalid .anki files
corruptFiles.add(f.getName());
continue;
} finally {
AnkiDatabaseManager.closeDatabase(deckPath);
}
// zip file
String tmpName = n + ".anki";
FileInputStream in = new FileInputStream(deckPath);
ZipEntry ze = new ZipEntry(tmpName);
zos.putNextEntry(ze);
int len;
while ((len = in.read(buf)) >= 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
map.put(tmpName, f.getName());
n++;
}
// if all .anki files were found corrupted, abort
if (fileList.length == corruptFiles.size()) {
data.success = false;
data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
return data;
}
ZipEntry ze = new ZipEntry("map.json");
zos.putNextEntry(ze);
InputStream in = new ByteArrayInputStream(Utils.jsonToString(map).getBytes("UTF-8"));
int len;
while ((len = in.read(buf)) >= 0) {
zos.write(buf, 0, len);
}
zos.closeEntry();
zos.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (JSONException e) {
throw new RuntimeException(e);
}
File zipFile = new File(zipFilename);
// step 1.1: if it's over 50MB compressed, it must be upgraded by the user
if (zipFile.length() > 50 * 1024 * 1024) {
data.success = false;
data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_exceeds) };
return data;
}
// step 2: upload zip file to upgrade service and get token
BasicHttpSyncer h = new BasicHttpSyncer(null, null);
// note: server doesn't expect it to be gzip compressed, because the zip file is compressed
// enable cancelling
publishProgress(R.string.upgrade_decks_upload, null, true);
try {
HttpResponse resp = h.req("upgrade/upload", new FileInputStream(zipFile), 0, false, null, mCancelCallback);
if (resp == null && !isCancelled()) {
data.success = false;
data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
return data;
}
String result;
String key = null;
if (!isCancelled()) {
result = h.stream2String(resp.getEntity().getContent());
if (result != null && result.startsWith("ok:")) {
key = result.split(":")[1];
} else {
data.success = false;
data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
return data;
}
}
while (!isCancelled()) {
result = h.stream2String(h.req("upgrade/status?key=" + key).getEntity().getContent());
if (result.equals("error")) {
data.success = false;
data.data = new Object[] { "error" };
return data;
} else if (result.startsWith("waiting:")) {
publishProgress(R.string.upgrade_decks_upload, result.split(":")[1]);
} else if (result.equals("upgrading")) {
publishProgress(new Object[] { R.string.upgrade_decks_upgrade_started });
} else if (result.equals("ready")) {
break;
} else {
data.success = false;
data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
return data;
}
Thread.sleep(1000);
}
// gzip compression if the client says it can handle it
if (!isCancelled()) {
publishProgress(new Object[] { R.string.upgrade_decks_downloading });
resp = h.req("upgrade/download?key=" + key, null, 6, true, null, mCancelCallback);
// uploads/downloads have finished so disable cancelling
}
publishProgress(R.string.upgrade_decks_downloading, null, false);
if (isCancelled()) {
return null;
}
if (resp == null) {
data.success = false;
data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_failed) };
return data;
}
// step 5: check the received file is valid
InputStream cont = resp.getEntity().getContent();
if (!h.writeToFile(cont, colFilename)) {
data.success = false;
data.data = new Object[] { sContext.getString(R.string.upgrade_deck_web_upgrade_sdcard, new File(colFilename).length() / 1048576 + 1) };
(new File(colFilename)).delete();
return data;
}
// check the received file is ok
publishProgress(new Object[] { R.string.sync_check_download_file });
publishProgress(R.string.sync_check_download_file);
try {
AnkiDb d = AnkiDatabaseManager.getDatabase(colFilename);
if (!d.queryString("PRAGMA integrity_check").equalsIgnoreCase("ok")) {
data.success = false;
data.data = new Object[] { sContext.getResources() };
return data;
}
} finally {
AnkiDatabaseManager.closeDatabase(colFilename);
}
Collection col = AnkiDroidApp.openCollection(colFilename);
ArrayList<String> decks = col.getDecks().allNames(false);
ArrayList<String> failed = new ArrayList<String>();
ArrayList<File> mediaDirs = new ArrayList<File>();
for (File f : fileList) {
String name = f.getName().replaceFirst("\\.anki$", "");
if (!decks.contains(name)) {
failed.add(name);
} else {
mediaDirs.add(new File(f.getAbsolutePath().replaceFirst("\\.anki$", ".media")));
}
}
File newMediaDir = new File(col.getMedia().getDir());
// step 6. move media files to new media directory
publishProgress(new Object[] { R.string.upgrade_decks_media });
ArrayList<String> failedMedia = new ArrayList<String>();
File curMediaDir = null;
for (File mediaDir : mediaDirs) {
curMediaDir = mediaDir;
// Check if media directory exists and is local
if (!curMediaDir.exists() || !curMediaDir.isDirectory()) {
// If not try finding it in dropbox 1.2.x
curMediaDir = new File(AnkiDroidApp.getDropboxDir(), mediaDir.getName());
if (!curMediaDir.exists() || !curMediaDir.isDirectory()) {
// No media for this deck
continue;
}
}
// Found media dir, copy files
for (File m : curMediaDir.listFiles()) {
try {
Utils.copyFile(m, new File(newMediaDir, m.getName()));
} catch (IOException e) {
failedMedia.add(curMediaDir.getName().replaceFirst("\\.media$", ".anki"));
break;
}
}
}
data.data = new Object[] { failed, failedMedia, newMediaDir.getAbsolutePath() };
data.success = true;
return data;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (IllegalStateException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
(new File(zipFilename)).delete();
}
}
use of com.ichi2.libanki.sync.BasicHttpSyncer in project Anki-Android by Ramblurr.
the class Connection method doInBackgroundLogin.
private Payload doInBackgroundLogin(Payload data) {
String username = (String) data.data[0];
String password = (String) data.data[1];
BasicHttpSyncer server = new RemoteServer(this, null);
HttpResponse ret = server.hostKey(username, password);
String hostkey = null;
boolean valid = false;
if (ret != null) {
data.returnType = ret.getStatusLine().getStatusCode();
Log.i(AnkiDroidApp.TAG, "doInBackgroundLogin - response from server: " + data.returnType + " (" + ret.getStatusLine().getReasonPhrase() + ")");
if (data.returnType == 200) {
try {
JSONObject jo = (new JSONObject(server.stream2String(ret.getEntity().getContent())));
hostkey = jo.getString("key");
valid = (hostkey != null) && (hostkey.length() > 0);
} catch (JSONException e) {
valid = false;
} catch (IllegalStateException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} else {
Log.e(AnkiDroidApp.TAG, "doInBackgroundLogin - empty response from server");
}
if (valid) {
data.success = true;
data.data = new String[] { username, hostkey };
} else {
data.success = false;
}
return data;
}
use of com.ichi2.libanki.sync.BasicHttpSyncer in project Anki-Android by Ramblurr.
the class Connection method doInBackgroundSync.
private Payload doInBackgroundSync(Payload data) {
// for for doInBackgroundLoadDeckCounts if any
DeckTask.waitToFinish();
String hkey = (String) data.data[0];
boolean media = (Boolean) data.data[1];
String conflictResolution = (String) data.data[2];
int mediaUsn = (Integer) data.data[3];
boolean colCorruptFullSync = false;
Collection col = AnkiDroidApp.getCol();
if (!AnkiDroidApp.colIsOpen()) {
if (conflictResolution != null && conflictResolution.equals("download")) {
colCorruptFullSync = true;
} else {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
}
String path = AnkiDroidApp.getCollectionPath();
BasicHttpSyncer server = new RemoteServer(this, hkey);
Syncer client = new Syncer(col, server);
// run sync and check state
boolean noChanges = false;
if (conflictResolution == null) {
Log.i(AnkiDroidApp.TAG, "Sync - starting sync");
publishProgress(R.string.sync_prepare_syncing);
Object[] ret = client.sync(this);
data.message = client.getSyncMsg();
mediaUsn = client.getmMediaUsn();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
String retCode = (String) ret[0];
if (!retCode.equals("noChanges") && !retCode.equals("success")) {
data.success = false;
data.result = ret;
// note mediaUSN for later
data.data = new Object[] { mediaUsn };
return data;
}
// save and note success state
if (retCode.equals("noChanges")) {
// publishProgress(R.string.sync_no_changes_message);
noChanges = true;
} else {
// publishProgress(R.string.sync_database_success);
}
} else {
try {
server = new FullSyncer(col, hkey, this);
if (conflictResolution.equals("upload")) {
Log.i(AnkiDroidApp.TAG, "Sync - fullsync - upload collection");
publishProgress(R.string.sync_preparing_full_sync_message);
Object[] ret = server.upload();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
AnkiDroidApp.openCollection(path);
return data;
}
if (!((String) ret[0]).equals(BasicHttpSyncer.ANKIWEB_STATUS_OK)) {
data.success = false;
data.result = ret;
AnkiDroidApp.openCollection(path);
return data;
}
} else if (conflictResolution.equals("download")) {
Log.i(AnkiDroidApp.TAG, "Sync - fullsync - download collection");
publishProgress(R.string.sync_downloading_message);
Object[] ret = server.download();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
AnkiDroidApp.openCollection(path);
return data;
}
if (!((String) ret[0]).equals("success")) {
data.success = false;
data.result = ret;
if (!colCorruptFullSync) {
AnkiDroidApp.openCollection(path);
}
return data;
}
}
col = AnkiDroidApp.openCollection(path);
} catch (OutOfMemoryError e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-fullSync");
data.success = false;
data.result = new Object[] { "OutOfMemoryError" };
data.data = new Object[] { mediaUsn };
return data;
} catch (RuntimeException e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-fullSync");
data.success = false;
data.result = new Object[] { "IOException" };
data.data = new Object[] { mediaUsn };
return data;
}
}
// clear undo to avoid non syncing orphans (because undo resets usn too
if (!noChanges) {
col.clearUndo();
}
// then move on to media sync
boolean noMediaChanges = false;
String mediaError = null;
if (media) {
server = new RemoteMediaServer(hkey, this);
MediaSyncer mediaClient = new MediaSyncer(col, (RemoteMediaServer) server);
String ret;
try {
ret = mediaClient.sync(mediaUsn, this);
if (ret == null) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error);
} else {
if (ret.equals("noChanges")) {
publishProgress(R.string.sync_media_no_changes);
noMediaChanges = true;
}
if (ret.equals("sanityFailed")) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_sanity_failed);
} else {
publishProgress(R.string.sync_media_success);
}
}
} catch (RuntimeException e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-mediaSync");
mediaError = e.getLocalizedMessage();
}
}
if (noChanges && noMediaChanges) {
data.success = false;
data.result = new Object[] { "noChanges" };
return data;
} else {
data.success = true;
TreeSet<Object[]> decks = col.getSched().deckDueTree();
int[] counts = new int[] { 0, 0, 0 };
for (Object[] deck : decks) {
if (((String[]) deck[0]).length == 1) {
counts[0] += (Integer) deck[2];
counts[1] += (Integer) deck[3];
counts[2] += (Integer) deck[4];
}
}
Object[] dc = col.getSched().deckCounts();
data.result = dc[0];
data.data = new Object[] { conflictResolution, col, dc[1], dc[2], mediaError };
return data;
}
}
use of com.ichi2.libanki.sync.BasicHttpSyncer in project Anki-Android by Ramblurr.
the class Connection method doInBackgroundRegister.
private Payload doInBackgroundRegister(Payload data) {
String username = (String) data.data[0];
String password = (String) data.data[1];
BasicHttpSyncer server = new RemoteServer(this, null);
HttpResponse ret = server.register(username, password);
String hostkey = null;
boolean valid = false;
String status = null;
if (ret != null) {
data.returnType = ret.getStatusLine().getStatusCode();
if (data.returnType == 200) {
try {
JSONObject jo = (new JSONObject(server.stream2String(ret.getEntity().getContent())));
status = jo.getString("status");
if (status.equals("ok")) {
hostkey = jo.getString("hkey");
valid = (hostkey != null) && (hostkey.length() > 0);
}
} catch (JSONException e) {
} catch (IllegalStateException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
if (valid) {
data.success = true;
data.data = new String[] { username, hostkey };
} else {
data.success = false;
data.data = new String[] { status != null ? status : AnkiDroidApp.getAppResources().getString(R.string.connection_error_message) };
}
return data;
}
Aggregations