From 89af00ba9890fba6aa5e564cc08b674f6c90988e Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Sun, 18 Sep 2022 10:37:32 -0700 Subject: [PATCH] file sharing backend --- app/src/main/AndroidManifest.xml | 3 + .../page/s3manager/ObjectSelect.java | 25 ++--- .../java/asgardius/page/s3manager/Share.java | 94 +++++++++++++++++++ app/src/main/res/layout/activity_share.xml | 9 ++ 4 files changed, 116 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/asgardius/page/s3manager/Share.java create mode 100644 app/src/main/res/layout/activity_share.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6a5e6f5..894774b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,6 +19,9 @@ android:supportsRtl="true" android:theme="@style/Theme.AsgardiusS3Manager" tools:targetApi="31"> + = 1) { @@ -383,14 +377,15 @@ public class ObjectSelect extends AppCompatActivity { private void share(String object) { - try { - - Intent shareIntent = new Intent(Intent.ACTION_VIEW); - shareIntent.setData(Uri.parse(object)); - startActivity(Intent.createChooser(shareIntent, "choose one")); - } catch(Exception e) { - Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); - } + Intent intent = new Intent(this, Share.class); + //treelevel ++; + intent.putExtra("endpoint", endpoint); + intent.putExtra("username", username); + intent.putExtra("password", password); + intent.putExtra("bucket", bucket); + intent.putExtra("object", object); + intent.putExtra("region", location); + startActivity(intent); } diff --git a/app/src/main/java/asgardius/page/s3manager/Share.java b/app/src/main/java/asgardius/page/s3manager/Share.java new file mode 100644 index 0000000..5a01179 --- /dev/null +++ b/app/src/main/java/asgardius/page/s3manager/Share.java @@ -0,0 +1,94 @@ +package asgardius.page.s3manager; + +import androidx.appcompat.app.AppCompatActivity; + +import android.os.Bundle; +import android.widget.Toast; + +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.regions.Region; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.S3ClientOptions; +import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; + +import java.net.URL; +import java.util.Calendar; +import java.util.Date; + +public class Share extends AppCompatActivity { + String username, password, endpoint, bucket, object, location; + Region region; + S3ClientOptions s3ClientOptions; + AWSCredentials myCredentials; + AmazonS3 s3client; + Calendar mycal; + + public static String URLify(String str) { + str = str.trim(); + int length = str.length(); + int trueL = length; + if(str.contains(" ")) { + for(int i = 0; i < length; i++) { + if(str.charAt(i) == ' ') { + trueL = trueL + 2; + } + } + char[] oldArr = str.toCharArray(); + char[] newArr = new char[trueL]; + int x = 0; + for(int i = 0; i < length; i++) { + if(oldArr[i] == ' ') { + newArr[x] = '%'; + newArr[x+1] = '2'; + newArr[x+2] = '0'; + x += 3; + } else { + newArr[x] = oldArr[i]; + x++; + } + } + str = new String(newArr, 0, trueL); + } + return str; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_share); + mycal = Calendar.getInstance(); + endpoint = getIntent().getStringExtra("endpoint"); + username = getIntent().getStringExtra("username"); + password = getIntent().getStringExtra("password"); + bucket = getIntent().getStringExtra("bucket"); + location = getIntent().getStringExtra("region"); + object = getIntent().getStringExtra("object"); + region = Region.getRegion(location); + s3ClientOptions = S3ClientOptions.builder().build(); + myCredentials = new BasicAWSCredentials(username, password); + try { + s3client = new AmazonS3Client(myCredentials, region); + } catch (Exception e) { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show(); + } + s3client.setEndpoint(endpoint); + if (!endpoint.contains(getResources().getString(R.string.aws_endpoint))) { + s3ClientOptions.setPathStyleAccess(true); + } + s3client.setS3ClientOptions(s3ClientOptions); + mycal.set(Calendar.YEAR, 2022); + mycal.set(Calendar.MONTH, 8); + mycal.set(Calendar.DATE, 18); + mycal.set(Calendar.HOUR, 14); + mycal.set(Calendar.MINUTE, 15); + mycal.set(Calendar.SECOND, 28); + Date expiration = mycal.getTime(); + System.out.println(expiration); + + GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, object).withExpiration(expiration); + URL objectURL = s3client.generatePresignedUrl(request); + System.out.println(URLify(objectURL.toString())); + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_share.xml b/app/src/main/res/layout/activity_share.xml new file mode 100644 index 0000000..168ee50 --- /dev/null +++ b/app/src/main/res/layout/activity_share.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file