Skip to content

Commit c88ab72

Browse files
authored
Merge pull request #58 from mirisbowring/Fix-#31
Implemented parser to retrieve displayname for files/content
2 parents e4ec9a2 + 1c8ac42 commit c88ab72

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

app/src/main/java/org/docspell/docspellshare/activity/ShareActivity.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
import android.content.ContentResolver;
44
import android.content.Intent;
5+
import android.database.Cursor;
56
import android.net.Uri;
67
import android.os.Bundle;
78
import android.os.Handler;
9+
import android.provider.MediaStore;
10+
import android.provider.OpenableColumns;
811
import android.util.Log;
912
import android.widget.ProgressBar;
1013
import android.widget.TextView;
1114
import androidx.annotation.Nullable;
1215
import androidx.appcompat.app.AppCompatActivity;
16+
1317
import java.util.Collections;
1418
import java.util.List;
1519
import org.docspell.docspellshare.R;
@@ -128,8 +132,53 @@ void handleFiles(List<Uri> uris, String url) {
128132
HttpRequest.Builder req = HttpRequest.newBuilder().setUrl(url);
129133
ContentResolver resolver = getContentResolver();
130134
for (Uri uri : uris) {
131-
req.addFile(resolver, uri);
135+
req.addFile(resolver, uri, parseFilenameFromUri(uri));
132136
}
133137
UploadManager.getInstance().submit(req.build());
134138
}
139+
140+
/**
141+
* Parses the filename for the passed uri.
142+
* Depending on the source application, a file:// or content:// will be shared.
143+
* If a file:// is shared, the filename/title is the last segment of the uri.
144+
* IF NOT, the last segment of the uri is the android document id which is not related to the
145+
* document at all.
146+
* Therefore, a cursor has to be defined to read the content's metadata to obtain the name/title
147+
*
148+
* See https://developer.android.com/training/secure-file-sharing/retrieve-info?hl=en for ref
149+
*
150+
* @param uri link to content/file the name should be parsed from
151+
* @return parsed "real" name of the content/file
152+
*/
153+
private String parseFilenameFromUri(Uri uri) {
154+
String fileName = null;
155+
if (uri.getScheme().equals("file")) {
156+
fileName = uri.getLastPathSegment();
157+
} else {
158+
Cursor cursor = null;
159+
try {
160+
cursor = getContentResolver().query(
161+
uri,
162+
new String[]{
163+
MediaStore.Images.ImageColumns.DISPLAY_NAME
164+
},
165+
null,
166+
null,
167+
null
168+
);
169+
if (cursor != null && cursor.moveToFirst()) {
170+
fileName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
171+
}
172+
} catch (IllegalArgumentException e) {
173+
// exception will be thrown if index is out of range (e.x. -1).
174+
// No need to handle the exception since the HttpRequest Class will fallback to Document ID
175+
} finally {
176+
if (cursor != null) {
177+
cursor.close();
178+
}
179+
}
180+
}
181+
return fileName;
182+
}
135183
}
184+

app/src/main/java/org/docspell/docspellshare/http/HttpRequest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import okio.Okio;
2323
import okio.Source;
2424
import org.docspell.docspellshare.data.Option;
25+
import org.docspell.docspellshare.util.Strings;
2526
import org.docspell.docspellshare.util.Uris;
2627

2728
public final class HttpRequest {
@@ -68,7 +69,7 @@ public static class Builder {
6869
private final List<DataPart> parts = new ArrayList<>();
6970
private String url;
7071

71-
public Builder addFile(ContentResolver resolver, Uri data) {
72+
public Builder addFile(ContentResolver resolver, Uri data, String fileName) {
7273
parts.add(
7374
new DataPart() {
7475
@Override
@@ -78,7 +79,11 @@ public InputStream getData() throws IOException {
7879

7980
@Override
8081
public String getName() {
81-
return data.getLastPathSegment();
82+
if (Strings.isNullOrBlank(fileName)) {
83+
return data.getLastPathSegment();
84+
} else {
85+
return fileName;
86+
}
8287
}
8388

8489
@Override
@@ -132,7 +137,7 @@ public long contentLength() {
132137
@Override
133138
public void writeTo(@NonNull BufferedSink sink) throws IOException {
134139
try (InputStream in = part.getData();
135-
Source source = Okio.source(in)) {
140+
Source source = Okio.source(in)) {
136141
long total = 0;
137142
long read;
138143
while ((read = source.read(sink.getBuffer(), CHUNK_SIZE)) != -1) {

0 commit comments

Comments
 (0)