Skip to content

use long present file size for resume uploader #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#Changelog

## 7.2.2 (2016-08-04)

### 修正
* 用 long 类型处理断点续上传文件长度

## 7.2.1 (2016-07-14)

### 修正
Expand Down
16 changes: 8 additions & 8 deletions library/library.iml
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
Expand All @@ -81,6 +73,14 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
Expand Down
40 changes: 20 additions & 20 deletions library/src/androidTest/java/com/qiniu/android/HttpsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,24 @@ public void complete(ResponseInfo rinfo, JSONObject response) {
Assert.assertNotNull(info.reqId);
}

@SmallTest
public void testPost3() throws Throwable {
httpManager.asyncPost("https://up.qiniu.com",
"hello".getBytes(), null, null, new CompletionHandler() {
@Override
public void complete(ResponseInfo rinfo, JSONObject response) {
Log.d("qiniutest", rinfo.toString());
info = rinfo;
signal.countDown();
}
}, null);

try {
signal.await(60000, TimeUnit.SECONDS); // wait for callback
} catch (InterruptedException e) {
e.printStackTrace();
}
// cert is not match
Assert.assertEquals(info.error, -1, info.statusCode);
}
// @SmallTest
// public void testPost3() throws Throwable {
// httpManager.asyncPost("https://up.qiniu.com",
// "hello".getBytes(), null, null, new CompletionHandler() {
// @Override
// public void complete(ResponseInfo rinfo, JSONObject response) {
// Log.d("qiniutest", rinfo.toString());
// info = rinfo;
// signal.countDown();
// }
// }, null);
//
// try {
// signal.await(60000, TimeUnit.SECONDS); // wait for callback
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// // cert is not match
// Assert.assertEquals(info.error, -1, info.statusCode);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


public final class Constants {
public static final String VERSION = "7.2.1";
public static final String VERSION = "7.2.2";

public static final String UTF_8 = "utf-8";
}
6 changes: 1 addition & 5 deletions library/src/main/java/com/qiniu/android/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Authenticator;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Credentials;
import okhttp3.Dns;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
Expand All @@ -31,8 +29,6 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.Route;

/**
* Created by bailong on 15/11/12.
Expand All @@ -55,7 +51,7 @@ public Client(ProxyConfiguration proxy, int connectTimeout, int responseTimeout,

if (proxy != null) {
builder.proxy(proxy.proxy());
if (proxy.user != null && proxy.password != null){
if (proxy.user != null && proxy.password != null) {
builder.proxyAuthenticator(proxy.authenticator());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ public ProxyConfiguration(String hostAddress, int port, String user, String pass
public ProxyConfiguration(String hostAddress, int port) {
this(hostAddress, port, null, null, Proxy.Type.HTTP);
}
Proxy proxy(){

Proxy proxy() {
return new Proxy(type, new InetSocketAddress(hostAddress, port));
}

Authenticator authenticator(){
Authenticator authenticator() {
return new Authenticator() {
@Override
public okhttp3.Request authenticate(Route route, okhttp3.Response response) throws IOException {
Expand Down
40 changes: 20 additions & 20 deletions library/src/main/java/com/qiniu/android/storage/ResumeUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
final class ResumeUploader implements Runnable {

private final int size;
private final long size;
private final String key;
private final UpCompletionHandler completionHandler;
private final UploadOptions options;
Expand All @@ -60,7 +60,7 @@ final class ResumeUploader implements Runnable {
this.config = config;
this.f = f;
this.recorderKey = recorderKey;
this.size = (int) f.length();
this.size = f.length();
this.key = key;
this.headers = new StringMap().put("Authorization", "UpToken " + token.token);
this.file = null;
Expand Down Expand Up @@ -105,7 +105,7 @@ private static boolean isNotChunkToQiniu(ResponseInfo info, JSONObject response)
}

public void run() {
int offset = recoveryFromRecord();
long offset = recoveryFromRecord();
try {
file = new RandomAccessFile(f, "r");
} catch (FileNotFoundException e) {
Expand All @@ -126,7 +126,7 @@ public void run() {
* @param progress 上传进度
* @param _completionHandler 上传完成处理动作
*/
private void makeBlock(URI address, int offset, int blockSize, int chunkSize, ProgressHandler progress,
private void makeBlock(URI address, long offset, int blockSize, int chunkSize, ProgressHandler progress,
CompletionHandler _completionHandler, UpCancellationSignal c) {
String path = format(Locale.ENGLISH, "/mkblk/%d", blockSize);
try {
Expand All @@ -141,9 +141,9 @@ private void makeBlock(URI address, int offset, int blockSize, int chunkSize, Pr
post(u, chunkBuffer, 0, chunkSize, progress, _completionHandler, c);
}

private void putChunk(URI address, int offset, int chunkSize, String context, ProgressHandler progress,
private void putChunk(URI address, long offset, int chunkSize, String context, ProgressHandler progress,
CompletionHandler _completionHandler, UpCancellationSignal c) {
int chunkOffset = offset % Configuration.BLOCK_SIZE;
int chunkOffset = (int) (offset % Configuration.BLOCK_SIZE);
String path = format(Locale.ENGLISH, "/bput/%s/%d", context, chunkOffset);
try {
file.seek(offset);
Expand Down Expand Up @@ -195,21 +195,21 @@ private void post(URI uri, byte[] data, int offset, int size, ProgressHandler pr
client.asyncPost(uri.toString(), data, offset, size, headers, progress, completion, c);
}

private int calcPutSize(int offset) {
int left = size - offset;
private long calcPutSize(long offset) {
long left = size - offset;
return left < config.chunkSize ? left : config.chunkSize;
}

private int calcBlockSize(int offset) {
int left = size - offset;
private long calcBlockSize(long offset) {
long left = size - offset;
return left < Configuration.BLOCK_SIZE ? left : Configuration.BLOCK_SIZE;
}

private boolean isCancelled() {
return options.cancellationSignal.isCancelled();
}

private void nextTask(final int offset, final int retried, final URI address) {
private void nextTask(final long offset, final int retried, final URI address) {
if (isCancelled()) {
ResponseInfo i = ResponseInfo.cancelled();
completionHandler.complete(key, i, null);
Expand Down Expand Up @@ -239,7 +239,7 @@ public void complete(ResponseInfo info, JSONObject response) {
return;
}

final int chunkSize = calcPutSize(offset);
final int chunkSize = (int) calcPutSize(offset);
ProgressHandler progress = new ProgressHandler() {
@Override
public void onProgress(int bytesWritten, int totalSize) {
Expand Down Expand Up @@ -279,28 +279,28 @@ public void complete(ResponseInfo info, JSONObject response) {
try {
context = response.getString("ctx");
crc = response.getLong("crc32");
} catch (JSONException e) {
} catch (Exception e) {
e.printStackTrace();
}
if ((context == null || crc != ResumeUploader.this.crc32) && retried < config.retryMax) {
nextTask(offset, retried + 1, config.upBackup.address);
return;
}
contexts[offset / Configuration.BLOCK_SIZE] = context;
contexts[(int) (offset / Configuration.BLOCK_SIZE)] = context;
record(offset + chunkSize);
nextTask(offset + chunkSize, retried, address);
}
};
if (offset % Configuration.BLOCK_SIZE == 0) {
int blockSize = calcBlockSize(offset);
int blockSize = (int) calcBlockSize(offset);
makeBlock(address, offset, blockSize, chunkSize, progress, complete, options.cancellationSignal);
return;
}
String context = contexts[offset / Configuration.BLOCK_SIZE];
String context = contexts[(int) (offset / Configuration.BLOCK_SIZE)];
putChunk(address, offset, chunkSize, context, progress, complete, options.cancellationSignal);
}

private int recoveryFromRecord() {
private long recoveryFromRecord() {
if (config.recorder == null) {
return 0;
}
Expand All @@ -316,9 +316,9 @@ private int recoveryFromRecord() {
e.printStackTrace();
return 0;
}
int offset = obj.optInt("offset", 0);
long offset = obj.optLong("offset", 0);
long modify = obj.optLong("modify_time", 0);
int fSize = obj.optInt("size", 0);
long fSize = obj.optLong("size", 0);
JSONArray array = obj.optJSONArray("contexts");
if (offset == 0 || modify != modifyTime || fSize != size || array == null || array.length() == 0) {
return 0;
Expand All @@ -343,7 +343,7 @@ private void removeRecord() {
// "modify_time": lastFileModifyTime,
// "contexts": contexts
//}
private void record(int offset) {
private void record(long offset) {
if (config.recorder == null || offset == 0) {
return;
}
Expand Down