file delete

This commit is contained in:
Page Asgardius 2022-09-10 13:10:35 -07:00
parent 42081e7c15
commit 3413ac4755
2 changed files with 122 additions and 6 deletions

View file

@ -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<Bucket> buckets = s3client.listBuckets();
if (folder) {
ListObjectsRequest orequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(object).withMaxKeys(2);
//List<S3Object> objects = (List<S3Object>) s3client.listObjects(bucket, "/");
ObjectListing result = s3client.listObjects(orequest);
ArrayList<String> object = new ArrayList<String>();
List<S3ObjectSummary> 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();
}
}

View file

@ -13,6 +13,11 @@
<string name="pending_feature">This feature is not implemented yet</string>
<string name="file_external">Open in</string>
<string name="file_del">Delete</string>
<string name="file_del_confirm">Do you want to delete this file?</string>
<string name="folder_del_confirm">Do you want to delete this folder?</string>
<string name="folder_del_success">Folder deleted successfully</string>
<string name="file_del_success">File deleted successfully</string>
<string name="only_item_onlist">This is the only item on this folder, you should delete the parent folder instead</string>
<string name="accountselect">Please choose an account</string>
<string name="accountadd_button">Add Account</string>
<string name="accounttest_button">Test credentials</string>