From 7223d9333fa43e7c870559d477426b9a09c7c356 Mon Sep 17 00:00:00 2001 From: Page Asgardius Date: Mon, 2 Jan 2023 17:03:58 -0700 Subject: [PATCH] copy links --- .../page/s3manager/BucketSelect.java | 26 +++- .../page/s3manager/ObjectSelect.java | 13 +- .../java/asgardius/page/s3manager/Share.java | 123 +++++++++++++++++- app/src/main/res/layout/activity_share.xml | 17 +++ app/src/main/res/menu/bucket_menu.xml | 4 + app/src/main/res/menu/folder_menu.xml | 4 + app/src/main/res/values-es/strings.xml | 6 +- app/src/main/res/values/strings.xml | 6 +- 8 files changed, 180 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/asgardius/page/s3manager/BucketSelect.java b/app/src/main/java/asgardius/page/s3manager/BucketSelect.java index c3d660f..39a7163 100644 --- a/app/src/main/java/asgardius/page/s3manager/BucketSelect.java +++ b/app/src/main/java/asgardius/page/s3manager/BucketSelect.java @@ -1,8 +1,5 @@ package asgardius.page.s3manager; -import static android.media.MediaExtractor.MetricsConstants.MIME_TYPE; -import static com.amazonaws.regions.Regions.US_EAST_1; - import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; @@ -13,7 +10,6 @@ import android.content.ClipboardManager; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.view.MenuItem; @@ -29,7 +25,6 @@ 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.DeleteObjectsRequest; import com.amazonaws.services.s3.model.ListObjectsRequest; import com.amazonaws.services.s3.model.ObjectListing; @@ -171,6 +166,8 @@ public class BucketSelect extends AppCompatActivity { //Toast.makeText(BucketSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); upload(Name.get(position).toString(), true); + } else if (menuItem.getTitle() == getResources().getString(R.string.create_link)) { + share(Name.get(position).toString()); } else if (menuItem.getTitle() == getResources().getString(R.string.download_bucket)) { //Toast.makeText(BucketSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); download(Name.get(position).toString()); @@ -223,6 +220,23 @@ public class BucketSelect extends AppCompatActivity { } + private void share(String bucket) { + + 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("title", bucket); + intent.putExtra("region", location); + intent.putExtra("videotime", videotime); + intent.putExtra("playlisttime", playlisttime); + intent.putExtra("style", style); + startActivity(intent); + + } + private void delete(String bucket) { AlertDialog.Builder builder = new AlertDialog.Builder(BucketSelect.this); builder.setCancelable(true); @@ -373,7 +387,7 @@ public class BucketSelect extends AppCompatActivity { ClipData clip = ClipData.newPlainText("name", name); clipboard.setPrimaryClip(clip); if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) { - Toast.makeText(getApplicationContext(),getResources().getString(R.string.copy_name_ok), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.copy_ok), Toast.LENGTH_SHORT).show(); } } } \ No newline at end of file diff --git a/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java b/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java index c13d83a..4259dde 100644 --- a/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java +++ b/app/src/main/java/asgardius/page/s3manager/ObjectSelect.java @@ -396,6 +396,8 @@ public class ObjectSelect extends AppCompatActivity { } else if (menuItem.getTitle() == getResources().getString(R.string.download_folder)) { //Toast.makeText(ObjectSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); download(Name.get(position).toString(), true); + } else if (menuItem.getTitle() == getResources().getString(R.string.create_link)) { + share( Name.get(position).toString(), Img.get(position).equals(R.drawable.audiofile) || Img.get(position).equals(R.drawable.videofile)); } else if (menuItem.getTitle() == getResources().getString(R.string.object_info)) { objectInfo(prefix + Name.get(position).toString(), Name.get(position).toString()); } else if (menuItem.getTitle() == getResources().getString(R.string.copy_name)) { @@ -433,7 +435,7 @@ public class ObjectSelect extends AppCompatActivity { //Toast.makeText(ObjectSelect.this, getResources().getString(R.string.pending_feature), Toast.LENGTH_SHORT).show(); upload(true); } else if (menuItem.getTitle() == getResources().getString(R.string.create_link)) { - share(prefix + Name.get(position).toString(), Name.get(position).toString(), Img.get(position).equals(R.drawable.audiofile) || Img.get(position).equals(R.drawable.videofile)); + share(Name.get(position).toString(), Img.get(position).equals(R.drawable.audiofile) || Img.get(position).equals(R.drawable.videofile)); } else if (menuItem.getTitle() == getResources().getString(R.string.object_info)) { objectInfo(prefix + Name.get(position).toString(), Name.get(position).toString()); } else if (menuItem.getTitle() == getResources().getString(R.string.copy_name)) { @@ -565,7 +567,7 @@ public class ObjectSelect extends AppCompatActivity { } - private void share(String object, String title, boolean mediafile) { + private void share(String object, boolean mediafile) { Intent intent = new Intent(this, Share.class); //treelevel ++; @@ -573,11 +575,12 @@ public class ObjectSelect extends AppCompatActivity { intent.putExtra("username", username); intent.putExtra("password", password); intent.putExtra("bucket", bucket); - intent.putExtra("object", object); - intent.putExtra("title", title); + intent.putExtra("object", prefix+object); + intent.putExtra("title", object); intent.putExtra("region", location); intent.putExtra("mediafile", mediafile); intent.putExtra("videotime", videotime); + intent.putExtra("playlisttime", playlisttime); intent.putExtra("style", style); startActivity(intent); @@ -739,7 +742,7 @@ public class ObjectSelect extends AppCompatActivity { } clipboard.setPrimaryClip(clip); if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) { - Toast.makeText(getApplicationContext(),getResources().getString(R.string.copy_name_ok), Toast.LENGTH_SHORT).show(); + Toast.makeText(getApplicationContext(),getResources().getString(R.string.copy_ok), Toast.LENGTH_SHORT).show(); } } diff --git a/app/src/main/java/asgardius/page/s3manager/Share.java b/app/src/main/java/asgardius/page/s3manager/Share.java index 102d883..d2a79fc 100644 --- a/app/src/main/java/asgardius/page/s3manager/Share.java +++ b/app/src/main/java/asgardius/page/s3manager/Share.java @@ -2,8 +2,11 @@ package asgardius.page.s3manager; import androidx.appcompat.app.AppCompatActivity; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; import android.content.Intent; -import android.database.sqlite.SQLiteDatabase; +import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.Button; @@ -17,26 +20,32 @@ 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 com.amazonaws.services.s3.model.ListObjectsRequest; +import com.amazonaws.services.s3.model.ObjectListing; +import com.amazonaws.services.s3.model.S3ObjectSummary; import java.net.URL; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.List; public class Share extends AppCompatActivity { - String username, password, endpoint, bucket, object, location, title; + String username, password, endpoint, bucket, object, location, title, objectlist; boolean mediafile, style; Region region; S3ClientOptions s3ClientOptions; AWSCredentials myCredentials; AmazonS3 s3client; + ListObjectsRequest orequest; Calendar mycal; EditText datepick, hourpick, minutepick; int date, hour, minute; - Button share; + Button share, copylinks, savelinks; GeneratePresignedUrlRequest request; Date expiration; URL objectURL; - int videotime; + int videotime, playlisttime; public static String URLify(String str) { str = str.trim(); @@ -76,6 +85,8 @@ public class Share extends AppCompatActivity { hourpick = (EditText)findViewById(R.id.Hour); minutepick = (EditText)findViewById(R.id.Minute); share = (Button)findViewById(R.id.share); + copylinks = (Button)findViewById(R.id.copy_links); + savelinks = (Button)findViewById(R.id.save_links); endpoint = getIntent().getStringExtra("endpoint"); username = getIntent().getStringExtra("username"); password = getIntent().getStringExtra("password"); @@ -85,6 +96,7 @@ public class Share extends AppCompatActivity { object = getIntent().getStringExtra("object"); mediafile = getIntent().getBooleanExtra("mediafile", false); videotime = getIntent().getIntExtra("videotime", 1); + playlisttime = getIntent().getIntExtra("playlisttime", 1); title = getIntent().getStringExtra("title"); getSupportActionBar().setTitle(title); region = Region.getRegion(location); @@ -98,6 +110,12 @@ public class Share extends AppCompatActivity { s3client.setEndpoint(endpoint); s3ClientOptions.setPathStyleAccess(style); s3client.setS3ClientOptions(s3ClientOptions); + if(object == null || object.endsWith("/")) { + copylinks.setVisibility(View.VISIBLE); + savelinks.setVisibility(View.VISIBLE); + } else { + share.setVisibility(View.VISIBLE); + } share.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { @@ -148,5 +166,102 @@ public class Share extends AppCompatActivity { } }); + copylinks.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View view) { + //buttonaction + Thread getLinks = new Thread(new Runnable() { + + @Override + public void run() { + try { + //load media file + if (datepick.getText().toString().equals("")) { + date = 0; + } else { + date = Integer.parseInt(datepick.getText().toString()); + } + if (hourpick.getText().toString().equals("")) { + hour = 0; + } else { + hour = Integer.parseInt(hourpick.getText().toString()); + } + if (minutepick.getText().toString().equals("")) { + minute = 0; + } else { + minute = Integer.parseInt(minutepick.getText().toString()); + } + expiration = new Date(); + //System.out.println("today is " + mycal.getTime()); + mycal.setTime(expiration); + if (date == 0 && hour == 0 && minute == 0) { + if (mediafile) { + mycal.add(Calendar.HOUR, playlisttime); + } else { + mycal.add(Calendar.MINUTE, 15); + } + } else { + mycal.add(Calendar.DATE, date); + mycal.add(Calendar.HOUR, hour); + mycal.add(Calendar.MINUTE, minute); + } + //System.out.println("Expiration date: " + mycal.getTime()); + expiration = mycal.getTime(); + //System.out.println(expiration); + if (object == null) { + orequest = new ListObjectsRequest().withBucketName(bucket).withMaxKeys(1000); + } else { + orequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(object).withMaxKeys(1000); + } + ObjectListing result = s3client.listObjects(orequest); + objectlist = ""; + List objects = result.getObjectSummaries(); + for (S3ObjectSummary os : objects) { + request = new GeneratePresignedUrlRequest(bucket, os.getKey()).withExpiration(expiration); + objectlist = objectlist+s3client.generatePresignedUrl(request).toString()+"\n"; + } + while (result.isTruncated()) { + result = s3client.listNextBatchOfObjects (result); + objects = result.getObjectSummaries(); + for (S3ObjectSummary os : objects) { + request = new GeneratePresignedUrlRequest(bucket, os.getKey()).withExpiration(expiration); + objectlist = objectlist+s3client.generatePresignedUrl(request).toString()+"\n"; + } + + } + + runOnUiThread(new Runnable() { + + @Override + public void run() { + // Sending reference and data to Adapter + ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clip; + clip = ClipData.newPlainText("name", objectlist); + clipboard.setPrimaryClip(clip); + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) { + Toast.makeText(getApplicationContext(),getResources().getString(R.string.copy_ok), Toast.LENGTH_SHORT).show(); + } + } + }); + //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.invalid_expiration_date), Toast.LENGTH_SHORT).show(); + } + }); + } + } + }); + getLinks.start(); + } + + }); } } \ 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 index 2fa0dfc..62d11f5 100644 --- a/app/src/main/res/layout/activity_share.xml +++ b/app/src/main/res/layout/activity_share.xml @@ -75,6 +75,23 @@ android:layout_width="match_parent" android:layout_height="70dp" android:text="@string/file_share" + android:visibility="gone" + tools:ignore="MissingConstraints" /> + +