@@ -23,14 +23,14 @@ package eu.opencloud.android.presentation.thumbnails
2323import android.accounts.Account
2424import android.accounts.AccountManager
2525import android.net.Uri
26+ import androidx.annotation.VisibleForTesting
2627import coil.ImageLoader
2728import coil.disk.DiskCache
2829import coil.memory.MemoryCache
2930import coil.util.DebugLogger
3031import eu.opencloud.android.MainApp.Companion.appContext
3132import eu.opencloud.android.data.ClientManager
3233import eu.opencloud.android.data.providers.SharedPreferencesProvider
33- import java.util.concurrent.ConcurrentHashMap
3434import eu.opencloud.android.domain.files.model.OCFile
3535import eu.opencloud.android.domain.files.model.OCFileWithSyncInfo
3636import eu.opencloud.android.domain.spaces.model.SpaceSpecial
@@ -50,14 +50,15 @@ import org.koin.core.component.KoinComponent
5050import org.koin.core.component.inject
5151import timber.log.Timber
5252import java.util.Locale
53+ import java.util.concurrent.ConcurrentHashMap
5354
5455object ThumbnailsRequester : KoinComponent {
5556 private val clientManager: ClientManager by inject()
5657 private val preferencesProvider: SharedPreferencesProvider by inject()
5758
5859 // https://docs.opencloud.eu/docs/next/dev/server/services/thumbnails/information/#thumbnail-query-string-parameters
5960 private const val SPACE_SPECIAL_PREVIEW_URI = " %s?scalingup=0&a=1&x=%d&y=%d&c=%s&preview=1"
60- private const val FILE_PREVIEW_URI = " %s/webdav %s?x=%d&y=%d&c=%s&preview=1"
61+ private const val FILE_PREVIEW_URI = " %s%s?x=%d&y=%d&c=%s&preview=1"
6162
6263 private const val THUMBNAIL_DISK_CACHE_SIZE : Long = 1024 * 1024 * 100 // 100MB
6364 private const val AVATAR_HTTP_CACHE_SIZE : Long = 10L * 1024 * 1024 // 10MB
@@ -99,25 +100,43 @@ object ThumbnailsRequester : KoinComponent {
99100 }
100101
101102 fun getPreviewUriForFile (file : OCFile , account : Account , etag : String? = null, width : Int = 1024, height : Int = 1024): String =
102- getPreviewUri(file.remotePath , etag ? : file.remoteEtag, account, width, height)
103+ getPreviewUri(file, null , etag ? : file.remoteEtag, account, width, height)
103104
104105 fun getPreviewUriForFile (fileWithSyncInfo : OCFileWithSyncInfo , account : Account , width : Int = 1024, height : Int = 1024): String =
105- getPreviewUriForFile (fileWithSyncInfo.file, account, null , width, height)
106+ getPreviewUri (fileWithSyncInfo.file, fileWithSyncInfo.space?.root?.webDavUrl, fileWithSyncInfo.file.remoteEtag, account , width, height)
106107
107108 fun getPreviewUriForSpaceSpecial (spaceSpecial : SpaceSpecial ): String =
108109 String .format(Locale .US , SPACE_SPECIAL_PREVIEW_URI , spaceSpecial.webDavUrl, 1024 , 1024 , spaceSpecial.eTag)
109110
110- private fun getPreviewUri (remotePath : String? , etag : String? , account : Account , width : Int , height : Int ): String {
111+ private fun getPreviewUri (file : OCFile , spaceWebDavUrl : String? , etag : String? , account : Account , width : Int , height : Int ): String {
111112 val baseUrl = accountBaseUrls.getOrPut(account.name) {
112113 val accountManager = AccountManager .get(appContext)
113114 accountManager.getUserData(account, eu.opencloud.android.lib.common.accounts.AccountUtils .Constants .KEY_OC_BASE_URL )
114115 ?.trimEnd(' /' )
115116 .orEmpty()
116117 }
118+ return buildPreviewUri(baseUrl, file.remotePath, file.spaceId, spaceWebDavUrl, etag, width, height)
119+ }
120+
121+ @VisibleForTesting
122+ internal fun buildPreviewUri (
123+ accountBaseUrl : String ,
124+ remotePath : String? ,
125+ spaceId : String? ,
126+ spaceWebDavUrl : String? ,
127+ etag : String? ,
128+ width : Int ,
129+ height : Int ,
130+ ): String {
131+ val previewBaseUrl = when {
132+ ! spaceWebDavUrl.isNullOrBlank() -> spaceWebDavUrl.trimEnd(' /' )
133+ ! spaceId.isNullOrBlank() -> " ${accountBaseUrl.trimEnd(' /' )} /dav/spaces/${Uri .encode(spaceId, " \$ " )} "
134+ else -> " ${accountBaseUrl.trimEnd(' /' )} /webdav"
135+ }
117136 val path = if (remotePath?.startsWith(" /" ) == true ) remotePath else " /$remotePath "
118137 val encodedPath = Uri .encode(path, " /" )
119138
120- return String .format(Locale .US , FILE_PREVIEW_URI , baseUrl , encodedPath, width, height, etag.orEmpty())
139+ return String .format(Locale .US , FILE_PREVIEW_URI , previewBaseUrl , encodedPath, width, height, etag.orEmpty())
121140 }
122141
123142 fun getContentAddressedImageLoader (): ImageLoader {
0 commit comments