Search in sources :

Example 1 with FileImageUpload

use of com.fanap.podchat.model.FileImageUpload 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));
            }
        }
    });
}
Also used : Context(android.content.Context) PodChatException(com.fanap.podchat.util.PodChatException) RetrofitHelperFileServer(com.fanap.podchat.networking.retrofithelper.RetrofitHelperFileServer) Uri(android.net.Uri) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) BitmapFactory(android.graphics.BitmapFactory) NonNull(android.support.annotation.NonNull) ResultFile(com.fanap.podchat.model.ResultFile) Response(retrofit2.Response) FileUpload(com.fanap.podchat.mainmodel.FileUpload) ChatConstant(com.fanap.podchat.util.ChatConstant) ChatResponse(com.fanap.podchat.model.ChatResponse) RequestBody(okhttp3.RequestBody) Observable(rx.Observable) Schedulers(rx.schedulers.Schedulers) FilePick(com.fanap.podchat.util.FilePick) Log(android.util.Log) MediaType(okhttp3.MediaType) TAG(com.fanap.podchat.chat.Chat.TAG) IOException(java.io.IOException) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody) FileApi(com.fanap.podchat.networking.api.FileApi) Util(com.fanap.podchat.util.Util) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ResultImageFile(com.fanap.podchat.model.ResultImageFile) FileImageUpload(com.fanap.podchat.model.FileImageUpload) MultipartBody(okhttp3.MultipartBody) Subscription(rx.Subscription) UploadingQueueCache(com.fanap.podchat.cachemodel.queue.UploadingQueueCache) FileUtils(com.fanap.podchat.util.FileUtils) FileNotFoundException(java.io.FileNotFoundException) PodChatException(com.fanap.podchat.util.PodChatException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) RetrofitHelperFileServer(com.fanap.podchat.networking.retrofithelper.RetrofitHelperFileServer) Response(retrofit2.Response) ChatResponse(com.fanap.podchat.model.ChatResponse) PodChatException(com.fanap.podchat.util.PodChatException) MultipartBody(okhttp3.MultipartBody) ResultFile(com.fanap.podchat.model.ResultFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody) FileApi(com.fanap.podchat.networking.api.FileApi) RequestBody(okhttp3.RequestBody) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody)

Aggregations

Context (android.content.Context)1 BitmapFactory (android.graphics.BitmapFactory)1 Uri (android.net.Uri)1 NonNull (android.support.annotation.NonNull)1 Log (android.util.Log)1 UploadingQueueCache (com.fanap.podchat.cachemodel.queue.UploadingQueueCache)1 TAG (com.fanap.podchat.chat.Chat.TAG)1 FileUpload (com.fanap.podchat.mainmodel.FileUpload)1 ChatResponse (com.fanap.podchat.model.ChatResponse)1 FileImageUpload (com.fanap.podchat.model.FileImageUpload)1 ResultFile (com.fanap.podchat.model.ResultFile)1 ResultImageFile (com.fanap.podchat.model.ResultImageFile)1 ProgressRequestBody (com.fanap.podchat.networking.ProgressRequestBody)1 FileApi (com.fanap.podchat.networking.api.FileApi)1 RetrofitHelperFileServer (com.fanap.podchat.networking.retrofithelper.RetrofitHelperFileServer)1 ChatConstant (com.fanap.podchat.util.ChatConstant)1 FilePick (com.fanap.podchat.util.FilePick)1 FileUtils (com.fanap.podchat.util.FileUtils)1 PodChatException (com.fanap.podchat.util.PodChatException)1 Util (com.fanap.podchat.util.Util)1