use of com.fanap.podchat.model.ResultFile in project pod-chat-android-sdk by FanapSoft.
the class PodUploader method generateImageUploadResultForSendMessage.
public static ChatResponse<ResultFile> generateImageUploadResultForSendMessage(UploadToPodSpaceResult response, String uniqueId) {
ResultFile result = PodUploader.generateFileUploadResult(response);
ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
chatResponse.setResult(result);
chatResponse.setUniqueId(uniqueId);
return chatResponse;
}
use of com.fanap.podchat.model.ResultFile in project pod-chat-android-sdk by FanapSoft.
the class PodUploader method generateFileUploadResult.
public static ResultFile generateFileUploadResult(UploadToPodSpaceResult response) {
ResultFile result = new ResultFile();
result.setId(0);
result.setName(response.getName());
result.setHashCode(response.getHashCode());
// result.setDescription(response.);
// TODO: 5/6/2020 ask for description
result.setSize(response.getSize());
result.setUrl(response.getParentHash());
return result;
}
use of com.fanap.podchat.model.ResultFile 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);
}
}
}
use of com.fanap.podchat.model.ResultFile in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method replyFileMessage.
/**
* Reply the message in the current thread and send az message and receive at the
* <p>
* messageContent content of the reply message
* threadId id of the thread
* messageId of the message that we want to reply
* metaData meta data of the message
*/
public String replyFileMessage(RequestReplyFileMessage request, ProgressHandler.sendFileMessage 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("PodSpace server is null", 0, uniqueId);
return uniqueId;
}
long threadId = request.getThreadId();
String messageContent = request.getMessageContent();
String systemMetaData = request.getSystemMetaData();
Uri fileUri = request.getFileUri();
long messageId = request.getMessageId();
int messageType = request.getMessageType();
String methodName = ChatConstant.METHOD_REPLY_MSG;
try {
Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, request.getFileUri(), request.getUserGroupHashCode(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, request.getImageXc(), request.getImageYc(), request.getImageHc(), request.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());
showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
mainReplyMessage(messageContent, threadId, messageId, systemMetaData, messageType, jsonMeta, uniqueId, null);
}
@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);
showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
mainReplyMessage(messageContent, threadId, messageId, systemMetaData, messageType, jsonMeta, uniqueId, null);
}
@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(messageContent, fileUri, messageType, threadId, request.getUserGroupHashCode(), 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);
}
}
return uniqueId;
}
use of com.fanap.podchat.model.ResultFile in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method mainUploadFileMessage.
private void mainUploadFileMessage(LFileUpload lFileUpload) {
JsonObject jsonLog = new JsonObject();
String description = lFileUpload.getDescription();
jsonLog.addProperty("description", description);
Integer messageType = lFileUpload.getMessageType();
jsonLog.addProperty("messageType", messageType);
long threadId = lFileUpload.getThreadId();
jsonLog.addProperty("threadId", threadId);
String uniqueId = lFileUpload.getUniqueId();
jsonLog.addProperty("uniqueId", uniqueId);
String systemMetadata = lFileUpload.getSystemMetaData();
jsonLog.addProperty("systemMetadata", systemMetadata);
long messageId = lFileUpload.getMessageId();
jsonLog.addProperty("messageId", messageId);
String mimeType = lFileUpload.getMimeType();
jsonLog.addProperty("mimeType", mimeType);
File file = lFileUpload.getFile();
ProgressHandler.sendFileMessage handler = lFileUpload.getHandler();
long file_size = lFileUpload.getFileSize();
jsonLog.addProperty("file_size", file_size);
String methodName = lFileUpload.getMethodName();
jsonLog.addProperty("methodName", methodName);
jsonLog.addProperty("name", file.getName());
showLog("UPLOADING_FILE_TO_SERVER", getJsonForLog(jsonLog));
if (chatReady) {
if (getFileServer() != null) {
RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(getFileServer());
FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), file.getName());
ProgressRequestBody requestFile = new ProgressRequestBody(file, mimeType, uniqueId, new ProgressRequestBody.UploadCallbacks() {
@Override
public void onProgress(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
if (handler != null)
handler.onProgressUpdate(uniqueId, progress, totalBytesSent, totalBytesToSend);
}
});
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
Observable<Response<FileUpload>> uploadObservable = fileApi.sendFile(body, getToken(), TOKEN_ISSUER, name);
Subscription subscription = uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(fileUploadResponse -> {
if (fileUploadResponse.isSuccessful() && fileUploadResponse.body() != null) {
boolean error = fileUploadResponse.body().isHasError();
if (error) {
String errorMessage = fileUploadResponse.body().getMessage();
if (log)
Log.e(TAG, errorMessage);
if (handler != null) {
String jsonError = captureError(ChatConstant.ERROR_UPLOAD_FILE, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
ErrorOutPut errorOutPut = new ErrorOutPut(true, ChatConstant.ERROR_UPLOAD_FILE, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
handler.onError(jsonError, errorOutPut);
}
} else {
// remove from Uploading Queue
removeFromUploadQueue(uniqueId);
ResultFile result = fileUploadResponse.body().getResult();
if (result != null) {
long fileId = result.getId();
String hashCode = result.getHashCode();
ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
chatResponse.setResult(result);
chatResponse.setUniqueId(uniqueId);
result.setSize(file_size);
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, hashCode, fileId, mimeType, file_size, "");
if (!Util.isNullOrEmpty(methodName) && methodName.equals(ChatConstant.METHOD_REPLY_MSG)) {
showLog("SEND_REPLY_FILE_MESSAGE", jsonMeta);
mainReplyMessage(description, threadId, messageId, systemMetadata, messageType, jsonMeta, uniqueId, null);
} else {
sendTextMessageWithFile(description, threadId, jsonMeta, systemMetadata, uniqueId, typeCode, messageType);
}
}
}
}
}, throwable -> {
if (log)
Log.e(TAG, throwable.getMessage());
});
/*
* Cancel Uploading progress
* */
initCancelUpload(uniqueId, subscription);
} else {
if (log)
Log.e(TAG, "FileServer url Is null");
}
} else {
captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
// listenerManager.callOnLogEvent(jsonError);
}
}
Aggregations