Skip to content

Commit fe0151d

Browse files
taglioIsCodingJustFanta01
authored andcommitted
Cleanup
1 parent 4d0e715 commit fe0151d

File tree

3 files changed

+27
-53
lines changed

3 files changed

+27
-53
lines changed

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

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,18 @@ import java.io.FileInputStream
4343
import java.io.FileOutputStream
4444
import java.io.IOException
4545
import java.io.OutputStream
46+
import java.io.PipedInputStream
47+
import java.io.PipedOutputStream
4648
import java.nio.ByteBuffer
4749
import java.nio.channels.Channels
4850
import java.util.LinkedList
4951
import java.util.Queue
5052
import java.util.UUID
51-
import java.util.function.Supplier
52-
import timber.log.Timber
53-
import java.io.PipedInputStream
54-
import java.io.PipedOutputStream
5553
import java.util.concurrent.ExecutorService
5654
import java.util.concurrent.Executors
5755
import java.util.concurrent.Future
58-
import kotlin.math.ceil
56+
import java.util.function.Supplier
57+
import timber.log.Timber
5958

6059

6160
abstract class CryptoImplDecorator(
@@ -81,10 +80,11 @@ abstract class CryptoImplDecorator(
8180
Executors.newFixedThreadPool(3, threadFactory)
8281
}
8382

84-
protected fun getLruCacheFor(type : CloudType): DiskLruCache? {
83+
protected fun getLruCacheFor(type: CloudType): DiskLruCache? {
8584
return getOrCreateLruCache(getCacheTypeFromCloudType(type), sharedPreferencesHandler.lruCacheSize())
8685
}
87-
private fun getOrCreateLruCache(key : LruFileCacheUtil.Cache, cacheSize: Int): DiskLruCache? {
86+
87+
private fun getOrCreateLruCache(key: LruFileCacheUtil.Cache, cacheSize: Int): DiskLruCache? {
8888
return diskLruCache.computeIfAbsent(key) {
8989
val where = LruFileCacheUtil(context).resolve(it)
9090
try {
@@ -96,7 +96,7 @@ abstract class CryptoImplDecorator(
9696
}
9797
}
9898

99-
private fun getCacheTypeFromCloudType(type : CloudType) : LruFileCacheUtil.Cache {
99+
private fun getCacheTypeFromCloudType(type: CloudType): LruFileCacheUtil.Cache {
100100
return when (type) {
101101
CloudType.DROPBOX -> LruFileCacheUtil.Cache.DROPBOX
102102
CloudType.GOOGLE_DRIVE -> LruFileCacheUtil.Cache.GOOGLE_DRIVE
@@ -401,12 +401,12 @@ abstract class CryptoImplDecorator(
401401
decrypted += read.toLong()
402402

403403
progressAware
404-
.onProgress(
405-
Progress.progress(DownloadState.decryption(cryptoFile)) //
406-
.between(0) //
407-
.and(cleartextSize) //
408-
.withValue(decrypted)
409-
)
404+
.onProgress(
405+
Progress.progress(DownloadState.decryption(cryptoFile)) //
406+
.between(0) //
407+
.and(cleartextSize) //
408+
.withValue(decrypted)
409+
)
410410
}
411411
}
412412
thumbnailWriter.flush()
@@ -423,44 +423,27 @@ abstract class CryptoImplDecorator(
423423
}
424424
}
425425

426-
private fun closeQuietly(closeable : Closeable) {
426+
private fun closeQuietly(closeable: Closeable) {
427427
try {
428428
closeable.close();
429-
} catch (e : IOException) {
429+
} catch (e: IOException) {
430430
// ignore
431431
}
432432
}
433-
private fun startThumbnailGeneratorThread(diskCache: DiskLruCache?, cacheKey: String, thumbnailReader: PipedInputStream) : Future<*> {
433+
434+
private fun startThumbnailGeneratorThread(diskCache: DiskLruCache?, cacheKey: String, thumbnailReader: PipedInputStream): Future<*> {
434435
return thumbnailExecutorService.submit {
435436
try {
436437
val options = BitmapFactory.Options()
437-
val thumbnailBitmap : Bitmap?
438-
// options.inJustDecodeBounds = true
439-
// read properties of the image: outWidth, outHeight (no bitmap allocation!)
440-
// BitmapFactory.decodeStream(thumbnailReaderTee, null, options)
441-
// options.inJustDecodeBounds = false
442-
// options.outWidth; options.outHeight
438+
val thumbnailBitmap: Bitmap?
443439
options.inSampleSize = 4 // pixel number reduced by a factor of 1/16
444-
// options.inSampleSize = 8 // pixel number reduced by a factor of 1/64
445440

446-
// obtain a subsampled version of the image
447441
val bitmap = BitmapFactory.decodeStream(thumbnailReader, null, options)
448-
449442
val thumbnailWidth = 100
450443
val thumbnailHeight = 100
451-
// var aspectRatio = 1f
452-
// bitmap?.let {
453-
// if (it.height != 0) {
454-
// aspectRatio = it.width.toFloat() / it.height
455-
// }
456-
// }
457-
// val thumbnailHeight = ceil(1 / aspectRatio * thumbnailWidth).toInt()
458-
459-
// generate thumbnail preserving aspect ratio
460444
thumbnailBitmap = ThumbnailUtils.extractThumbnail(bitmap, thumbnailWidth, thumbnailHeight)
461445

462-
// store it in cloud-related LRU cache
463-
if(thumbnailBitmap != null) {
446+
if (thumbnailBitmap != null) {
464447
storeThumbnail(diskCache, cacheKey, thumbnailBitmap)
465448
}
466449

@@ -471,34 +454,30 @@ abstract class CryptoImplDecorator(
471454
}
472455
}
473456

474-
protected fun generateCacheKey(cloudFile: CloudFile) : String{
457+
protected fun generateCacheKey(cloudFile: CloudFile): String {
475458
return buildString {
476-
// distinguish between two files with same path but on different instances of the same cloud
477459
if (cloudFile.cloud?.id() != null)
478460
this.append(cloudFile.cloud!!.id())
479461
else
480-
// this.append(null obj) will add the string "null"
481462
this.append("c") // "common"
482463
this.append("-")
483464
this.append(cloudFile.path.hashCode())
484465
}
485466
}
486467

487-
private fun isGenerateThumbnailsEnabled(cache: DiskLruCache?, fileName: String) : Boolean {
488-
return sharedPreferencesHandler.useLruCache() &&
468+
private fun isGenerateThumbnailsEnabled(cache: DiskLruCache?, fileName: String): Boolean {
469+
return sharedPreferencesHandler.useLruCache() &&
489470
sharedPreferencesHandler.generateThumbnails() != ThumbnailsOption.NEVER &&
490471
cache != null &&
491472
isImageMediaType(fileName)
492473
}
493474

494-
private fun storeThumbnail(cache: DiskLruCache?, cacheKey: String, thumbnailBitmap: Bitmap){
495-
// write the thumbnail in a file (on disk)
496-
val thumbnailFile : File = File.createTempFile(UUID.randomUUID().toString(), ".thumbnail", internalCache)
475+
private fun storeThumbnail(cache: DiskLruCache?, cacheKey: String, thumbnailBitmap: Bitmap) {
476+
val thumbnailFile: File = File.createTempFile(UUID.randomUUID().toString(), ".thumbnail", internalCache)
497477
thumbnailBitmap.compress(Bitmap.CompressFormat.JPEG, 100, thumbnailFile.outputStream())
498478

499479
try {
500480
cache?.let {
501-
// store File to LruCache (on disk)
502481
LruFileCacheUtil.storeToLruCache(it, cacheKey, thumbnailFile)
503482
} ?: Timber.tag("CryptoImplDecorator").e("Failed to store item in LRU cache")
504483
} catch (e: IOException) {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ open class CryptoImplVaultFormat7 : CryptoImplDecorator {
8787
val shortFileName = BaseEncoding.base64Url().encode(hash) + LONG_NODE_FILE_EXT
8888
var dirFolder = cloudContentRepository.folder(getOrCreateCachingAwareDirIdInfo(cryptoParent).cloudFolder, shortFileName)
8989

90-
// if folder already exists in case of renaming
9190
if (!cloudContentRepository.exists(dirFolder)) {
9291
dirFolder = cloudContentRepository.create(dirFolder)
9392
}
@@ -167,7 +166,6 @@ open class CryptoImplVaultFormat7 : CryptoImplDecorator {
167166
}.map { node ->
168167
ciphertextToCleartextNode(cryptoFolder, dirId, node)
169168
}.onEach { cryptoNode ->
170-
// if present, associate cached-thumbnail to the Cryptofile
171169
if (cryptoNode is CryptoFile && isImageMediaType(cryptoNode.name)) {
172170
val cacheKey = generateCacheKey(cryptoNode.cloudFile)
173171
cryptoNode.cloudFile.cloud?.type()?.let { cloudType ->
@@ -463,7 +461,6 @@ open class CryptoImplVaultFormat7 : CryptoImplDecorator {
463461
cloudContentRepository.delete(node.cloudFile)
464462
}
465463

466-
// Delete thumbnail file from cache
467464
val cacheKey = generateCacheKey(node.cloudFile)
468465
node.cloudFile.cloud?.type()?.let { cloudType ->
469466
getLruCacheFor(cloudType)?.let { diskCache ->

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ internal class CryptoImplVaultFormatPre7(
129129
.map { node ->
130130
ciphertextToCleartextNode(cryptoFolder, dirId, node)
131131
}.onEach { cryptoNode ->
132-
// if present, associate cached-thumbnail to the Cryptofile
133132
if (cryptoNode is CryptoFile && isImageMediaType(cryptoNode.name)) {
134133
val cacheKey = generateCacheKey(cryptoNode.cloudFile)
135134
cryptoNode.cloudFile.cloud?.type()?.let { cloudType ->
@@ -262,11 +261,10 @@ internal class CryptoImplVaultFormatPre7(
262261
} else if (node is CryptoFile) {
263262
cloudContentRepository.delete(node.cloudFile)
264263

265-
// Delete thumbnail file from cache
266264
val cacheKey = generateCacheKey(node.cloudFile)
267-
node.cloudFile.cloud?.type()?.let{ cloudType ->
265+
node.cloudFile.cloud?.type()?.let { cloudType ->
268266
getLruCacheFor(cloudType)?.let { diskCache ->
269-
if(diskCache[cacheKey] != null) {
267+
if (diskCache[cacheKey] != null) {
270268
diskCache.delete(cacheKey)
271269
}
272270
}

0 commit comments

Comments
 (0)