Skip to content

Commit

Permalink
Refactor thumbnail association
Browse files Browse the repository at this point in the history
  • Loading branch information
JustFanta01 committed Sep 7, 2024
1 parent 68b8744 commit 7110a54
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ abstract class CryptoImplDecorator(

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

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

private fun isGenerateThumbnailsEnabled(cache: DiskLruCache?, fileName: String): Boolean {
return sharedPreferencesHandler.useLruCache() &&
sharedPreferencesHandler.generateThumbnails() != ThumbnailsOption.NEVER &&
cache != null &&
isImageMediaType(fileName)
private fun isThumbnailGenerationAvailable(cache: DiskLruCache?, fileName: String): Boolean {
return isGenerateThumbnailsEnabled() && cache != null && isImageMediaType(fileName)
}

protected fun associateThumbnailIfInCache(list: List<CryptoNode?>): List<CryptoNode?> {
if (isGenerateThumbnailsEnabled()) {
val firstCryptoFile = list.find { it is CryptoFile } ?: return list
val cloudType = (firstCryptoFile as CryptoFile).cloudFile.cloud?.type() ?: return list
val diskCache = getLruCacheFor(cloudType) ?: return list
list.onEach { cryptoNode ->
if (cryptoNode is CryptoFile && isImageMediaType(cryptoNode.name)) {
val cacheKey = generateCacheKey(cryptoNode.cloudFile)
val cacheFile = diskCache[cacheKey]
if (cacheFile != null) {
cryptoNode.thumbnail = cacheFile
}
}
}
}
return list
}

private fun isGenerateThumbnailsEnabled(): Boolean {
return sharedPreferencesHandler.useLruCache() && sharedPreferencesHandler.generateThumbnails() != ThumbnailsOption.NEVER
}

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

protected fun isImageMediaType(filename: String): Boolean {
private fun isImageMediaType(filename: String): Boolean {
return (mimeTypes.fromFilename(filename) ?: MimeType.WILDCARD_MIME_TYPE).mediatype == "image"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,8 @@ open class CryptoImplVaultFormat7 : CryptoImplDecorator {
}
}.map { node ->
ciphertextToCleartextNode(cryptoFolder, dirId, node)
}.onEach { cryptoNode ->
if (cryptoNode is CryptoFile && isImageMediaType(cryptoNode.name)) {
val cacheKey = generateCacheKey(cryptoNode.cloudFile)
cryptoNode.cloudFile.cloud?.type()?.let { cloudType ->
getLruCacheFor(cloudType)?.let { diskCache ->
val cacheFile = diskCache[cacheKey]
if (cacheFile != null) {
cryptoNode.thumbnail = cacheFile
}
}
}
}
}.also {
associateThumbnailIfInCache(it)
}.toList().filterNotNull()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,9 @@ internal class CryptoImplVaultFormatPre7(
.filterIsInstance<CloudFile>()
.map { node ->
ciphertextToCleartextNode(cryptoFolder, dirId, node)
}.onEach { cryptoNode ->
if (cryptoNode is CryptoFile && isImageMediaType(cryptoNode.name)) {
val cacheKey = generateCacheKey(cryptoNode.cloudFile)
cryptoNode.cloudFile.cloud?.type()?.let { cloudType ->
getLruCacheFor(cloudType)?.let { diskCache ->
val cacheFile = diskCache[cacheKey]
if (cacheFile != null) {
cryptoNode.thumbnail = cacheFile
}
}
}
}
}
.toList()
.filterNotNull()
}.also {
associateThumbnailIfInCache(it)
}.toList().filterNotNull()
}

@Throws(BackendException::class)
Expand Down

0 comments on commit 7110a54

Please sign in to comment.