[V2] MM-33495 Sanitize filename in Android ShareExtension (#5211)

* [V2] MM-33495 Sanitize filename in Android ShareExtension

* Apply sanitization after getting uri last path segment
This commit is contained in:
Elias Nahum
2021-03-09 11:53:53 -03:00
committed by GitHub
parent d6a3504c08
commit 62e1003f10

View File

@@ -97,20 +97,24 @@ public class RealPathUtil {
File tmpFile;
String fileName = null;
if (uri == null || uri.isRelative()) {
return null;
}
// Try and get the filename from the Uri
try {
Cursor returnCursor =
context.getContentResolver().query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
fileName = returnCursor.getString(nameIndex);
fileName = sanitizeFilename(returnCursor.getString(nameIndex));
} catch (Exception e) {
// just continue to get the filename with the last segment of the path
}
try {
if (fileName == null) {
fileName = uri.getLastPathSegment().toString().trim();
if (TextUtils.isEmpty(fileName)) {
fileName = sanitizeFilename(uri.getLastPathSegment().toString().trim());
}
@@ -231,4 +235,14 @@ public class RealPathUtil {
fileOrDirectory.delete();
}
private static String sanitizeFilename(String filename) {
if (filename == null) {
return null;
}
File f = new File(filename);
return f.getName();
}
}