KhunHtetzNaing / xGetter

Android library for extract stream/download url from Google Drive,MegaUp,Google Photos,Mp4Upload,Facebook,Mediafire,Ok.Ru,VK,Twitter,Youtube,SolidFiles,Vidoza,UptoStream,SendVid,FanSubs,Uptobox,FEmbed,FileRio
Apache License 2.0
177 stars 95 forks source link

Please add file size to XModel #37

Closed lwinmoehein closed 4 years ago

lwinmoehein commented 4 years ago

It would be perfect if there is a way to get the size of file and it can be retrieved easily with XModel.

KhunHtetzNaing commented 4 years ago

It's will affect process time, because of some sites need to get content length manual. So if you want to get the size of a file, you can check like this

private void checkFileSize(XModel xModel){
        new AsyncTask<Void,Void,String>(){
            @Override
            protected String doInBackground(Void... voids) {
                if (xModel.getUrl()!=null) {
                    try {
                        URLConnection connection = new URL(xModel.getUrl()).openConnection();
                        if (xModel.getCookie() != null) {
                            connection.setRequestProperty("Cookie", xModel.getCookie());
                        }
                        connection.connect();
                        return calculateFileSize(connection.getContentLength());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if(s!=null){
                    //Your file size => 
                    System.out.println("File Size=> "+s);
                }
            }
        }.execute();
    }

    private String calculateFileSize(long size) {
        if(size <= 0) return "0";
        final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
        int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
lwinmoehein commented 4 years ago

I worked fine bro,thanks a lot ,it's really helpful