Search in sources :

Example 1 with TAG

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));
            }
        }
    });
}
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)

Example 2 with TAG

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));
            }
        }
    });
}
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)2 BitmapFactory (android.graphics.BitmapFactory)2 Uri (android.net.Uri)2 NonNull (android.support.annotation.NonNull)2 Log (android.util.Log)2 UploadingQueueCache (com.fanap.podchat.cachemodel.queue.UploadingQueueCache)2 TAG (com.fanap.podchat.chat.Chat.TAG)2 FileUpload (com.fanap.podchat.mainmodel.FileUpload)2 ChatResponse (com.fanap.podchat.model.ChatResponse)2 FileImageUpload (com.fanap.podchat.model.FileImageUpload)2 ResultFile (com.fanap.podchat.model.ResultFile)2 ResultImageFile (com.fanap.podchat.model.ResultImageFile)2 ProgressRequestBody (com.fanap.podchat.networking.ProgressRequestBody)2 FileApi (com.fanap.podchat.networking.api.FileApi)2 RetrofitHelperFileServer (com.fanap.podchat.networking.retrofithelper.RetrofitHelperFileServer)2 ChatConstant (com.fanap.podchat.util.ChatConstant)2 FilePick (com.fanap.podchat.util.FilePick)2 FileUtils (com.fanap.podchat.util.FileUtils)2 PodChatException (com.fanap.podchat.util.PodChatException)2 Util (com.fanap.podchat.util.Util)2