Skip to content

Commit 9c1524a

Browse files
committed
sharing: Set appropriate filename of shared content.
Previously filename was the last element of sharedFileUrl.split('/') or sharedImageUrl.split('/'). This did not always correspond to the actual filename, sometimes this element did not contain a file extension making the uploaded data to be stored as a binary file. This made shared images fail to render and on downloading the shared content it was downloaded as a binary file. This commit uses ContentResolver to find the actual name of file being shared and uses that to identify the shared file.
1 parent 19e542c commit 9c1524a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

android/app/src/main/java/com/zulipmobile/MainActivity.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.zulipmobile
22

33
import android.content.Intent
4+
import android.provider.MediaStore
5+
import android.content.ContentResolver
46
import android.net.Uri
57
import android.os.Bundle
68
import android.util.Log
79
import android.webkit.WebView
10+
import android.database.Cursor
811
import com.facebook.react.ReactActivity
912
import com.facebook.react.ReactActivityDelegate;
1013
import com.facebook.react.ReactRootView;
@@ -94,6 +97,26 @@ open class MainActivity : ReactActivity() {
9497
}
9598
}
9699

100+
private fun getFileName(uri: Uri, cr: ContentResolver): String? {
101+
var fileName: String? = null;
102+
if (uri.scheme.equals("file")) {
103+
fileName = uri.lastPathSegment;
104+
} else {
105+
var cursor: Cursor? = null;
106+
try {
107+
cursor = cr.query(uri, null, null, null, null)
108+
if (cursor != null && cursor.moveToFirst()) {
109+
fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME))
110+
}
111+
} finally {
112+
if (cursor != null) {
113+
cursor.close();
114+
}
115+
}
116+
}
117+
return fileName;
118+
}
119+
97120
private fun getParamsFromIntent(intent: Intent): WritableMap {
98121
// For documentation of what fields to expect here, see:
99122
// https://developer.android.com/reference/android/content/Intent#ACTION_SEND
@@ -109,12 +132,14 @@ open class MainActivity : ReactActivity() {
109132
?: throw ShareParamsParseException("Could not extract URL from Image Intent")
110133
params.putString("type", "image")
111134
params.putString("sharedImageUrl", url.toString())
135+
params.putString("fileName", getFileName(url, this.contentResolver))
112136
}
113137
else -> {
114138
val url = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
115139
?: throw ShareParamsParseException("Could not extract URL from File Intent")
116140
params.putString("type", "file")
117141
params.putString("sharedFileUrl", url.toString())
142+
params.putString("fileName", getFileName(url, this.contentResolver))
118143
}
119144
}
120145
return params

src/sharing/send.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const handleSend = async (data: SendStream | SendPm, auth: Auth, _: GetTe
3434

3535
if (sharedData.type === 'image' || sharedData.type === 'file') {
3636
const url = sharedData.type === 'image' ? sharedData.sharedImageUrl : sharedData.sharedFileUrl;
37-
const fileName = url.split('/').pop();
37+
const fileName = sharedData.fileName;
3838
const response = await uploadFile(auth, url, fileName);
3939
messageToSend += `\n[${fileName}](${response.uri})`;
4040
}

src/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,13 @@ export type SharedText = {|
346346
export type SharedImage = {|
347347
type: 'image',
348348
sharedImageUrl: string,
349+
fileName: string,
349350
|};
350351

351352
export type SharedFile = {|
352353
type: 'file',
353354
sharedFileUrl: string,
355+
fileName: string,
354356
|};
355357

356358
/**

0 commit comments

Comments
 (0)