use of com.fanap.podchat.requestobject.RequestUploadFile in project pod-chat-android-sdk by FanapSoft.
the class ChatActivity method createThreadWithFile.
private void createThreadWithFile() {
if (getUri() == null) {
Toast.makeText(this, "Pick a file", Toast.LENGTH_SHORT).show();
return;
}
RequestUploadImage requestUploadThreadImageImage = new RequestUploadImage.Builder(this, getUri()).setwC(140).sethC(140).build();
RequestUploadImage requestUploadImage = new RequestUploadImage.Builder(this, getUri()).setwC(120).sethC(120).setxC(1).setyC(1).build();
RequestUploadFile requestUploadFile = new RequestUploadFile.Builder(this, getUri()).build();
List<Invitee> invite = new ArrayList<>();
// f.kh sandbox
// invite.add(new Invitee("4893", InviteType.Constants.TO_BE_USER_CONTACT_ID));
// POURIA main
invite.add(new Invitee(1151568, InviteType.Constants.TO_BE_USER_CONTACT_ID));
// MASOUD
// invite.add(new Invitee(1511971, InviteType.Constants.TO_BE_USER_CONTACT_ID));
// ARVIN
invite.add(new Invitee(1512305, InviteType.Constants.TO_BE_USER_CONTACT_ID));
// MAHYAR
// invite.add(new Invitee(1196793, InviteType.Constants.TO_BE_USER_CONTACT_ID));
// RequestThreadInnerMessage innerMessage = new RequestThreadInnerMessage
// .Builder(TextMessageType.Constants.PICTURE)
// // .message("Create thread for File Message Test " + new Date().toString())
// // .forwardedMessageIds(listForwardIds)
// .build();
RequestCreateThreadWithFile request = new RequestCreateThreadWithFile.Builder(ThreadType.Constants.OWNER_GROUP, invite, requestUploadFile, TextMessageType.Constants.POD_SPACE_FILE).title("Test File PodSpace").setUploadThreadImageRequest(requestUploadThreadImageImage).build();
presenter.createThreadWithFile(request, new ProgressHandler.sendFileMessage() {
@Override
public void onProgressUpdate(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
Log.e("CTF", "Upload Progress: " + progress);
}
@Override
public void onFinishImage(String json, ChatResponse<ResultImageFile> chatResponse) {
Log.e("CTF", "Upload Finish (IMAGE): ");
}
@Override
public void onFinishFile(String json, ChatResponse<ResultFile> chatResponse) {
Log.e("CTF", "Upload Finish (FILE): ");
}
@Override
public void onError(String jsonError, ErrorOutPut error) {
Log.e("CTF", "Upload Error");
}
});
}
use of com.fanap.podchat.requestobject.RequestUploadFile in project pod-chat-android-sdk by FanapSoft.
the class ChatPresenter method uploadFileProgress.
@Override
public void uploadFileProgress(Context context, Activity activity, Uri fileUri, ProgressHandler.onProgressFile handler) {
RequestUploadFile req = new RequestUploadFile.Builder(activity, fileUri).setPublic(false).build();
chat.uploadFileProgress(req, handler);
}
use of com.fanap.podchat.requestobject.RequestUploadFile 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;
}
use of com.fanap.podchat.requestobject.RequestUploadFile 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);
}
}
}
use of com.fanap.podchat.requestobject.RequestUploadFile in project pod-chat-android-sdk by FanapSoft.
the class ChatPresenter method uploadFile.
@Override
public void uploadFile(@NonNull Activity activity, @NonNull Uri uri) {
RequestUploadFile request = new RequestUploadFile.Builder(activity, uri).build();
chat.uploadFile(request);
}
Aggregations