From 6e1f171f7b3192becc988d0b7ecef95bde8bd78d Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Fri, 30 Dec 2022 20:13:34 -0700 Subject: [PATCH] calculating folder size --- .../asgardius/page/s3manager/Uploader.java | 86 ++++++++++++++++++- app/src/main/res/values-es/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 3 files changed, 86 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/asgardius/page/s3manager/Uploader.java b/app/src/main/java/asgardius/page/s3manager/Uploader.java index 3db51cd..56e1590 100644 --- a/app/src/main/java/asgardius/page/s3manager/Uploader.java +++ b/app/src/main/java/asgardius/page/s3manager/Uploader.java @@ -62,7 +62,7 @@ public class Uploader extends AppCompatActivity { DocumentFile document; Intent intent; Button fileUpload; - Thread uploadFile, uploadProgress; + Thread uploadFile, uploadProgress, calculateProgress; boolean style, isfolder; boolean started = false; boolean cancel = false; @@ -363,7 +363,9 @@ public class Uploader extends AppCompatActivity { fprefix.setText(prefix); fprefix.setHint(getResources().getString(R.string.upload_prefix)); fprefixlabel.setText(getResources().getString(R.string.upload_prefix)); - fileUpload.setText(getResources().getString(R.string.batch_upload_button)); + fileUpload.setText(getResources().getString(R.string.upload_calculate)); + fileUpload.setEnabled(false); + calculateSize(); } else { fprefix.setText(prefix+getDisplayName(fileuri)); } @@ -464,4 +466,84 @@ public class Uploader extends AppCompatActivity { s3client.completeMultipartUpload(compRequest); } + private void calculateSize() { + calculateProgress = new Thread(new Runnable() { + + @Override + public void run() { + try { + //Your code goes here + document = DocumentFile.fromTreeUri(getApplicationContext(), fileuri); + DocumentFile[] filelist = document.listFiles(); + ArrayList filepath = new ArrayList(); + int treelevel = 0; + ArrayList fileindex = new ArrayList(); + fileindex.add(0); + for (int i = 0; i < filelist.length; i++) { + filepath.add(filelist[i].getName()); + if(filelist[i].isDirectory()) { + treelevel++; + fileindex.add(0); + document = filelist[i]; + filelist = document.listFiles(); + while (treelevel >= 1 && fileindex.get(treelevel) < filelist.length+1) { + if(fileindex.get(treelevel) == filelist.length) { + fileindex.remove(treelevel); + document = document.getParentFile(); + filelist = document.listFiles(); + treelevel--; + filepath.remove(treelevel); + fileindex.set(treelevel, fileindex.get(treelevel)+1); + } else { + filepath.add(filelist[fileindex.get(treelevel)].getName()); + if (filelist[fileindex.get(treelevel)].isDirectory()) { + document = filelist[fileindex.get(treelevel)]; + filelist = document.listFiles(); + treelevel++; + fileindex.add(0); + } else { + filepath.remove(treelevel); + if(filelist[fileindex.get(treelevel)].length()%MAX_SINGLE_PART_UPLOAD_BYTES == 0) { + filesize = filesize+(filelist[fileindex.get(treelevel)].length()/MAX_SINGLE_PART_UPLOAD_BYTES); + } else { + filesize = filesize+((filelist[fileindex.get(treelevel)].length()/MAX_SINGLE_PART_UPLOAD_BYTES)+1); + } + fileindex.set(treelevel, fileindex.get(treelevel)+1); + } + } + } + //document = document.getParentFile(); + //filelist = document.listFiles(); + //treelevel--; + } else { + if(filelist[i].length()%MAX_SINGLE_PART_UPLOAD_BYTES == 0) { + filesize = filesize+(filelist[i].length()/MAX_SINGLE_PART_UPLOAD_BYTES); + } else { + filesize = filesize+((filelist[i].length()/MAX_SINGLE_PART_UPLOAD_BYTES)+1); + } + } + filepath.clear(); + fileindex.clear(); + fileindex.add(0); + } + + runOnUiThread(new Runnable() { + + @Override + public void run() { + fileUpload.setText(getResources().getString(R.string.batch_upload_button)); + fileUpload.setEnabled(true); + } + }); + + } catch (Exception e) { + e.printStackTrace(); + //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + //finish(); + } + } + }); + calculateProgress.start(); + } + } \ No newline at end of file diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index e722de1..302ec5e 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -114,4 +114,5 @@ Archivos descargados exitosamente Copiar nombre al portapapeles Nombre copiado al portapapeles + Calculando tamaƱo de la carpeta \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 32cc62f..5995c05 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -117,4 +117,5 @@ CORS policy changed successfully Copy name to clipboard Name copied to clipboard + Calculating folder size \ No newline at end of file