use of com.fanap.podchat.chat.Chat.TAG in project pod-chat-android-sdk by FanapSoft.
the class PodUploader method uploadImageToChatServer.
public static Subscription uploadImageToChatServer(String uniqueId, @NonNull Uri fileUri, Context context, String fileServer, String token, int tokenIssuer, IPodUploadImage listener) throws Exception {
if (fileUri.getPath() == null)
throw new NullPointerException("Invalid file uri!");
String mimeType = FileUtils.getMimeType(fileUri, context);
String path = FilePick.getSmartFilePath(context, fileUri);
if (path == null)
throw new NullPointerException("Invalid path!");
File file = new File(path);
if (!file.exists() || !file.isFile())
throw new FileNotFoundException("Invalid file!");
long fileSize = 0;
try {
fileSize = file.length();
} catch (Exception x) {
Log.e(TAG, "File length exception: " + x.getMessage());
}
listener.onUploadStarted(mimeType, file, fileSize);
RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(fileServer);
FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
RequestBody namePart = 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) {
listener.onProgressUpdate(progress, totalBytesSent, totalBytesToSend);
}
});
MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
Observable<Response<FileImageUpload>> uploadObservable = fileApi.sendImageFile(filePart, token, tokenIssuer, namePart);
long finalFileSize = fileSize;
return uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnError(error -> listener.onFailure(uniqueId + " - " + error.getMessage(), error)).subscribe(response -> {
if (response.isSuccessful() && response.body() != null) {
if (response.body().isHasError()) {
listener.onFailure(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), new PodChatException(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), uniqueId, token));
} else {
listener.onSuccess(response.body(), file, mimeType, finalFileSize);
}
} else {
if (response.body() != null) {
listener.onFailure(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), new PodChatException(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), uniqueId, token));
} else {
listener.onFailure(uniqueId + " - " + response.message(), new PodChatException(uniqueId + " - " + response.message(), uniqueId, token));
}
}
});
}
use of com.fanap.podchat.chat.Chat.TAG in project pod-chat-android-sdk by FanapSoft.
the class PodUploader method uploadFileToChatServer.
public static Subscription uploadFileToChatServer(String uniqueId, @NonNull Uri fileUri, Context context, String fileServer, String token, int tokenIssuer, IPodUploadFile listener) throws Exception {
if (fileUri.getPath() == null)
throw new NullPointerException("Invalid file uri!");
String mimeType = FileUtils.getMimeType(fileUri, context);
String path = FilePick.getSmartFilePath(context, fileUri);
if (path == null)
throw new NullPointerException("Invalid path!");
File file = new File(path);
if (!file.exists() || !file.isFile())
throw new FileNotFoundException("Invalid file!");
long fileSize = 0;
try {
fileSize = file.length();
} catch (Exception x) {
Log.e(TAG, "File length exception: " + x.getMessage());
}
listener.onUploadStarted(mimeType, file, fileSize);
RetrofitHelperFileServer retrofitHelperFileServer = new RetrofitHelperFileServer(fileServer);
FileApi fileApi = retrofitHelperFileServer.getService(FileApi.class);
RequestBody namePart = 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) {
listener.onProgressUpdate(progress, totalBytesSent, totalBytesToSend);
}
});
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
Observable<Response<FileUpload>> uploadObservable = fileApi.sendFile(filePart, token, tokenIssuer, namePart);
long finalFileSize = fileSize;
return uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnError(error -> listener.onFailure(uniqueId + " - " + error.getMessage(), error)).subscribe(response -> {
if (response.isSuccessful() && response.body() != null) {
if (response.body().isHasError()) {
listener.onFailure(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), new PodChatException(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), uniqueId, token));
} else {
listener.onSuccess(response.body(), file, mimeType, finalFileSize);
}
} else {
if (response.body() != null) {
listener.onFailure(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), new PodChatException(uniqueId + " - " + response.body().getMessage() + " - " + response.body().getReferenceNumber(), uniqueId, token));
} else {
listener.onFailure(uniqueId + " - " + response.message(), new PodChatException(uniqueId + " - " + response.message(), uniqueId, token));
}
}
});
}
Aggregations