koush / ion

Android Asynchronous Networking and Image Loading
Other
6.29k stars 1.03k forks source link

Downloading files from wrong url #640

Open zavernutiy opened 9 years ago

zavernutiy commented 9 years ago

I download files from server and when I pass the url for file that is not on the server it is saved on sd card anyway. To be clear, let's assume I have following url: https://example.com/img/img_3.jpg but there is no such file. So actually this url is 404 Not Found, but ion creates a file img_3.jpg on my SD card. When I open the image it is blank. I've tried to check if downloaded file is empty, but it's not. So is there any possibility to forbid ion to download from not existing URL. Here is my code:

private void executeDownload(final FilesToDownload downloadFiles) {
        if (downloading != null && !downloading.isCancelled()) {
            resetDownload();
            return;
        }
        FileAndDirName fileAndDir = downloadFiles.popFileAndDirName();
        final int size = downloadFiles.getFilesAndDirSize();
        final String fileName = fileAndDir.getFileName();
        final String dirName = fileAndDir.getDirName();
        String url = mServerUrl + dirName + "/" + fileName;

        File dir = new File(root.getAbsolutePath() + "/seatconnect/" + dirName);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        final File destinationFile = new File(dir, fileName);

        downloading = Ion.with(getActivity())
                .load(url)
                        // attach the percentage report to a progress bar.
                        // can also attach to a ProgressDialog with progressDialog.
                .progressBar(mProgressBar)
                .progressDialog(mProgressDialog)
                        // callbacks on progress can happen on the UI thread
                        // via progressHandler. This is useful if you need to update a TextView.
                        // Updates to TextViews MUST happen on the UI thread.
                .progressHandler(new ProgressCallback() {
                    @Override
                    public void onProgress(long downloaded, long total) {
//                        mProgressDialog.setProgress((int) downloaded);
                    }
                })
                        // write to a file
                .write(destinationFile)
                        // run a callback on completion
                .setCallback(new FutureCallback<File>() {
                    @Override
                    public void onCompleted(Exception e, File result) {
                        resetDownload();
                        if (e != null) {
                            Toast.makeText(getActivity(), "Error downloading file " + fileName, Toast.LENGTH_SHORT).show();
//                            return;
                        } else {
                            Toast.makeText(getActivity(), "File download complete " + fileName, Toast.LENGTH_SHORT).show();
                        }

                        if (result.exists() && result.length() == 0) {
                            String message = result.delete() ? "Deleted empty file " : "The file is not empty ";
                            message += fileName;
                            Log.d(TAG, message);
                        }

                        if (size != 0) {
                            mProgressDialog.show();
                            executeDownload(downloadFiles);
                            return;
                        }

                        mProgressBar.setVisibility(View.INVISIBLE);
                        if (mProgressDialog.isShowing()) {
                            mProgressDialog.dismiss();
                        }
                        mNewsFooterCheckForUpdateButton.setVisibility(View.VISIBLE);
                        mNewsFooterUpdateButton.setVisibility(View.INVISIBLE);
                    }
                });
}
muhammad-naderi commented 9 years ago

+1 same here