calculating folder size

This commit is contained in:
Page Asgardius 2022-12-30 20:13:34 -07:00
parent 4b0651c9c6
commit 6e1f171f7b
3 changed files with 86 additions and 2 deletions

View file

@ -62,7 +62,7 @@ public class Uploader extends AppCompatActivity {
DocumentFile document; DocumentFile document;
Intent intent; Intent intent;
Button fileUpload; Button fileUpload;
Thread uploadFile, uploadProgress; Thread uploadFile, uploadProgress, calculateProgress;
boolean style, isfolder; boolean style, isfolder;
boolean started = false; boolean started = false;
boolean cancel = false; boolean cancel = false;
@ -363,7 +363,9 @@ public class Uploader extends AppCompatActivity {
fprefix.setText(prefix); fprefix.setText(prefix);
fprefix.setHint(getResources().getString(R.string.upload_prefix)); fprefix.setHint(getResources().getString(R.string.upload_prefix));
fprefixlabel.setText(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 { } else {
fprefix.setText(prefix+getDisplayName(fileuri)); fprefix.setText(prefix+getDisplayName(fileuri));
} }
@ -464,4 +466,84 @@ public class Uploader extends AppCompatActivity {
s3client.completeMultipartUpload(compRequest); 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<String> filepath = new ArrayList<String>();
int treelevel = 0;
ArrayList<Integer> fileindex = new ArrayList<Integer>();
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();
}
} }

View file

@ -114,4 +114,5 @@
<string name="batch_download_success">Archivos descargados exitosamente</string> <string name="batch_download_success">Archivos descargados exitosamente</string>
<string name="copy_name">Copiar nombre al portapapeles</string> <string name="copy_name">Copiar nombre al portapapeles</string>
<string name="copy_name_ok">Nombre copiado al portapapeles</string> <string name="copy_name_ok">Nombre copiado al portapapeles</string>
<string name="upload_calculate">Calculando tamaño de la carpeta</string>
</resources> </resources>

View file

@ -117,4 +117,5 @@
<string name="cors_ok">CORS policy changed successfully</string> <string name="cors_ok">CORS policy changed successfully</string>
<string name="copy_name">Copy name to clipboard</string> <string name="copy_name">Copy name to clipboard</string>
<string name="copy_name_ok">Name copied to clipboard</string> <string name="copy_name_ok">Name copied to clipboard</string>
<string name="upload_calculate">Calculating folder size</string>
</resources> </resources>