From 3413ac47554a85f362be9d7905b37bea908d3d7a Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Sat, 10 Sep 2022 13:10:35 -0700 Subject: [PATCH] file delete --- .../page/s3manager/ObjectSelect.java | 123 +++++++++++++++++- app/src/main/res/values/strings.xml | 5 + 2 files changed, 122 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java b/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java index 31bd839..79a9d84 100644 --- a/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java +++ b/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java @@ -3,10 +3,12 @@ package asgardius.page.s3manager; import static com.amazonaws.regions.Regions.US_EAST_1; import static com.amazonaws.regions.Regions.US_WEST_1; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -23,6 +25,7 @@ import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.S3ClientOptions; import com.amazonaws.services.s3.model.Bucket; +import com.amazonaws.services.s3.model.DeleteObjectRequest; import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; import com.amazonaws.services.s3.model.ListObjectsRequest; import com.amazonaws.services.s3.model.ListObjectsV2Request; @@ -45,6 +48,10 @@ public class ObjectSelect extends AppCompatActivity { String username, password, endpoint, bucket, prefix, location; int treelevel; String[] filename; + Region region; + S3ClientOptions s3ClientOptions; + AWSCredentials myCredentials; + AmazonS3 s3client; @Override protected void onCreate(Bundle savedInstanceState) { @@ -57,10 +64,10 @@ public class ObjectSelect extends AppCompatActivity { prefix = getIntent().getStringExtra("prefix"); treelevel = getIntent().getIntExtra("treelevel", 0); setContentView(R.layout.activity_object_select); - Region region = Region.getRegion(location); - S3ClientOptions s3ClientOptions = S3ClientOptions.builder().build(); - AWSCredentials myCredentials = new BasicAWSCredentials(username, password); - AmazonS3 s3client = new AmazonS3Client(myCredentials, region); + region = Region.getRegion(location); + s3ClientOptions = S3ClientOptions.builder().build(); + myCredentials = new BasicAWSCredentials(username, password); + s3client = new AmazonS3Client(myCredentials, region); s3client.setEndpoint(endpoint); if (!endpoint.contains(getResources().getString(R.string.aws_endpoint))) { s3ClientOptions.setPathStyleAccess(true); @@ -240,7 +247,11 @@ public class ObjectSelect extends AppCompatActivity { // Toast message on menu item clicked //Toast.makeText(MainActivity.this, "You Clicked " + menuItem.getTitle(), Toast.LENGTH_SHORT).show(); if (menuItem.getTitle() == getResources().getString(R.string.file_del)) { - Toast.makeText(ObjectSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); + if (Name.size() == 1 && treelevel >= 1) { + Toast.makeText(ObjectSelect.this, getResources().getString(R.string.only_item_onlist), Toast.LENGTH_SHORT).show(); + } else { + delete(prefix + Name.get(position).toString(), true); + } } return true; } @@ -263,7 +274,13 @@ public class ObjectSelect extends AppCompatActivity { URL objectURL = s3client.generatePresignedUrl(request); share(objectURL.toString()); } else if (menuItem.getTitle() == getResources().getString(R.string.file_del)) { - Toast.makeText(ObjectSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); + if (menuItem.getTitle() == getResources().getString(R.string.file_del)) { + if (Name.size() == 1 && treelevel >= 1) { + Toast.makeText(ObjectSelect.this, getResources().getString(R.string.only_item_onlist), Toast.LENGTH_SHORT).show(); + } else { + delete(prefix + Name.get(position).toString(), false); + } + } } return true; } @@ -333,4 +350,98 @@ public class ObjectSelect extends AppCompatActivity { } } + + private void delete(String object, boolean folder) { + AlertDialog.Builder builder = new AlertDialog.Builder(ObjectSelect.this); + builder.setCancelable(true); + builder.setTitle(getResources().getString(R.string.file_del)); + if (folder) { + builder.setMessage(getResources().getString(R.string.folder_del_confirm)); + } else { + builder.setMessage(getResources().getString(R.string.file_del_confirm)); + } + builder.setPositiveButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + //Toast.makeText(ObjectSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); + Thread deleteobject = new Thread(new Runnable() { + + @Override + public void run() { + try { + //Your code goes here + //List buckets = s3client.listBuckets(); + if (folder) { + ListObjectsRequest orequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(object).withMaxKeys(2); + //List objects = (List) s3client.listObjects(bucket, "/"); + ObjectListing result = s3client.listObjects(orequest); + ArrayList object = new ArrayList(); + List objects = result.getObjectSummaries(); + boolean nextbatch = false; + while (result.isTruncated() || !nextbatch) { + if (nextbatch) { + result = s3client.listNextBatchOfObjects (result); + objects = result.getObjectSummaries(); + } else { + nextbatch = true; + } + for (S3ObjectSummary os : objects) { + object.add(os.getKey()); + + //i++; + } + + } + //System.out.println(object); + + } else { + DeleteObjectRequest deleteObjectRequest =new DeleteObjectRequest(bucket, object); + s3client.deleteObject(deleteObjectRequest); + } + + //System.out.println(Name); + + runOnUiThread(new Runnable() { + + @Override + public void run() { + // Sending reference and data to Adapter + if (folder) { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.folder_del_success), Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.file_del_success), Toast.LENGTH_SHORT).show(); + } + recreate(); + + } + }); + //System.out.println("tree "+treelevel); + //System.out.println("prefix "+prefix); + + } catch (Exception e) { + e.printStackTrace(); + runOnUiThread(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + } + }); + //Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + finish(); + } + } + }); + deleteobject.start(); + } + }); + builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } } \ 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 d9335f3..7ef9869 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,6 +13,11 @@ This feature is not implemented yet Open in Delete + Do you want to delete this file? + Do you want to delete this folder? + Folder deleted successfully + File deleted successfully + This is the only item on this folder, you should delete the parent folder instead Please choose an account Add Account Test credentials