Search in sources :

Example 1 with UploadRequestBody

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);
    }
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Request(okhttp3.Request) FileNotFoundException(java.io.FileNotFoundException) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JSONException(org.json.JSONException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Response(okhttp3.Response) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) UploadRequestBody(net.iGap.helper.upload.UploadRequestBody) RequestBody(okhttp3.RequestBody) UploadRequestBody(net.iGap.helper.upload.UploadRequestBody)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 SecureRandom (java.security.SecureRandom)1 CipherInputStream (javax.crypto.CipherInputStream)1 UploadRequestBody (net.iGap.helper.upload.UploadRequestBody)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1 JSONException (org.json.JSONException)1