Search in sources :

Example 1 with UploadToPodSpaceResult

use of com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method retryUpload.

/**
 * It retry upload that they didn't send
 */
// public void retryUpload(RetryUpload retry, ProgressHandler.sendFileMessage handler) {
// 
// Runnable retryTask = () -> {
// String uniqueId = retry.getUniqueId();
// Activity activity = retry.getActivity();
// 
// UploadingQueueCache uploadingQ;
// if (cache) {
// uploadingQ = messageDatabaseHelper.getUploadingQ(uniqueId);
// } else {
// uploadingQ = uploadingQList.get(uniqueId);
// }
// 
// if (uploadingQ != null) {
// long messageId = uploadingQ.getId();
// int messageType = uploadingQ.getMessageType();
// long threadId = uploadingQ.getThreadId();
// String message = uploadingQ.getMessage();
// String systemMetadata = uploadingQ.getSystemMetadata();
// MetaDataFile metaDataFile = gson.fromJson(systemMetadata, MetaDataFile.class);
// String link = metaDataFile.getFile().getLink();
// String mimeType = metaDataFile.getFile().getMimeType();
// 
// LFileUpload lFileUpload = new LFileUpload();
// lFileUpload.setActivity(activity);
// lFileUpload.setDescription(message);
// lFileUpload.setFileUri(Uri.parse(link));
// lFileUpload.setHandler(handler);
// lFileUpload.setMessageType(messageType);
// lFileUpload.setThreadId(threadId);
// lFileUpload.setUniqueId(uniqueId);
// lFileUpload.setSystemMetaData(systemMetadata);
// lFileUpload.setHandler(handler);
// lFileUpload.setMimeType(mimeType);
// 
// if (!Util.isNullOrEmpty(messageId)) {
// String methodName = ChatConstant.METHOD_REPLY_MSG;
// lFileUpload.setMethodName(methodName);
// }
// 
// removeFromUploadQueue(uniqueId);
// 
// if (FileUtils.isImage(mimeType) && !FileUtils.isGif(mimeType)) {
// uploadImageFileMessage(lFileUpload);
// } else {
// uploadFileMessage(lFileUpload);
// }
// 
// }
// };
// 
// new PodThreadManager()
// .doThisAndGo(retryTask);
// 
// }
public void retryUpload(RetryUpload retry, ProgressHandler.sendFileMessage handler) {
    Runnable retryTask = () -> {
        String uniqueId = retry.getUniqueId();
        Activity activity = retry.getActivity();
        UploadingQueueCache uploadingQ;
        if (cache) {
            uploadingQ = dataSource.getUploadingQ(uniqueId);
        // uploadingQ = messageDatabaseHelper.getUploadingQ(uniqueId);
        } else {
            uploadingQ = uploadingQList.get(uniqueId);
        }
        if (uploadingQ != null) {
            long messageId = uploadingQ.getId();
            int messageType = uploadingQ.getMessageType();
            long threadId = uploadingQ.getThreadId();
            String userGroupHash = uploadingQ.getUserGroupHash();
            String methodName = !Util.isNullOrEmpty(messageId) ? ChatConstant.METHOD_REPLY_MSG : null;
            String message = uploadingQ.getMessage();
            String systemMetadata = uploadingQ.getSystemMetadata();
            String metadata = uploadingQ.getMetadata();
            MetaDataFile metaDataFile = gson.fromJson(metadata, MetaDataFile.class);
            String link = null;
            try {
                link = metaDataFile.getFile().getLink();
            } catch (Exception e) {
                Log.e(TAG, "Couldn't retrieve link");
            }
            if (needReadStoragePermission(activity)) {
                String jsonError = captureError(ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
                ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
                if (handler != null) {
                    handler.onError(jsonError, error);
                }
                return;
            }
            if (!chatReady) {
                onChatNotReady(uniqueId);
                return;
            }
            if (getPodSpaceServer() == null) {
                captureError("File server is null", 0, uniqueId);
                return;
            }
            removeFromUploadQueue(uniqueId);
            try {
                String finalLink = link;
                Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, Util.isNullOrEmpty(link) ? null : Uri.parse(link), userGroupHash, context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, new PodUploader.IPodUploadFileToPodSpace() {

                    @Override
                    public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length) {
                        removeFromUploadQueue(uniqueId);
                        ChatResponse<ResultFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId);
                        String json = gson.toJson(chatResponse);
                        showLog("FILE_UPLOADED_TO_SERVER", json);
                        listenerManager.callOnUploadFile(json, chatResponse);
                        if (handler != null) {
                            handler.onFinishFile(json, chatResponse);
                        }
                        String jsonMeta = createFileMetadata(file, response.getHashCode(), 0, mimeType, length, response.getParentHash());
                        if (isReplyMessage(methodName)) {
                            showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
                            mainReplyMessage(message, threadId, messageId, systemMetadata, messageType, jsonMeta, uniqueId, null);
                        } else {
                            sendTextMessageWithFile(message, threadId, jsonMeta, systemMetadata, uniqueId, typeCode, messageType);
                        }
                    }

                    @Override
                    public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length, int actualWidth, int actualHeight, int width, int height) {
                        removeFromUploadQueue(uniqueId);
                        ChatResponse<ResultImageFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId, actualWidth, actualHeight, width, height, getPodSpaceImageUrl(response.getHashCode()));
                        String imageJson = gson.toJson(chatResponse);
                        listenerManager.callOnUploadImageFile(imageJson, chatResponse);
                        if (handler != null) {
                            handler.onFinishImage(imageJson, chatResponse);
                        }
                        showLog("RECEIVE_UPLOAD_IMAGE", imageJson);
                        String jsonMeta = createImageMetadata(file, response.getHashCode(), 0, height, width, mimeType, length, response.getParentHash(), false, null);
                        if (isReplyMessage(methodName)) {
                            showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
                            mainReplyMessage(message, threadId, messageId, systemMetadata, messageType, jsonMeta, uniqueId, null);
                        } else {
                            sendTextMessageWithFile(message, threadId, jsonMeta, systemMetadata, uniqueId, typeCode, messageType);
                        }
                    }

                    @Override
                    public void onFailure(String cause, Throwable t) {
                        String jsonError = captureError(cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId, t);
                        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
                        if (handler != null) {
                            handler.onError(jsonError, error);
                        }
                    }

                    @Override
                    public void onUploadStarted(String mimeType, File file, long length) {
                        addToUploadQueue(message, Util.isNullOrEmpty(finalLink) ? null : Uri.parse(finalLink), messageType, threadId, userGroupHash, uniqueId, systemMetadata, messageId, mimeType, null, methodName, file, length);
                        showLog("UPLOAD_FILE_TO_SERVER");
                    }

                    @Override
                    public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
                        if (handler != null)
                            handler.onProgressUpdate(uniqueId, progress, totalBytesSent, totalBytesToSend);
                    }
                });
                initCancelUpload(uniqueId, subscription);
            } catch (Exception e) {
                String jsonError = captureError(ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId, e);
                ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
                if (handler != null) {
                    handler.onError(jsonError, error);
                }
            }
        }
    };
    new PodThreadManager().doThisAndGo(retryTask);
}
Also used : PodThreadManager(com.fanap.podchat.util.PodThreadManager) UploadingQueueCache(com.fanap.podchat.cachemodel.queue.UploadingQueueCache) Activity(android.app.Activity) ResultFile(com.fanap.podchat.model.ResultFile) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) ResultImageFile(com.fanap.podchat.model.ResultImageFile) UploadToPodSpaceResult(com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile) MetaDataFile(com.fanap.podchat.model.MetaDataFile)

Example 2 with UploadToPodSpaceResult

use of com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method sendFileMessage.

/**
 * This method first check the messageType of the file and then choose the right
 * server and send that
 * <p>
 * description    Its the description that you want to send with file in the thread
 * fileUri        Uri of the file that you want to send to thread
 * threadId       Id of the thread that you want to send file
 * systemMetaData [optional]
 * handler        it is for send file message with progress
 */
// public String sendFileMessage(RequestFileMessage requestFileMessage, ProgressHandler.sendFileMessage handler) {
// 
// long threadId = requestFileMessage.getThreadId();
// Activity activity = requestFileMessage.getActivity();
// Uri fileUri = requestFileMessage.getFileUri();
// String description = requestFileMessage.getDescription();
// int messageType = requestFileMessage.getMessageType();
// String systemMetadata = requestFileMessage.getSystemMetadata();
// 
// return sendFileMessage(activity, description, threadId, fileUri, systemMetadata, messageType, handler);
// }
public String sendFileMessage(RequestFileMessage requestFileMessage, ProgressHandler.sendFileMessage handler) {
    String uniqueId = generateUniqueId();
    if (needReadStoragePermission(requestFileMessage.getActivity())) {
        String jsonError = captureError(ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
        return uniqueId;
    }
    if (!chatReady) {
        onChatNotReady(uniqueId);
        return uniqueId;
    }
    if (getPodSpaceServer() == null) {
        captureError("PodSpace server is null", 0, uniqueId);
        return uniqueId;
    }
    try {
        Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, requestFileMessage.getFileUri(), requestFileMessage.getUserGroupHash(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, requestFileMessage.getImageXc(), requestFileMessage.getImageYc(), requestFileMessage.getImageHc(), requestFileMessage.getImageWc(), new PodUploader.IPodUploadFileToPodSpace() {

            @Override
            public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length) {
                removeFromUploadQueue(uniqueId);
                ChatResponse<ResultFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId);
                String json = gson.toJson(chatResponse);
                showLog("FILE_UPLOADED_TO_SERVER", json);
                listenerManager.callOnUploadFile(json, chatResponse);
                if (handler != null) {
                    handler.onFinishFile(json, chatResponse);
                }
                String jsonMeta = createFileMetadata(file, response.getHashCode(), 0, mimeType, length, response.getParentHash());
                sendTextMessageWithFile(requestFileMessage.getDescription(), requestFileMessage.getThreadId(), jsonMeta, requestFileMessage.getSystemMetadata(), uniqueId, typeCode, requestFileMessage.getMessageType());
            }

            @Override
            public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length, int actualWidth, int actualHeight, int width, int height) {
                removeFromUploadQueue(uniqueId);
                ChatResponse<ResultImageFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId, actualWidth, actualHeight, width, height, getPodSpaceImageUrl(response.getHashCode()));
                String imageJson = gson.toJson(chatResponse);
                listenerManager.callOnUploadImageFile(imageJson, chatResponse);
                if (handler != null) {
                    handler.onFinishImage(imageJson, chatResponse);
                }
                showLog("RECEIVE_UPLOAD_IMAGE", imageJson);
                String jsonMeta = createImageMetadata(file, response.getHashCode(), 0, height, width, mimeType, length, response.getParentHash(), false, null);
                sendTextMessageWithFile(requestFileMessage.getDescription(), requestFileMessage.getThreadId(), jsonMeta, requestFileMessage.getSystemMetadata(), uniqueId, typeCode, requestFileMessage.getMessageType());
            }

            @Override
            public void onFailure(String cause, Throwable t) {
                String jsonError = captureError(cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId, t);
                ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
                if (handler != null) {
                    handler.onError(jsonError, error);
                }
            }

            @Override
            public void onUploadStarted(String mimeType, File file, long length) {
                addToUploadQueue(requestFileMessage.getDescription(), requestFileMessage.getFileUri(), requestFileMessage.getMessageType(), requestFileMessage.getThreadId(), requestFileMessage.getUserGroupHash(), uniqueId, requestFileMessage.getSystemMetadata(), mimeType, file, length);
                showLog("UPLOAD_FILE_TO_SERVER_STARTED");
                showLog(requestFileMessage.toString());
            }

            @Override
            public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
                if (handler != null)
                    handler.onProgressUpdate(uniqueId, progress, totalBytesSent, totalBytesToSend);
            }
        });
        initCancelUpload(uniqueId, subscription);
    } catch (Exception e) {
        String jsonError = captureError(ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId, e);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
    }
    return uniqueId;
}
Also used : PodUploader(com.fanap.podchat.chat.file_manager.upload_file.PodUploader) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) UploadToPodSpaceResult(com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult) ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile)

Example 3 with UploadToPodSpaceResult

use of com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method uploadFileProgress.

/**
 * It uploads file and it shows progress of the file downloading
 */
public String uploadFileProgress(RequestUploadFile request, @Nullable ProgressHandler.onProgressFile handler) {
    String uniqueId = generateUniqueId();
    if (needReadStoragePermission(request.getActivity())) {
        String jsonError = captureError(ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
        return uniqueId;
    }
    if (!chatReady) {
        onChatNotReady(uniqueId);
        return uniqueId;
    }
    if (getPodSpaceServer() == null) {
        captureError("File server is null", 0, uniqueId);
        return uniqueId;
    }
    try {
        Subscription subscription = PodUploader.uploadPublicToPodSpace(uniqueId, request.getFileUri(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, request.isPublic(), new PodUploader.IPodUploadFileToPodSpace() {

            @Override
            public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length) {
                ResultFile resultFile = PodUploader.generateFileUploadResult(response);
                FileUpload result = new FileUpload();
                result.setResult(resultFile);
                ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
                resultFile.setUrl(getPodSpaceFileUrl(resultFile.getHashCode()));
                showLog("FINISH_UPLOAD_FILE", gson.toJson(resultFile));
                chatResponse.setResult(resultFile);
                chatResponse.setUniqueId(uniqueId);
                if (handler != null) {
                    handler.onFinish(gson.toJson(chatResponse), result);
                }
                listenerManager.callOnUploadFile(gson.toJson(resultFile), chatResponse);
            }

            @Override
            public void onFailure(String cause, Throwable t) {
                String jsonError = captureError(cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId, t);
                ErrorOutPut error = new ErrorOutPut(true, cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
                if (handler != null) {
                    handler.onError(jsonError, error);
                }
            }

            @Override
            public void onUploadStarted(String mimeType, File file, long length) {
                showLog("UPLOADING_FILE");
            }

            @Override
            public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
                if (handler != null) {
                    handler.onProgressUpdate(progress);
                    handler.onProgress(uniqueId, progress, totalBytesSent, totalBytesToSend);
                }
            }
        });
        initCancelUpload(uniqueId, subscription);
    } catch (Exception e) {
        String jsonError = captureError(ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId, e);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
    }
    return uniqueId;
}
Also used : ResultFile(com.fanap.podchat.model.ResultFile) PodUploader(com.fanap.podchat.chat.file_manager.upload_file.PodUploader) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) UploadToPodSpaceResult(com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult) ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile) FileUpload(com.fanap.podchat.mainmodel.FileUpload) LFileUpload(com.fanap.podchat.localmodel.LFileUpload)

Example 4 with UploadToPodSpaceResult

use of com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method uploadFileToThread.

// private String uploadFileProgress(Activity activity, Uri uri, ProgressHandler.onProgressFile handler) {
// String uniqueId = generateUniqueId();
// try {
// if (chatReady) {
// 
// if (Permission.Check_READ_STORAGE(activity)) {
// 
// if (getFileServer() != null) {
// 
// String mimeType = getMimType(uri);
// //                    File file = new File(getRealPathFromURI(context, uri));
// String path = FilePick.getSmartFilePath(getContext(), uri);
// File file = new File(path);
// 
// 
// JsonObject jLog = new JsonObject();
// 
// jLog.addProperty("name", file.getName());
// jLog.addProperty("token", getToken());
// jLog.addProperty("tokenIssuer", TOKEN_ISSUER);
// jLog.addProperty("uniqueId", uniqueId);
// 
// showLog("UPLOADING_FILE", getJsonForLog(jLog));
// 
// RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(getFileServer());
// FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
// RequestBody name = RequestBody.create(MediaType.parse("text/plain"), file.getName());
// ProgressRequestBody requestFile = new ProgressRequestBody(file, mimeType, uniqueId, new ProgressRequestBody.UploadCallbacks() {
// 
// @Override
// public void onProgress(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
// handler.onProgress(uniqueId, progress, totalBytesSent, totalBytesToSend);
// handler.onProgressUpdate(progress);
// }
// 
// });
// 
// MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
// Observable<Response<FileUpload>> uploadObservable = fileApi.sendFile(body, getToken(), TOKEN_ISSUER, name);
// uploadObservable.subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe(fileUploadResponse -> {
// if (fileUploadResponse.isSuccessful()) {
// boolean hasError = fileUploadResponse.body().isHasError();
// if (hasError) {
// String errorMessage = fileUploadResponse.body().getMessage();
// int errorCode = fileUploadResponse.body().getErrorCode();
// String jsonError = getErrorOutPut(errorMessage, errorCode, uniqueId);
// ErrorOutPut error = new ErrorOutPut(true, errorMessage, errorCode, uniqueId);
// handler.onError(jsonError, error);
// } else {
// 
// FileUpload result = fileUploadResponse.body();
// ResultFile resultFile = result.getResult();
// resultFile.setUrl(getFile(resultFile.getId(), resultFile.getHashCode(), true));
// 
// 
// ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
// chatResponse.setResult(resultFile);
// chatResponse.setUniqueId(uniqueId);
// String json = gson.toJson(chatResponse);
// showLog("FINISH_UPLOAD_FILE", json);
// listenerManager.callOnUploadFile(json, chatResponse);
// 
// handler.onFinish(json, result);
// }
// }
// }, throwable -> {
// ErrorOutPut error = new ErrorOutPut(true, throwable.getMessage(), 0, uniqueId);
// String json = gson.toJson(error);
// getErrorOutPut(ChatConstant.ERROR_UPLOAD_FILE + " " + throwable.getMessage(), ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
// handler.onError(json, error);
// 
// });
// } else {
// 
// if (log) Log.e(TAG, "FileServer url Is null");
// 
// getErrorOutPut("File Server url Is null", ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
// 
// }
// 
// } else {
// String jsonError = getErrorOutPut(ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION
// , ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
// ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION
// , ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
// handler.onError(jsonError, error);
// Permission.Request_WRITE_TORAGE(activity, WRITE_EXTERNAL_STORAGE_CODE);
// }
// } else {
// getErrorOutPut(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
// }
// 
// } catch (Throwable throwable) {
// if (log) Log.e(TAG, throwable.getMessage());
// getErrorOutPut(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId);
// 
// }
// return uniqueId;
// }
// new upload file function
private void uploadFileToThread(RequestUploadFile request, String userGroupHash, String uniqueId, @Nullable ProgressHandler.onProgressFile handler, OnWorkDone listener) {
    if (needReadStoragePermission(request.getActivity())) {
        String jsonError = captureError(ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
        return;
    }
    if (!chatReady) {
        onChatNotReady(uniqueId);
        return;
    }
    if (getPodSpaceServer() == null) {
        captureError("File server is null", 0, uniqueId);
        return;
    }
    try {
        Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, request.getFileUri(), request.getUserGroupHashCode(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, new PodUploader.IPodUploadFileToPodSpace() {

            @Override
            public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length) {
                ResultFile resultFile = PodUploader.generateFileUploadResult(response);
                FileUpload result = new FileUpload();
                result.setResult(resultFile);
                ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
                resultFile.setUrl(getFile(resultFile.getId(), resultFile.getHashCode(), true));
                showLog("FINISH_UPLOAD_FILE", gson.toJson(resultFile));
                MetaDataFile metaDataFile = new MetaDataFile();
                FileMetaDataContent metaDataContent = new FileMetaDataContent();
                metaDataContent.setHashCode(resultFile.getHashCode());
                metaDataContent.setId(resultFile.getId());
                metaDataContent.setName(resultFile.getName());
                metaDataFile.setFile(metaDataContent);
                chatResponse.setResult(resultFile);
                chatResponse.setUniqueId(uniqueId);
                JsonObject metadata = (JsonObject) gson.toJsonTree(metaDataFile);
                metadata.addProperty("name", result.getResult().getName());
                metadata.addProperty("id", result.getResult().getId());
                if (handler != null) {
                    handler.onFinish(gson.toJson(chatResponse), result);
                }
                listener.onWorkDone(metadata.toString());
            }

            @Override
            public void onFailure(String cause, Throwable t) {
                String jsonError = captureError(cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId, t);
                ErrorOutPut error = new ErrorOutPut(true, cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
                if (handler != null) {
                    handler.onError(jsonError, error);
                }
            }

            @Override
            public void onUploadStarted(String mimeType, File file, long length) {
                showLog("UPLOADING_FILE");
            }

            @Override
            public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
                if (handler != null) {
                    handler.onProgressUpdate(progress);
                    handler.onProgress(uniqueId, progress, totalBytesSent, totalBytesToSend);
                }
            }
        });
        initCancelUpload(uniqueId, subscription);
    } catch (Exception e) {
        String jsonError = captureError(ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId, e);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
    }
}
Also used : FileMetaDataContent(com.fanap.podchat.model.FileMetaDataContent) JsonObject(com.google.gson.JsonObject) ResultFile(com.fanap.podchat.model.ResultFile) PodUploader(com.fanap.podchat.chat.file_manager.upload_file.PodUploader) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) UploadToPodSpaceResult(com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult) ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile) FileUpload(com.fanap.podchat.mainmodel.FileUpload) LFileUpload(com.fanap.podchat.localmodel.LFileUpload) MetaDataFile(com.fanap.podchat.model.MetaDataFile)

Example 5 with UploadToPodSpaceResult

use of com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method sendFileMessage.

private void sendFileMessage(RequestFileMessage requestFileMessage, String uniqueId, ProgressHandler.sendFileMessage handler) {
    if (needReadStoragePermission(requestFileMessage.getActivity())) {
        String jsonError = captureError(ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
        return;
    }
    if (!chatReady) {
        onChatNotReady(uniqueId);
        return;
    }
    if (getPodSpaceServer() == null) {
        captureError("File server is null", 0, uniqueId);
        return;
    }
    try {
        Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, requestFileMessage.getFileUri(), requestFileMessage.getUserGroupHash(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, requestFileMessage.getImageXc(), requestFileMessage.getImageYc(), requestFileMessage.getImageHc(), requestFileMessage.getImageWc(), new PodUploader.IPodUploadFileToPodSpace() {

            @Override
            public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length) {
                removeFromUploadQueue(uniqueId);
                ChatResponse<ResultFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId);
                String json = gson.toJson(chatResponse);
                showLog("FILE_UPLOADED_TO_SERVER", json);
                listenerManager.callOnUploadFile(json, chatResponse);
                if (handler != null) {
                    handler.onFinishFile(json, chatResponse);
                }
                String jsonMeta = createFileMetadata(file, response.getHashCode(), 0, mimeType, length, response.getParentHash());
                sendTextMessageWithFile(requestFileMessage.getDescription(), requestFileMessage.getThreadId(), jsonMeta, requestFileMessage.getSystemMetadata(), uniqueId, typeCode, requestFileMessage.getMessageType());
            }

            @Override
            public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length, int actualWidth, int actualHeight, int width, int height) {
                removeFromUploadQueue(uniqueId);
                ChatResponse<ResultImageFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId, actualWidth, actualHeight, width, height, getPodSpaceImageUrl(response.getHashCode()));
                String imageJson = gson.toJson(chatResponse);
                listenerManager.callOnUploadImageFile(imageJson, chatResponse);
                if (handler != null) {
                    handler.onFinishImage(imageJson, chatResponse);
                }
                showLog("RECEIVE_UPLOAD_IMAGE", imageJson);
                String jsonMeta = createImageMetadata(file, response.getHashCode(), 0, height, width, mimeType, length, response.getParentHash(), false, null);
                sendTextMessageWithFile(requestFileMessage.getDescription(), requestFileMessage.getThreadId(), jsonMeta, requestFileMessage.getSystemMetadata(), uniqueId, typeCode, requestFileMessage.getMessageType());
            }

            @Override
            public void onFailure(String cause, Throwable t) {
                String jsonError = captureError(cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId, t);
                ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
                if (handler != null) {
                    handler.onError(jsonError, error);
                }
            }

            @Override
            public void onUploadStarted(String mimeType, File file, long length) {
                addToUploadQueue(requestFileMessage.getDescription(), requestFileMessage.getFileUri(), requestFileMessage.getMessageType(), requestFileMessage.getThreadId(), requestFileMessage.getUserGroupHash(), uniqueId, requestFileMessage.getSystemMetadata(), mimeType, file, length);
                showLog("UPLOAD_FILE_TO_SERVER");
                showLog(requestFileMessage.toString());
            }

            @Override
            public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
                if (handler != null)
                    handler.onProgressUpdate(uniqueId, progress, totalBytesSent, totalBytesToSend);
            }
        });
        initCancelUpload(uniqueId, subscription);
    } catch (Exception e) {
        String jsonError = captureError(ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId, e);
        ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
        if (handler != null) {
            handler.onError(jsonError, error);
        }
    }
}
Also used : PodUploader(com.fanap.podchat.chat.file_manager.upload_file.PodUploader) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException) UploadToPodSpaceResult(com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult) ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) Subscription(rx.Subscription) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile)

Aggregations

ResultDownloadFile (com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile)9 UploadToPodSpaceResult (com.fanap.podchat.chat.file_manager.upload_file.UploadToPodSpaceResult)9 ErrorOutPut (com.fanap.podchat.model.ErrorOutPut)9 MetaDataFile (com.fanap.podchat.model.MetaDataFile)9 ResultFile (com.fanap.podchat.model.ResultFile)9 ResultImageFile (com.fanap.podchat.model.ResultImageFile)9 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)9 RequestCreateThreadWithFile (com.fanap.podchat.requestobject.RequestCreateThreadWithFile)9 RequestGetFile (com.fanap.podchat.requestobject.RequestGetFile)9 RequestGetPodSpaceFile (com.fanap.podchat.requestobject.RequestGetPodSpaceFile)9 RequestUploadFile (com.fanap.podchat.requestobject.RequestUploadFile)9 PodChatException (com.fanap.podchat.util.PodChatException)9 JsonSyntaxException (com.google.gson.JsonSyntaxException)9 SentryException (io.sentry.core.protocol.SentryException)9 File (java.io.File)9 IOException (java.io.IOException)9 JSONException (org.json.JSONException)9 Subscription (rx.Subscription)9 ChatResponse (com.fanap.podchat.model.ChatResponse)8 PodUploader (com.fanap.podchat.chat.file_manager.upload_file.PodUploader)7