Dispose для EncryptedStorage
This commit is contained in:
@@ -4,8 +4,8 @@ import com.github.nullptroma.wallenc.domain.datatypes.EncryptKey
|
||||
import com.github.nullptroma.wallenc.domain.datatypes.StorageEncryptionInfo
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.ILogger
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorage
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorageAccessor
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.DisposableHandle
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import java.util.UUID
|
||||
|
||||
@@ -14,7 +14,7 @@ class EncryptedStorage(
|
||||
key: EncryptKey,
|
||||
logger: ILogger,
|
||||
ioDispatcher: CoroutineDispatcher,
|
||||
) : IStorage {
|
||||
) : IStorage, DisposableHandle {
|
||||
override val size: StateFlow<Long?>
|
||||
get() = TODO("Not yet implemented")
|
||||
override val numberOfFiles: StateFlow<Int?>
|
||||
@@ -27,10 +27,14 @@ class EncryptedStorage(
|
||||
get() = TODO("Not yet implemented")
|
||||
override val encInfo: StateFlow<StorageEncryptionInfo>
|
||||
get() = TODO("Not yet implemented")
|
||||
override val accessor: IStorageAccessor =
|
||||
override val accessor: EncryptedStorageAccessor =
|
||||
EncryptedStorageAccessor(source.accessor, key, logger, ioDispatcher)
|
||||
|
||||
override suspend fun rename(newName: String) {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
accessor.dispose()
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import com.github.nullptroma.wallenc.domain.interfaces.IMetaInfo
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorageAccessor
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.DisposableHandle
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
@@ -36,7 +38,10 @@ class EncryptedStorageAccessor(
|
||||
key: EncryptKey,
|
||||
private val logger: ILogger,
|
||||
ioDispatcher: CoroutineDispatcher
|
||||
) : IStorageAccessor {
|
||||
) : IStorageAccessor, DisposableHandle {
|
||||
private val _job = Job()
|
||||
private val _scope = CoroutineScope(ioDispatcher + _job)
|
||||
|
||||
override val size: StateFlow<Long?> = source.size
|
||||
override val numberOfFiles: StateFlow<Int?> = source.numberOfFiles
|
||||
override val isAvailable: StateFlow<Boolean> = source.isAvailable
|
||||
@@ -50,11 +55,11 @@ class EncryptedStorageAccessor(
|
||||
private val _secretKey = SecretKeySpec(key.to32Bytes(), "AES")
|
||||
|
||||
init {
|
||||
collectSourceState(CoroutineScope(ioDispatcher))
|
||||
collectSourceState()
|
||||
}
|
||||
|
||||
private fun collectSourceState(coroutineScope: CoroutineScope) {
|
||||
coroutineScope.launch {
|
||||
private fun collectSourceState() {
|
||||
_scope.launch {
|
||||
launch {
|
||||
source.filesUpdates.collect {
|
||||
val files = it.data.map(::decryptEntity)
|
||||
@@ -246,6 +251,11 @@ class EncryptedStorageAccessor(
|
||||
source.moveToTrash(encryptPath(path))
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
_job.cancel()
|
||||
// TODO сделать удаление ключа, чтобы нельзя было вызвать ни один из методов
|
||||
}
|
||||
|
||||
|
||||
companion object {
|
||||
private const val IV_LEN = 16
|
||||
|
||||
@@ -11,5 +11,5 @@ interface IUnlockManager {
|
||||
val openedStorages: StateFlow<Map<UUID, IStorage>>
|
||||
|
||||
fun open(storage: IStorage, key: EncryptKey)
|
||||
fun close(storage: IStorage)
|
||||
fun close(storage: UUID)
|
||||
}
|
||||
Reference in New Issue
Block a user