Skip to content

Commit 7110a54

Browse files
committed
Refactor thumbnail association
1 parent 68b8744 commit 7110a54

File tree

3 files changed

+31
-34
lines changed

3 files changed

+31
-34
lines changed

data/src/main/java/org/cryptomator/data/cloud/crypto/CryptoImplDecorator.kt

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ abstract class CryptoImplDecorator(
384384

385385
val diskCache = cryptoFile.cloudFile.cloud?.type()?.let { getLruCacheFor(it) }
386386
val cacheKey = generateCacheKey(ciphertextFile)
387-
val genThumbnail = isGenerateThumbnailsEnabled(diskCache, cryptoFile.name)
387+
val genThumbnail = isThumbnailGenerationAvailable(diskCache, cryptoFile.name)
388388

389389
val thumbnailWriter = PipedOutputStream()
390390
val thumbnailReader = PipedInputStream(thumbnailWriter)
@@ -471,11 +471,30 @@ abstract class CryptoImplDecorator(
471471
return String.format("%s-%d", cloudFile.cloud?.id() ?: "common", cloudFile.path.hashCode())
472472
}
473473

474-
private fun isGenerateThumbnailsEnabled(cache: DiskLruCache?, fileName: String): Boolean {
475-
return sharedPreferencesHandler.useLruCache() &&
476-
sharedPreferencesHandler.generateThumbnails() != ThumbnailsOption.NEVER &&
477-
cache != null &&
478-
isImageMediaType(fileName)
474+
private fun isThumbnailGenerationAvailable(cache: DiskLruCache?, fileName: String): Boolean {
475+
return isGenerateThumbnailsEnabled() && cache != null && isImageMediaType(fileName)
476+
}
477+
478+
protected fun associateThumbnailIfInCache(list: List<CryptoNode?>): List<CryptoNode?> {
479+
if (isGenerateThumbnailsEnabled()) {
480+
val firstCryptoFile = list.find { it is CryptoFile } ?: return list
481+
val cloudType = (firstCryptoFile as CryptoFile).cloudFile.cloud?.type() ?: return list
482+
val diskCache = getLruCacheFor(cloudType) ?: return list
483+
list.onEach { cryptoNode ->
484+
if (cryptoNode is CryptoFile && isImageMediaType(cryptoNode.name)) {
485+
val cacheKey = generateCacheKey(cryptoNode.cloudFile)
486+
val cacheFile = diskCache[cacheKey]
487+
if (cacheFile != null) {
488+
cryptoNode.thumbnail = cacheFile
489+
}
490+
}
491+
}
492+
}
493+
return list
494+
}
495+
496+
private fun isGenerateThumbnailsEnabled(): Boolean {
497+
return sharedPreferencesHandler.useLruCache() && sharedPreferencesHandler.generateThumbnails() != ThumbnailsOption.NEVER
479498
}
480499

481500
private fun storeThumbnail(cache: DiskLruCache?, cacheKey: String, thumbnailBitmap: Bitmap) {
@@ -493,7 +512,7 @@ abstract class CryptoImplDecorator(
493512
thumbnailFile.delete()
494513
}
495514

496-
protected fun isImageMediaType(filename: String): Boolean {
515+
private fun isImageMediaType(filename: String): Boolean {
497516
return (mimeTypes.fromFilename(filename) ?: MimeType.WILDCARD_MIME_TYPE).mediatype == "image"
498517
}
499518

data/src/main/java/org/cryptomator/data/cloud/crypto/CryptoImplVaultFormat7.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,8 @@ open class CryptoImplVaultFormat7 : CryptoImplDecorator {
165165
}
166166
}.map { node ->
167167
ciphertextToCleartextNode(cryptoFolder, dirId, node)
168-
}.onEach { cryptoNode ->
169-
if (cryptoNode is CryptoFile && isImageMediaType(cryptoNode.name)) {
170-
val cacheKey = generateCacheKey(cryptoNode.cloudFile)
171-
cryptoNode.cloudFile.cloud?.type()?.let { cloudType ->
172-
getLruCacheFor(cloudType)?.let { diskCache ->
173-
val cacheFile = diskCache[cacheKey]
174-
if (cacheFile != null) {
175-
cryptoNode.thumbnail = cacheFile
176-
}
177-
}
178-
}
179-
}
168+
}.also {
169+
associateThumbnailIfInCache(it)
180170
}.toList().filterNotNull()
181171
}
182172

data/src/main/java/org/cryptomator/data/cloud/crypto/CryptoImplVaultFormatPre7.kt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,9 @@ internal class CryptoImplVaultFormatPre7(
128128
.filterIsInstance<CloudFile>()
129129
.map { node ->
130130
ciphertextToCleartextNode(cryptoFolder, dirId, node)
131-
}.onEach { cryptoNode ->
132-
if (cryptoNode is CryptoFile && isImageMediaType(cryptoNode.name)) {
133-
val cacheKey = generateCacheKey(cryptoNode.cloudFile)
134-
cryptoNode.cloudFile.cloud?.type()?.let { cloudType ->
135-
getLruCacheFor(cloudType)?.let { diskCache ->
136-
val cacheFile = diskCache[cacheKey]
137-
if (cacheFile != null) {
138-
cryptoNode.thumbnail = cacheFile
139-
}
140-
}
141-
}
142-
}
143-
}
144-
.toList()
145-
.filterNotNull()
131+
}.also {
132+
associateThumbnailIfInCache(it)
133+
}.toList().filterNotNull()
146134
}
147135

148136
@Throws(BackendException::class)

0 commit comments

Comments
 (0)