Search in sources :

Example 11 with FileApi

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));
}
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) IOException(java.io.IOException) 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) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody) FileApi(com.fanap.podchat.networking.api.FileApi) RequestBody(okhttp3.RequestBody) ProgressRequestBody(com.fanap.podchat.networking.ProgressRequestBody)

Aggregations

FileApi (com.fanap.podchat.networking.api.FileApi)11 File (java.io.File)11 IOException (java.io.IOException)10 MediaType (okhttp3.MediaType)9 ChatResponse (com.fanap.podchat.model.ChatResponse)8 ResultFile (com.fanap.podchat.model.ResultFile)8 ResultImageFile (com.fanap.podchat.model.ResultImageFile)8 ProgressRequestBody (com.fanap.podchat.networking.ProgressRequestBody)8 RetrofitHelperFileServer (com.fanap.podchat.networking.retrofithelper.RetrofitHelperFileServer)8 MultipartBody (okhttp3.MultipartBody)8 RequestBody (okhttp3.RequestBody)8 Response (retrofit2.Response)8 Subscription (rx.Subscription)8 PodChatException (com.fanap.podchat.util.PodChatException)7 Context (android.content.Context)6 BitmapFactory (android.graphics.BitmapFactory)6 Uri (android.net.Uri)6 NonNull (android.support.annotation.NonNull)6 Log (android.util.Log)6 UploadingQueueCache (com.fanap.podchat.cachemodel.queue.UploadingQueueCache)6