Fix handling file names with spaces for mime type detection (#134)
This fixes https://github.com/syncthing/syncthing-lite/issues/133
This commit is contained in:
@@ -47,7 +47,7 @@ class FileMenuDialogFragment: BottomSheetDialogFragment() {
|
||||
Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
|
||||
addCategory(Intent.CATEGORY_OPENABLE)
|
||||
|
||||
type = MimeType.getFromUrl(fileSpec.fileName)
|
||||
type = MimeType.getFromFilename(fileSpec.fileName)
|
||||
|
||||
putExtra(Intent.EXTRA_TITLE, fileSpec.fileName)
|
||||
},
|
||||
|
||||
+1
-1
@@ -88,7 +88,7 @@ class DownloadFileDialogFragment : DialogFragment() {
|
||||
dismissAllowingStateLoss()
|
||||
|
||||
if (outputUri == null) {
|
||||
val mimeType = MimeType.getFromUrl(fileSpec.fileName)
|
||||
val mimeType = MimeType.getFromFilename(fileSpec.fileName)
|
||||
|
||||
try {
|
||||
context!!.startActivity(
|
||||
|
||||
@@ -159,7 +159,7 @@ class SyncthingProvider : DocumentsProvider() {
|
||||
if (fileInfo.isDirectory())
|
||||
Document.MIME_TYPE_DIR
|
||||
else
|
||||
MimeType.getFromUrl(fileInfo.fileName)
|
||||
MimeType.getFromFilename(fileInfo.fileName)
|
||||
)
|
||||
add(Document.COLUMN_LAST_MODIFIED, fileInfo.lastModified)
|
||||
add(Document.COLUMN_FLAGS, 0)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.syncthing.lite.utils
|
||||
|
||||
import android.webkit.MimeTypeMap
|
||||
import net.syncthing.java.core.utils.PathUtils
|
||||
|
||||
object MimeType {
|
||||
private const val DEFAULT_MIME_TYPE = "application/octet-stream"
|
||||
@@ -11,7 +12,7 @@ object MimeType {
|
||||
return mimeType ?: DEFAULT_MIME_TYPE
|
||||
}
|
||||
|
||||
fun getFromUrl(url: String) = getFromExtension(
|
||||
MimeTypeMap.getFileExtensionFromUrl(url).toLowerCase()
|
||||
fun getFromFilename(path: String) = getFromExtension(
|
||||
PathUtils.getFileExtensionFromFilename(path).toLowerCase()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -130,4 +130,12 @@ object PathUtils {
|
||||
|
||||
return dir.removeSuffix(PATH_SEPARATOR) + file
|
||||
}
|
||||
|
||||
fun getFileExtensionFromFilename(filename: String): String {
|
||||
assertFilenameValid(filename)
|
||||
|
||||
val dotIndex = filename.lastIndexOf(".")
|
||||
|
||||
return if (dotIndex != 0) filename.substring(dotIndex + 1) else ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user