use of com.fanap.podchat.networking.api.FileApi in project pod-chat-android-sdk by FanapSoft.
the class PodUploader method uploadPublicFileToPodSpace.
private static Subscription uploadPublicFileToPodSpace(String uniqueId, String fileServer, String token, int tokenIssuer, boolean isPublic, IPodUploadFileToPodSpace listener, String mimeType, File file, long fileSize) {
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) {
if (progress < 95)
listener.onProgressUpdate(progress, totalBytesSent, totalBytesToSend);
}
});
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
Observable<Response<UploadToPodSpaceResponse>> uploadObservable = fileApi.uploadPublicFileToPodSpace(filePart, token, tokenIssuer, namePart, isPublic);
return uploadObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).doOnError(error -> listener.onFailure(error.getMessage(), error)).subscribe(response -> {
if (response.isSuccessful() && response.body() != null) {
if (response.body().isHasError()) {
listener.onFailure(response.body().getMessage(), new PodChatException(response.body().getMessage(), uniqueId, token));
return;
}
listener.onProgressUpdate(100, (int) fileSize, 0);
listener.onSuccess(response.body().getUploadToPodSpaceResult(), file, mimeType, fileSize);
} else {
try {
if (response.errorBody() != null) {
listener.onFailure(response.errorBody().string(), new PodChatException(response.errorBody().string(), uniqueId, token));
} else {
listener.onFailure(response.message(), new PodChatException(response.message(), uniqueId, token));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}, error -> listener.onFailure(error.getMessage(), error));
}
Aggregations