throw Error on 404 response
This commit is contained in:
parent
2ed90fecc6
commit
1418eb8f18
@ -67,6 +67,9 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
|||||||
Request request = new Request.Builder().url(url)
|
Request request = new Request.Builder().url(url)
|
||||||
.build();
|
.build();
|
||||||
Response response = client.newCall(request).execute();
|
Response response = client.newCall(request).execute();
|
||||||
|
if (response.code() > 299) {
|
||||||
|
throw new Error("Server return code " + response.code());
|
||||||
|
}
|
||||||
ResponseBody body = response.body();
|
ResponseBody body = response.body();
|
||||||
long contentLength = body.contentLength();
|
long contentLength = body.contentLength();
|
||||||
BufferedSource source = body.source();
|
BufferedSource source = body.source();
|
||||||
@ -118,6 +121,21 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
|||||||
zis.closeEntry();
|
zis.closeEntry();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void copyFile(File from, File fmd) throws IOException {
|
||||||
|
int count;
|
||||||
|
|
||||||
|
InputStream in = new FileInputStream(from);
|
||||||
|
FileOutputStream fout = new FileOutputStream(fmd);
|
||||||
|
|
||||||
|
while ((count = in.read(buffer)) != -1)
|
||||||
|
{
|
||||||
|
fout.write(buffer, 0, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
fout.close();
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
|
||||||
private byte[] readBytes(ZipInputStream zis) throws IOException {
|
private byte[] readBytes(ZipInputStream zis) throws IOException {
|
||||||
int count;
|
int count;
|
||||||
|
|
||||||
@ -147,6 +165,48 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
|||||||
return fout.toByteArray();
|
return fout.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private byte[] readFile(File file) throws IOException {
|
||||||
|
InputStream in = new FileInputStream(file);
|
||||||
|
int count;
|
||||||
|
|
||||||
|
ByteArrayOutputStream fout = new ByteArrayOutputStream();
|
||||||
|
while ((count = in.read(buffer)) != -1)
|
||||||
|
{
|
||||||
|
fout.write(buffer, 0, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
fout.close();
|
||||||
|
in.close();
|
||||||
|
return fout.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyFilesWithBlacklist(String current, File from, File to, JSONObject blackList) throws IOException {
|
||||||
|
File[] files = from.listFiles();
|
||||||
|
for (File file : files) {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
String subName = current + file.getName() + '/';
|
||||||
|
if (blackList.has(subName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
File toFile = new File(to, file.getName());
|
||||||
|
if (!toFile.exists()) {
|
||||||
|
toFile.mkdir();
|
||||||
|
}
|
||||||
|
copyFilesWithBlacklist(subName, file, toFile, blackList);
|
||||||
|
} else if (!blackList.has(current + file.getName())) {
|
||||||
|
// Copy file.
|
||||||
|
File toFile = new File(to, file.getName());
|
||||||
|
if (!toFile.exists()) {
|
||||||
|
copyFile(file, toFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyFilesWithBlacklist(File from, File to, JSONObject blackList) throws IOException {
|
||||||
|
copyFilesWithBlacklist("", from, to, blackList);
|
||||||
|
}
|
||||||
|
|
||||||
private void doDownload(DownloadTaskParams param) {
|
private void doDownload(DownloadTaskParams param) {
|
||||||
try {
|
try {
|
||||||
downloadFile(param.url, param.zipFilePath);
|
downloadFile(param.url, param.zipFilePath);
|
||||||
@ -274,6 +334,79 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void doPatchFromPpk(DownloadTaskParams param) {
|
||||||
|
try {
|
||||||
|
downloadFile(param.url, param.zipFilePath);
|
||||||
|
|
||||||
|
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(param.zipFilePath)));
|
||||||
|
ZipEntry ze;
|
||||||
|
int count;
|
||||||
|
String filename;
|
||||||
|
|
||||||
|
removeDirectory(param.unzipDirectory);
|
||||||
|
param.unzipDirectory.mkdirs();
|
||||||
|
|
||||||
|
while ((ze = zis.getNextEntry()) != null)
|
||||||
|
{
|
||||||
|
String fn = ze.getName();
|
||||||
|
|
||||||
|
if (fn.equals("__diff.json")) {
|
||||||
|
// copy files from assets
|
||||||
|
byte[] bytes = readBytes(zis);
|
||||||
|
String json = new String(bytes, "UTF-8");
|
||||||
|
JSONObject obj = (JSONObject)new JSONTokener(json).nextValue();
|
||||||
|
|
||||||
|
JSONObject copies = obj.getJSONObject("copies");
|
||||||
|
Iterator<?> keys = copies.keys();
|
||||||
|
while( keys.hasNext() ) {
|
||||||
|
String to = (String)keys.next();
|
||||||
|
String from = copies.getString(to);
|
||||||
|
if (from.isEmpty()) {
|
||||||
|
from = to;
|
||||||
|
}
|
||||||
|
copyFile(new File(param.originDirectory, from), new File(param.unzipDirectory, to));
|
||||||
|
}
|
||||||
|
JSONObject blackList = obj.getJSONObject("deletes");
|
||||||
|
copyFilesWithBlacklist(param.originDirectory, param.unzipDirectory, blackList);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (fn.equals("index.bundlejs.patch")) {
|
||||||
|
// do bsdiff patch
|
||||||
|
byte[] patched = bsdiffPatch(readFile(new File(param.originDirectory, "index.bundlejs")), readBytes(zis));
|
||||||
|
|
||||||
|
FileOutputStream fout = new FileOutputStream(new File(param.unzipDirectory, "index.bundlejs"));
|
||||||
|
fout.write(patched);
|
||||||
|
fout.close();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
File fmd = new File(param.unzipDirectory, fn);
|
||||||
|
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("RNUpdate", "Unzipping " + fn);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ze.isDirectory()) {
|
||||||
|
fmd.mkdirs();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
unzipToFile(zis, fmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
zis.close();
|
||||||
|
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
Log.d("RNUpdate", "Unzip finished");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Throwable e) {
|
||||||
|
if (UpdateContext.DEBUG) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
param.listener.onDownloadFailed(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(DownloadTaskParams... params) {
|
protected Void doInBackground(DownloadTaskParams... params) {
|
||||||
switch (params[0].type) {
|
switch (params[0].type) {
|
||||||
@ -283,6 +416,9 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
|||||||
case DownloadTaskParams.TASK_TYPE_PATCH_FROM_APK:
|
case DownloadTaskParams.TASK_TYPE_PATCH_FROM_APK:
|
||||||
doPatchFromApk(params[0]);
|
doPatchFromApk(params[0]);
|
||||||
break;
|
break;
|
||||||
|
case DownloadTaskParams.TASK_TYPE_PATCH_FROM_PPK:
|
||||||
|
doPatchFromPpk(params[0]);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,14 @@ import java.io.File;
|
|||||||
class DownloadTaskParams {
|
class DownloadTaskParams {
|
||||||
static final int TASK_TYPE_FULL_DOWNLOAD = 1;
|
static final int TASK_TYPE_FULL_DOWNLOAD = 1;
|
||||||
static final int TASK_TYPE_PATCH_FROM_APK = 2;
|
static final int TASK_TYPE_PATCH_FROM_APK = 2;
|
||||||
static final int TASK_TYPE_PATCH_FROM_HASH = 3;
|
static final int TASK_TYPE_PATCH_FROM_PPK = 3;
|
||||||
|
|
||||||
int type;
|
int type;
|
||||||
String url;
|
String url;
|
||||||
String hash;
|
String hash;
|
||||||
File zipFilePath;
|
String originHash;
|
||||||
File unzipDirectory;
|
File zipFilePath;
|
||||||
|
File unzipDirectory;
|
||||||
|
File originDirectory;
|
||||||
UpdateContext.DownloadFileListener listener;
|
UpdateContext.DownloadFileListener listener;
|
||||||
}
|
}
|
||||||
|
@ -60,4 +60,17 @@ public class UpdateContext {
|
|||||||
params.unzipDirectory = new File(rootDir, hashName);
|
params.unzipDirectory = new File(rootDir, hashName);
|
||||||
new DownloadTask(context).execute(params);
|
new DownloadTask(context).execute(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void downloadPatchFromPpk(String url, String hashName, String originHashName, DownloadFileListener listener) {
|
||||||
|
DownloadTaskParams params = new DownloadTaskParams();
|
||||||
|
params.type = DownloadTaskParams.TASK_TYPE_PATCH_FROM_APK;
|
||||||
|
params.url = url;
|
||||||
|
params.hash = hashName;
|
||||||
|
params.originHash = originHashName;
|
||||||
|
params.listener = listener;
|
||||||
|
params.zipFilePath = new File(rootDir, originHashName + "-" + hashName + ".ppk.patch");
|
||||||
|
params.unzipDirectory = new File(rootDir, hashName);
|
||||||
|
params.originDirectory = new File(rootDir, originHashName);
|
||||||
|
new DownloadTask(context).execute(params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,4 +65,22 @@ public class UpdateModule extends ReactContextBaseJavaModule{
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
public void downloadPatchFromPpk(ReadableMap options, final Promise promise){
|
||||||
|
String url = options.getString("updateUrl");
|
||||||
|
String hash = options.getString("hashName");
|
||||||
|
String originHash = options.getString("originHashName");
|
||||||
|
updateContext.downloadPatchFromPpk(url, hash, originHash, new UpdateContext.DownloadFileListener() {
|
||||||
|
@Override
|
||||||
|
public void onDownloadCompleted() {
|
||||||
|
promise.resolve(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDownloadFailed(Throwable error) {
|
||||||
|
promise.reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user