file sharing backend

This commit is contained in:
Page Asgardius 2022-09-18 10:37:32 -07:00
parent 568352bde5
commit 89af00ba98
4 changed files with 116 additions and 15 deletions

View file

@ -19,6 +19,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.AsgardiusS3Manager"
tools:targetApi="31">
<activity
android:name=".Share"
android:exported="false" />
<activity
android:name=".Downloader"
android:configChanges="orientation|keyboardHidden|screenSize|uiMode|keyboardHidden"

View file

@ -307,13 +307,7 @@ public class ObjectSelect extends AppCompatActivity {
//Toast.makeText(ObjectSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show();
upload();
} else if (menuItem.getTitle() == getResources().getString(R.string.file_external)) {
try {
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, prefix + Name.get(position).toString());
URL objectURL = s3client.generatePresignedUrl(request);
share(objectURL.toString());
} catch (Exception e) {
Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
}
share(prefix + Name.get(position).toString());
} else if (menuItem.getTitle() == getResources().getString(R.string.file_del)) {
if (menuItem.getTitle() == getResources().getString(R.string.file_del)) {
if (Name.size() == 1 && treelevel >= 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);
}

View file

@ -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()));
}
}

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Share">
</androidx.constraintlayout.widget.ConstraintLayout>