use of net.iGap.helper.upload.UploadRequestBody in project iGap-Android by KianIranian-STDG.
the class UploadHttpRequest method startOrResume.
private void startOrResume() {
if (fileObject.fileToken == null) {
FileLog.i(TAG, "startOrResume: " + fileObject.key);
return;
}
isUploading = true;
storeTime = 0;
resumeRetryCount = 0;
String url = FILE + "upload/" + fileObject.fileToken;
SecureRandom secureRandom = new SecureRandom();
byte[] iv = secureRandom.generateSeed(16);
fileObject.auth = G.symmetricKeyString.getBytes();
try (FileInputStream fileInputStream = new FileInputStream(fileObject.file);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(iv)) {
if (fileObject.offset > 0 && isResume) {
try {
long skip = fileInputStream.skip(fileObject.offset);
} catch (IOException e) {
fileObject.offset = 0;
FileLog.e(e);
}
}
try (InputStream inputStream = new SequenceInputStream(byteArrayInputStream, new CipherInputStream(fileInputStream, getCipher(iv)))) {
RequestBody requestBody = new UploadRequestBody(null, fileObject.offset, inputStream, new UploadRequestBody.IOnProgressListener() {
@Override
public void onProgress(long totalByte) {
if (cancelDownload.get() && isUploading) {
isUploading = false;
error(new Exception("Upload Canceled"), false);
return;
}
int progress = (int) ((totalByte * 100) / fileObject.file.length());
if (fileObject.progress < progress) {
fileObject.progress = progress;
FileLog.i(TAG, fileObject.fileToken + " progress -> " + fileObject.progress + " bytes -> " + totalByte);
AndroidUtils.globalQueue.postRunnable(() -> {
if (delegate != null) {
delegate.onUploadProgress(fileObject);
}
});
if (storeTime >= 3) {
storeTime = 0;
preferences.edit().putLong("offset_" + md5Key, totalByte).putString("token_" + md5Key, fileObject.fileToken).putInt("progress_" + md5Key, fileObject.progress).apply();
}
storeTime++;
}
}
@Override
public void onError(Exception e) {
FileLog.i(TAG, "Error from stream " + e.getMessage());
}
});
Request request = new Request.Builder().url(url).post(requestBody).addHeader("Authorization", TokenContainer.getInstance().getToken()).build();
requestCall = client.newCall(request);
AndroidUtils.globalQueue.postRunnable(() -> {
// FIXME: 10/12/2020 remove to SendMessageUtil
if (fileObject.message != null) {
HelperSetAction.setActionFiles(fileObject.message.getRoomId(), fileObject.messageId, HelperSetAction.getAction(fileObject.messageType), fileObject.roomType);
}
});
Response response = requestCall.execute();
if (response.isSuccessful() && response.body() != null) {
preferences.edit().remove("offset_" + md5Key).remove("token_" + md5Key).remove("progress_" + md5Key).apply();
AndroidUtils.globalQueue.postRunnable(() -> {
HelperSetAction.sendCancel(fileObject.messageId);
if (delegate != null) {
delegate.onUploadFinish(fileObject);
}
});
} else if (response.body() != null) {
if (response.code() >= 500 && response.code() < 600) {
preferences.edit().remove("offset_" + md5Key).remove("token_" + md5Key).remove("progress_" + md5Key).apply();
}
error(new Exception(response.body().string()), false);
}
} catch (Exception e) {
error(e, false);
}
} catch (FileNotFoundException e) {
FileLog.e(e);
} catch (IOException e) {
FileLog.e(e);
}
}
Aggregations