Search in sources :

Example 16 with UnknownHttpResponseException

use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.

the class MediaSyncer method _downloadFiles.

private void _downloadFiles(List<String> fnames) {
    mCol.log(fnames.size() + " files to fetch");
    while (fnames.size() > 0) {
        try {
            List<String> top = fnames.subList(0, Math.min(fnames.size(), Consts.SYNC_ZIP_COUNT));
            mCol.log("fetch " + top);
            ZipFile zipData = mServer.downloadFiles(top);
            int cnt = mCol.getMedia().addFilesFromZip(zipData);
            mDownloadCount += cnt;
            mCol.log("received " + cnt + " files");
            // if we've reached the end and clear the fnames list manually.
            if (cnt == fnames.size()) {
                fnames.clear();
            } else {
                fnames = fnames.subList(cnt, fnames.size());
            }
            mCon.publishProgress(String.format(AnkiDroidApp.getAppResources().getString(R.string.sync_media_downloaded_count), mDownloadCount));
        } catch (IOException | UnknownHttpResponseException e) {
            Timber.e(e, "Error downloading media files");
            throw new RuntimeException(e);
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException)

Example 17 with UnknownHttpResponseException

use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.

the class RemoteMediaServer method mediaChanges.

// args: lastUsn
public JSONArray mediaChanges(int lastUsn) throws UnknownHttpResponseException, MediaSyncException {
    try {
        mPostVars = new HashMap<>();
        mPostVars.put("sk", mSKey);
        Response resp = super.req("mediaChanges", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("lastUsn", lastUsn))));
        JSONObject jresp = new JSONObject(resp.body().string());
        return _dataOnly(jresp, JSONArray.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException)

Example 18 with UnknownHttpResponseException

use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.

the class RemoteMediaServer method downloadFiles.

/**
 * args: files
 * <br>
 * This method returns a ZipFile with the OPEN_DELETE flag, ensuring that the file on disk will
 * be automatically deleted when the stream is closed.
 */
public ZipFile downloadFiles(List<String> top) throws UnknownHttpResponseException {
    Response resp = null;
    try {
        resp = super.req("downloadFiles", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("files", new JSONArray(top)))));
        String zipPath = mCol.getPath().replaceFirst("collection\\.anki2$", "tmpSyncFromServer.zip");
        // retrieve contents and save to file on disk:
        super.writeToFile(resp.body().byteStream(), zipPath);
        return new ZipFile(new File(zipPath), ZipFile.OPEN_READ | ZipFile.OPEN_DELETE);
    } catch (IOException | NullPointerException e) {
        Timber.e(e, "Failed to download requested media files");
        throw new RuntimeException(e);
    } finally {
        if (resp != null && resp.body() != null) {
            resp.body().close();
        }
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) ZipFile(java.util.zip.ZipFile) JSONArray(com.ichi2.utils.JSONArray) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 19 with UnknownHttpResponseException

use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.

the class RemoteMediaServer method begin.

public JSONObject begin() throws UnknownHttpResponseException, MediaSyncException {
    try {
        mPostVars = new HashMap<>();
        mPostVars.put("k", mHKey);
        mPostVars.put("v", String.format(Locale.US, "anki,%s,%s", VersionUtils.getPkgVersionNameFake(), Utils.platDesc()));
        Response resp = super.req("begin", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject())));
        JSONObject jresp = new JSONObject(resp.body().string());
        JSONObject ret = _dataOnly(jresp, JSONObject.class);
        mSKey = ret.getString("sk");
        return ret;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException)

Example 20 with UnknownHttpResponseException

use of com.ichi2.anki.exception.UnknownHttpResponseException in project AnkiChinaAndroid by ankichinateam.

the class RemoteServer method hostKey.

/**
 * Returns hkey or null if user/pw incorrect.
 */
@Override
public Response hostKey(String user, String pw) throws UnknownHttpResponseException {
    try {
        mPostVars = new HashMap<>();
        JSONObject credentials = new JSONObject();
        credentials.put("u", user);
        credentials.put("p", pw);
        return super.req("hostKey", HttpSyncer.getInputStream(Utils.jsonToString(credentials)));
    } catch (JSONException e) {
        return null;
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONException(com.ichi2.utils.JSONException)

Aggregations

IOException (java.io.IOException)23 JSONObject (com.ichi2.utils.JSONObject)22 Response (okhttp3.Response)20 UnknownHttpResponseException (com.ichi2.anki.exception.UnknownHttpResponseException)8 NoEnoughServerSpaceException (com.ichi2.anki.exception.NoEnoughServerSpaceException)7 File (java.io.File)7 JSONException (com.ichi2.utils.JSONException)6 FileInputStream (java.io.FileInputStream)6 MediaSyncException (com.ichi2.anki.exception.MediaSyncException)5 HostNum (com.ichi2.libanki.sync.HostNum)5 RemoteServer (com.ichi2.libanki.sync.RemoteServer)5 HttpSyncer (com.ichi2.libanki.sync.HttpSyncer)4 ZipFile (java.util.zip.ZipFile)4 JSONArray (com.ichi2.utils.JSONArray)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 SharedPreferences (android.content.SharedPreferences)2 SQLiteDatabaseCorruptException (android.database.sqlite.SQLiteDatabaseCorruptException)2 Uri (android.net.Uri)2 Pair (android.util.Pair)2