Skip to content

Commit

Permalink
substitute unavailable names with sha512
Browse files Browse the repository at this point in the history
  • Loading branch information
9001 committed Jan 23, 2022
1 parent b7a2870 commit 9da219a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
43 changes: 30 additions & 13 deletions app/src/main/java/me/ocv/partyup/XferActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ else if (one) {
return;
}

password = prefs.getString("server_password", "");
password = password == null ? "" : password;
if (password.equals("Default value"))
password = ""; // necessary in the emulator, not on real devices(?)

password = password.isEmpty() ? null :
"Basic " + new String(Base64.getEncoder().encode(password.getBytes()));

final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(v -> {
fab.setVisibility(View.GONE);
Expand Down Expand Up @@ -175,8 +183,13 @@ private void handleSendImage() {
Log.w("me.ocv.partyup", "contentresolver: " + ex.toString());
}

if (f.name == null)
f.name = "mystery-file." + getext(the_intent.getType());
MessageDigest md = null;
if (f.name == null) {
try {
md = MessageDigest.getInstance("SHA-512");
}
catch (Exception ex) { }
}

// get correct filesize
try {
Expand All @@ -190,22 +203,30 @@ private void handleSendImage() {
break;

sz += n;
if (md != null)
md.update(buf, 0, n);
}
f.size = sz;
} catch (Exception ex) {
show_msg("Error3: " + ex.toString());
return;
}

if (md != null) {
String csum = new String(Base64.getUrlEncoder().encode(md.digest())).substring(0, 15);
f.name = format("mystery-file-%s.%s", csum, getext(the_intent.getType()));
}

f.desc = format("%s\n\nsize: %,d byte\ntype: %s", f.name, f.size, the_intent.getType());
}

String msg;
bytes_done = bytes_total = 0;
if (files.length == 1) {
msg = "Upload the following file?\n\n" + files[0].desc;
bytes_total = files[0].size;
}
else {
bytes_done = bytes_total = 0;
msg = "Upload the following " + files.length + " files?\n\n";
for (int a = 0; a < Math.min(10, files.length); a++) {
msg += files[a].name + "\n";
Expand Down Expand Up @@ -242,11 +263,6 @@ private void do_up2() {
if (!base_url.endsWith("/"))
base_url += "/";

password = prefs.getString("server_password", "");
password = password == null ? "" : password;
if (password.equals("Default value"))
password = ""; // necessary in the emulator, not on real devices(?)

tshow_msg("Sending to " + base_url + " ...");

int nfiles = files == null ? 1 : files.length;
Expand All @@ -262,8 +278,8 @@ private void do_up2() {
URL url = new URL(full_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
if (!password.isEmpty())
conn.setRequestProperty("Authorization", "Basic " + new String(Base64.getEncoder().encode(password.getBytes())));
if (password != null)
conn.setRequestProperty("Authorization", password);

if (files == null)
do_textmsg(conn);
Expand Down Expand Up @@ -314,6 +330,7 @@ private boolean do_fileput(HttpURLConnection conn, int nfile) throws Exception {
conn.setRequestProperty("Content-Type", "application/octet-stream");
conn.connect();
final TextView tv = (TextView) findViewById(R.id.upper_info);
final ProgressBar pb = (ProgressBar) findViewById(R.id.progbar);
OutputStream os = conn.getOutputStream();
InputStream ins = getContentResolver().openInputStream(f.handle);
MessageDigest md = MessageDigest.getInstance("SHA-512");
Expand All @@ -329,17 +346,17 @@ private boolean do_fileput(HttpURLConnection conn, int nfile) throws Exception {
md.update(buf, 0, n);

tv.post(() -> {
double perc = ((double) bytes_done * 100) / bytes_total;
double perc = ((double) bytes_done * 1000) / bytes_total;
tv.setText(format("Sending %d of %d to %s ...\n\n%s\n\nbytes done: %,d\nbytes left: %,d\nprogress: %.2f %%",
nfile + 1,
files.length,
base_url,
f.desc,
bytes_done,
bytes_total - bytes_done,
perc
perc / 10
));
((ProgressBar) findViewById(R.id.progbar)).setProgress((int) Math.round(perc));
pb.setProgress((int) Math.round(perc));
});
}
os.flush();
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_xfer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="1000"
android:scaleY="5" />

</LinearLayout>
Expand Down
4 changes: 2 additions & 2 deletions metadata/en-US/changelogs/10600.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
• new: upload multiple files
• new: choose what happens when an upload succeeds
• new: choose what happens when upload succeeds
• new: add http:// to the server URL if missing
• bugfix: sharing from some apps in android 10+ could crash this app
• bugfix: sharing from some apps on android 10+ could crash this app

0 comments on commit 9da219a

Please sign in to comment.