Search in sources :

Example 1 with RequestGetPodSpaceFile

use of com.fanap.podchat.requestobject.RequestGetPodSpaceFile in project pod-chat-android-sdk by FanapSoft.

the class ChatActivity method downloadFile.

private void downloadFile() {
    String url = "https://core.pod.ir/nzh/image?imageId=222808&hashCode=16c3cd2b93f-0.527719303638482";
    // image
    // long imageId = 404143;
    long imageId = 68349;
    long fileId = 68684;
    // String imageHashCode = "17024463974-0.5588549950419339";
    String imageHashCode = "16feadc7bf1-0.5248624596970302";
    String fileHashCode = "17077360d4b-0.2366487166443898";
    RequestGetImage requestGetImage = new RequestGetImage.Builder(imageId, imageHashCode, true).build();
    RequestGetFile requestGetFile = new RequestGetFile.Builder(fileId, fileHashCode, true).build();
    RequestGetPodSpaceFile rePod = new RequestGetPodSpaceFile.Builder("GAUPE1ZRK76VOXBS").build();
    RequestGetPodSpaceImage rePodImage = new RequestGetPodSpaceImage.Builder("613Q7WCCEXZ1DGY5").setQuality(0.45f).build();
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_WRITE_EXTERNAL_STORAGE);
        return;
    }
    downloadingId = presenter.downloadFile(rePodImage, new ProgressHandler.IDownloadFile() {

        @Override
        public void onProgressUpdate(String uniqueId, long bytesDownloaded, long totalBytesToDownload) {
            Log.e("DOWNLOAD", "IN ACTIVITY: " + "Downloaded: " + bytesDownloaded + " Left: " + totalBytesToDownload);
        }

        @Override
        public void onProgressUpdate(String uniqueId, int progress) {
            Log.e("DOWNLOAD", "IN ACTIVITY: " + "Progress: " + progress);
        }

        @Override
        public void onError(String uniqueId, String error, String url) {
            Log.e("DOWNLOAD", "IN ACTIVITY: ERROR :(((");
        }

        @Override
        public void onLowFreeSpace(String uniqueId, String url) {
            Log.e("DOWNLOAD", "Low Space...");
        }

        @Override
        public void onFileReady(ChatResponse<ResultDownloadFile> response) {
            Log.e("DOWNLOAD", "IN ACTIVITY: Finish File!!!!");
            Log.e("DOWNLOAD", "File name: " + response.getResult().getFile().getName());
            Log.e("DOWNLOAD", "Uri " + response.getResult().getUri());
            Log.e("DOWNLOAD", "File Exist " + response.getResult().getFile().exists());
            if (response.getResult().getFile().exists()) {
                try {
                    Bitmap v = BitmapFactory.decodeFile(response.getResult().getFile().getAbsolutePath());
                    runOnUiThread(() -> {
                    // imageMap.setImageBitmap(v)
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("DOWNLOAD", "Not Image");
                }
            }
        }
    });
// downloadingId = presenter.downloadFile(requestGetImage, new ProgressHandler.IDownloadFile() {
// 
// 
// @Override
// public void onProgressUpdate(String uniqueId, int bytesDownloaded, int totalBytesToDownload) {
// Log.e("DOWNLOAD", "IN ACTIVITY: " + "Downloaded: " + bytesDownloaded + " Left: " + totalBytesToDownload);
// 
// }
// 
// @Override
// public void onProgressUpdate(String uniqueId, int progress) {
// 
// }
// 
// @Override
// public void onError(String uniqueId, String error, String url) {
// Log.e("DOWNLOAD", "IN ACTIVITY: ERROR :(((");
// 
// }
// 
// @Override
// public void onLowFreeSpace(String uniqueId, String url) {
// Log.e("DOWNLOAD", "Low Space...");
// 
// }
// 
// @Override
// public void onFileReady(ChatResponse<ResultDownloadFile> response) {
// Log.e("DOWNLOAD", "IN ACTIVITY: Finish File!!!!");
// Log.e("DOWNLOAD", "File name: " + response.getResult().getFile().getName());
// Log.e("DOWNLOAD", "Uri " + response.getResult().getUri());
// Log.e("DOWNLOAD", "File Exist " + response.getResult().getFile().exists());
// 
// 
// if (response.getResult().getFile().exists()) {
// 
// 
// try {
// Bitmap v = BitmapFactory.decodeFile(response.getResult().getFile().getAbsolutePath());
// runOnUiThread(() -> imageMap.setImageBitmap(v));
// } catch (Exception e) {
// e.printStackTrace();
// }
// 
// }
// 
// 
// }
// 
// });
}
Also used : RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) GsonBuilder(com.google.gson.GsonBuilder) RequestGetImage(com.fanap.podchat.requestobject.RequestGetImage) RequestGetPodSpaceImage(com.fanap.podchat.requestobject.RequestGetPodSpaceImage) Bitmap(android.graphics.Bitmap) ChatResponse(com.fanap.podchat.model.ChatResponse) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile)

Example 2 with RequestGetPodSpaceFile

use of com.fanap.podchat.requestobject.RequestGetPodSpaceFile in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method getFile.

/**
 * @param request         desired file hashCode
 * @param progressHandler callbacks for downloading progress.
 * @return uniqueId of request.
 */
public String getFile(RequestGetPodSpaceFile request, ProgressHandler.IDownloadFile progressHandler) {
    String uniqueId = generateUniqueId();
    String url = getPodSpaceFileUrl(request.getHashCode());
    showLog("DOWNLOAD FILE: " + url);
    PodDownloader.IDownloaderError downloaderErrorInterface = getDownloaderErrorInterface(progressHandler, uniqueId, url);
    File destinationFolder;
    if (cache && request.canUseCache()) {
        destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.FILES) : FileUtils.getOrCreateDirectory(FileUtils.FILES);
    } else {
        destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.FILES) : FileUtils.getPublicFilesDirectory();
    }
    showLog("Save in folder: " + destinationFolder);
    String fileName = "file_" + request.getHashCode();
    if (destinationFolder == null) {
        showErrorLog("Error Creating destination folder");
        progressHandler.onError(uniqueId, ChatConstant.ERROR_WRITING_FILE, url);
        return uniqueId;
    }
    File cachedFile = FileUtils.findFileInFolder(destinationFolder, fileName);
    if (cachedFile != null && cachedFile.isFile() && request.canUseCache()) {
        showLog("File Exist in cache: " + cachedFile);
        // file exists
        ChatResponse<ResultDownloadFile> response = PodDownloader.generatePodSpaceDownloadResult(request.getHashCode(), cachedFile);
        progressHandler.onFileReady(response);
        return uniqueId;
    }
    // only url should return in callback
    if (!hasFreeSpace) {
        showErrorLog("Download couldn't start. cause: LOW FREE SPACE");
        progressHandler.onLowFreeSpace(uniqueId, url);
        return uniqueId;
    }
    if (chatReady) {
        showLog("Download Started", request.toString());
        Call call = PodDownloader.downloadFileFromPodSpace(new ProgressHandler.IDownloadFile() {

            @Override
            public void onError(String mUniqueId, String error, String mUrl) {
                progressHandler.onError(uniqueId, error, url);
                showErrorLog("Download Error. cause: " + error);
            }

            @Override
            public void onProgressUpdate(String mUniqueId, long bytesDownloaded, long totalBytesToDownload) {
                progressHandler.onProgressUpdate(uniqueId, bytesDownloaded, totalBytesToDownload);
                if (totalBytesToDownload > checkFreeSpace()) {
                    showErrorLog("Total file space is more than free space");
                    progressHandler.onLowFreeSpace(uniqueId, url);
                }
            }

            @Override
            public void onProgressUpdate(String mUniqueId, int progress) {
                progressHandler.onProgressUpdate(uniqueId, progress);
            }

            @Override
            public void onFileReady(ChatResponse<ResultDownloadFile> response) {
                progressHandler.onFileReady(response);
                showLog("Download is complete!");
            }
        }, getToken(), TOKEN_ISSUER, request.getHashCode(), getPodSpaceServer(), fileName, destinationFolder, downloaderErrorInterface);
        downloadCallList.put(uniqueId, call);
    } else
        onChatNotReady(uniqueId);
    return uniqueId;
}
Also used : PodDownloader(com.fanap.podchat.chat.file_manager.download_file.PodDownloader) Call(retrofit2.Call) ProgressHandler(com.fanap.podchat.ProgressHandler) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile)

Example 3 with RequestGetPodSpaceFile

use of com.fanap.podchat.requestobject.RequestGetPodSpaceFile in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method isAvailableInCache.

/**
 * @param request contains desired file hashcode
 * @return true if file exist in cache
 */
public boolean isAvailableInCache(RequestGetPodSpaceFile request) {
    File destinationFolder;
    if (cache && request.canUseCache()) {
        destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.FILES) : FileUtils.getOrCreateDirectory(FileUtils.FILES);
    } else {
        destinationFolder = FileUtils.getDownloadDirectory() != null ? FileUtils.getOrCreateDownloadDirectory(FileUtils.FILES) : FileUtils.getPublicFilesDirectory();
    }
    showLog("Save in folder: " + destinationFolder);
    String fileName = "file_" + request.getHashCode();
    if (destinationFolder == null) {
        showErrorLog("Error Creating destination folder");
        return false;
    }
    File cachedFile = FileUtils.findFileInFolder(destinationFolder, fileName);
    if (cachedFile != null && cachedFile.isFile() && request.canUseCache()) {
        showLog("File Exist in cache: " + cachedFile);
        return true;
    }
    return false;
}
Also used : MetaDataFile(com.fanap.podchat.model.MetaDataFile) RequestUploadFile(com.fanap.podchat.requestobject.RequestUploadFile) ResultDownloadFile(com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile) ResultFile(com.fanap.podchat.model.ResultFile) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile) RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) RequestCreateThreadWithFile(com.fanap.podchat.requestobject.RequestCreateThreadWithFile) File(java.io.File) ResultImageFile(com.fanap.podchat.model.ResultImageFile)

Aggregations

RequestGetFile (com.fanap.podchat.requestobject.RequestGetFile)3 RequestGetPodSpaceFile (com.fanap.podchat.requestobject.RequestGetPodSpaceFile)3 ResultDownloadFile (com.fanap.podchat.chat.file_manager.download_file.model.ResultDownloadFile)2 MetaDataFile (com.fanap.podchat.model.MetaDataFile)2 ResultFile (com.fanap.podchat.model.ResultFile)2 ResultImageFile (com.fanap.podchat.model.ResultImageFile)2 RequestCreateThreadWithFile (com.fanap.podchat.requestobject.RequestCreateThreadWithFile)2 RequestUploadFile (com.fanap.podchat.requestobject.RequestUploadFile)2 File (java.io.File)2 Bitmap (android.graphics.Bitmap)1 ProgressHandler (com.fanap.podchat.ProgressHandler)1 PodDownloader (com.fanap.podchat.chat.file_manager.download_file.PodDownloader)1 ChatResponse (com.fanap.podchat.model.ChatResponse)1 RequestGetImage (com.fanap.podchat.requestobject.RequestGetImage)1 RequestGetPodSpaceImage (com.fanap.podchat.requestobject.RequestGetPodSpaceImage)1 GsonBuilder (com.google.gson.GsonBuilder)1 Call (retrofit2.Call)1