Skip to content

Commit

Permalink
SearchResultEntry: detect file results not from Files provider
Browse files Browse the repository at this point in the history
Some providers like FullTextSearch give file results without the fileId attribute.
This refactor allows for that, otherwise they would crash the UnifiedSearch UI.

Signed-off-by: Álvaro Brey Vilas <[email protected]>
  • Loading branch information
AlvaroBrey authored and backportbot[bot] committed Nov 25, 2021
1 parent 99bcaa3 commit dcd6a26
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/main/java/com/owncloud/android/lib/common/SearchResultEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
package com.owncloud.android.lib.common

import android.net.UrlQuerySanitizer
import java.net.URL

/**
* One search result entry of an unified search
Expand All @@ -42,8 +41,14 @@ data class SearchResultEntry(
var attributes: Map<String, String> = emptyMap()
) {

companion object {
private const val PARAM_DIR = "dir"
private const val PARAM_FILE = "scrollto"
private const val DIR_ROOT = "/"
}

val isFile: Boolean
get() = fileId() != null
get() = fileId() != null || listOf(PARAM_DIR, PARAM_FILE).all { resourceUrl.contains(it) }

fun fileId(): String? {
return attributes["fileId"]
Expand All @@ -54,17 +59,20 @@ data class SearchResultEntry(
}

private fun parseRemotePath(): String {
val sanitizer = UrlQuerySanitizer()
sanitizer.allowUnregisteredParamaters = true
sanitizer.unregisteredParameterValueSanitizer = UrlQuerySanitizer.getAllButNulLegal()
val sanitizer = UrlQuerySanitizer().apply {
allowUnregisteredParamaters = true
unregisteredParameterValueSanitizer = UrlQuerySanitizer.getAllButNulLegal()
}

sanitizer.parseUrl(resourceUrl)
URL(resourceUrl).query?.let { sanitizer.parseQuery(it) }
val dir = if (sanitizer.getValue("dir") == "/") {
""
} else {
sanitizer.getValue("dir")

val dirParam = sanitizer.getValue(PARAM_DIR)
val dir = when (dirParam) {
DIR_ROOT -> ""
else -> dirParam
}
val file = sanitizer.getValue("scrollto")

val file = sanitizer.getValue(PARAM_FILE)

return "$dir/$file"
}
Expand Down

0 comments on commit dcd6a26

Please sign in to comment.