Опциональное шифрование имён файлов
This commit is contained in:
@@ -11,6 +11,7 @@ import com.github.nullptroma.wallenc.domain.interfaces.IUnlockManager
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
@@ -22,7 +23,6 @@ import java.util.UUID
|
||||
|
||||
class UnlockManager(
|
||||
private val keymapRepository: StorageKeyMapRepository,
|
||||
private val metaInfoRepository: StorageMetaInfoRepository,
|
||||
private val ioDispatcher: CoroutineDispatcher,
|
||||
vaultsManager: IVaultsManager
|
||||
) : IUnlockManager {
|
||||
@@ -36,23 +36,32 @@ class UnlockManager(
|
||||
vaultsManager.allStorages.collectLatest {
|
||||
mutex.lock()
|
||||
val allKeys = keymapRepository.getAll()
|
||||
val usedKeys = mutableListOf<StorageKeyMap>()
|
||||
val keysToRemove = mutableListOf<StorageKeyMap>()
|
||||
val allStorages = it.associateBy({ it.uuid }, { it })
|
||||
val allStorages = it.toMutableList()
|
||||
val map = _openedStorages.value?.toMutableMap() ?: mutableMapOf()
|
||||
for(keymap in allKeys) {
|
||||
if(map.contains(keymap.sourceUuid))
|
||||
while(allStorages.size > 0) {
|
||||
val storage = allStorages[allStorages.size-1]
|
||||
val key = allKeys.find { key -> key.sourceUuid == storage.uuid }
|
||||
if(key == null) {
|
||||
allStorages.removeAt(allStorages.size - 1)
|
||||
continue
|
||||
}
|
||||
try {
|
||||
val storage = allStorages[keymap.sourceUuid] ?: continue
|
||||
val encStorage = createEncryptedStorage(storage, keymap.key, keymap.destUuid)
|
||||
val encStorage = createEncryptedStorage(storage, key.key, key.destUuid)
|
||||
map[storage.uuid] = encStorage
|
||||
usedKeys.add(key)
|
||||
allStorages.removeAt(allStorages.size - 1)
|
||||
allStorages.add(encStorage)
|
||||
}
|
||||
catch (_: Exception) {
|
||||
keysToRemove.add(keymap)
|
||||
// ключ не подошёл
|
||||
keysToRemove.add(key)
|
||||
allStorages.removeAt(allStorages.size - 1)
|
||||
}
|
||||
}
|
||||
_openedStorages.value = map
|
||||
keymapRepository.delete(*keysToRemove.toTypedArray()) // удалить мёртвые ключи
|
||||
_openedStorages.value = map.toMap()
|
||||
mutex.unlock()
|
||||
}
|
||||
}
|
||||
@@ -63,7 +72,6 @@ class UnlockManager(
|
||||
source = storage,
|
||||
key = key,
|
||||
ioDispatcher = ioDispatcher,
|
||||
metaInfoProvider = metaInfoRepository.createSingleStorageProvider(uuid),
|
||||
uuid = uuid
|
||||
)
|
||||
}
|
||||
@@ -71,7 +79,7 @@ class UnlockManager(
|
||||
override suspend fun open(
|
||||
storage: IStorage,
|
||||
key: EncryptKey
|
||||
) = withContext(ioDispatcher) {
|
||||
): EncryptedStorage = withContext(ioDispatcher) {
|
||||
mutex.lock()
|
||||
val encInfo = storage.metaInfo.value.encInfo ?: throw Exception("EncInfo is null") // TODO
|
||||
if (!Encryptor.checkKey(key, encInfo))
|
||||
@@ -92,6 +100,7 @@ class UnlockManager(
|
||||
_openedStorages.value = opened
|
||||
keymapRepository.add(keymap)
|
||||
mutex.unlock()
|
||||
return@withContext encStorage
|
||||
}
|
||||
|
||||
override suspend fun close(storage: IStorage) = withContext(ioDispatcher) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.github.nullptroma.wallenc.data.storages.encrypt
|
||||
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.github.nullptroma.wallenc.data.db.app.repository.StorageMetaInfoRepository
|
||||
import com.github.nullptroma.wallenc.domain.common.impl.CommonStorageMetaInfo
|
||||
import com.github.nullptroma.wallenc.domain.datatypes.EncryptKey
|
||||
@@ -13,24 +14,27 @@ import kotlinx.coroutines.DisposableHandle
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.InputStream
|
||||
import java.util.UUID
|
||||
|
||||
class EncryptedStorage private constructor(
|
||||
private val source: IStorage,
|
||||
private val key: EncryptKey,
|
||||
ioDispatcher: CoroutineDispatcher,
|
||||
private val metaInfoProvider: StorageMetaInfoRepository.SingleStorageMetaInfoProvider,
|
||||
override val uuid: UUID = UUID.randomUUID()
|
||||
) : IStorage, DisposableHandle {
|
||||
private val job = Job()
|
||||
private val scope = CoroutineScope(ioDispatcher + job)
|
||||
private val encInfo = source.metaInfo.value.encInfo ?: throw Exception("Storage is not encrypted") // TODO
|
||||
private val encInfo =
|
||||
source.metaInfo.value.encInfo ?: throw Exception("Storage is not encrypted") // TODO
|
||||
private val metaInfoFileName: String = "${uuid.toString().take(8)}$STORAGE_INFO_FILE_POSTFIX"
|
||||
|
||||
override val size: StateFlow<Long?>
|
||||
get() = source.size
|
||||
get() = accessor.size
|
||||
override val numberOfFiles: StateFlow<Int?>
|
||||
get() = source.numberOfFiles
|
||||
get() = accessor.numberOfFiles
|
||||
|
||||
private val _metaInfo = MutableStateFlow<IStorageMetaInfo>(
|
||||
CommonStorageMetaInfo()
|
||||
@@ -42,45 +46,64 @@ class EncryptedStorage private constructor(
|
||||
override val isAvailable: StateFlow<Boolean>
|
||||
get() = source.isAvailable
|
||||
override val accessor: EncryptedStorageAccessor =
|
||||
EncryptedStorageAccessor(source.accessor, encInfo.pathIv, key, scope)
|
||||
EncryptedStorageAccessor(source.accessor, encInfo.pathIv, key, "${uuid.toString().take(8)}$SYSTEM_HIDDEN_DIRNAME_POSTFIX", scope)
|
||||
|
||||
private suspend fun init() {
|
||||
checkKey()
|
||||
readMeta()
|
||||
readMetaInfo()
|
||||
}
|
||||
|
||||
private fun checkKey() {
|
||||
if(!Encryptor.checkKey(key, encInfo))
|
||||
if (!Encryptor.checkKey(key, encInfo))
|
||||
throw Exception("Incorrect key") // TODO
|
||||
}
|
||||
|
||||
private suspend fun readMeta() = scope.run {
|
||||
var meta = metaInfoProvider.get()
|
||||
if(meta == null) {
|
||||
private suspend fun readMetaInfo() = scope.run {
|
||||
var meta: CommonStorageMetaInfo
|
||||
var reader: InputStream? = null
|
||||
try {
|
||||
reader = accessor.openReadSystemFile(metaInfoFileName)
|
||||
meta = jackson.readValue(reader, CommonStorageMetaInfo::class.java)
|
||||
} catch (e: Exception) {
|
||||
// чтение не удалось, значит нужно записать файл
|
||||
meta = CommonStorageMetaInfo()
|
||||
metaInfoProvider.set(meta)
|
||||
updateMetaInfo(meta)
|
||||
} finally {
|
||||
reader?.close()
|
||||
}
|
||||
_metaInfo.value = meta
|
||||
}
|
||||
|
||||
private suspend fun updateMetaInfo(meta: IStorageMetaInfo) = scope.run {
|
||||
val writer = accessor.openWriteSystemFile(metaInfoFileName)
|
||||
try {
|
||||
jackson.writeValue(writer, meta)
|
||||
} catch (e: Exception) {
|
||||
throw e
|
||||
} finally {
|
||||
writer.close()
|
||||
}
|
||||
_metaInfo.value = meta
|
||||
}
|
||||
|
||||
override suspend fun rename(newName: String) = scope.run {
|
||||
val cur = _metaInfo.value
|
||||
val newMeta = CommonStorageMetaInfo(
|
||||
encInfo = cur.encInfo,
|
||||
name = newName
|
||||
val curMeta = metaInfo.value
|
||||
updateMetaInfo(
|
||||
CommonStorageMetaInfo(
|
||||
encInfo = curMeta.encInfo,
|
||||
name = newName
|
||||
)
|
||||
)
|
||||
_metaInfo.value = newMeta
|
||||
metaInfoProvider.set(newMeta)
|
||||
}
|
||||
|
||||
override suspend fun setEncInfo(encInfo: StorageEncryptionInfo) = scope.run {
|
||||
val cur = _metaInfo.value
|
||||
val newMeta = CommonStorageMetaInfo(
|
||||
encInfo = encInfo,
|
||||
name = cur.name
|
||||
val curMeta = metaInfo.value
|
||||
updateMetaInfo(
|
||||
CommonStorageMetaInfo(
|
||||
encInfo = encInfo,
|
||||
name = curMeta.name
|
||||
)
|
||||
)
|
||||
_metaInfo.value = newMeta
|
||||
metaInfoProvider.set(newMeta)
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
@@ -93,24 +116,25 @@ class EncryptedStorage private constructor(
|
||||
source: IStorage,
|
||||
key: EncryptKey,
|
||||
ioDispatcher: CoroutineDispatcher,
|
||||
metaInfoProvider: StorageMetaInfoRepository.SingleStorageMetaInfoProvider,
|
||||
uuid: UUID = UUID.randomUUID()
|
||||
): EncryptedStorage = withContext(ioDispatcher) {
|
||||
val storage = EncryptedStorage(
|
||||
source = source,
|
||||
key = key,
|
||||
ioDispatcher = ioDispatcher,
|
||||
metaInfoProvider = metaInfoProvider,
|
||||
uuid = uuid
|
||||
)
|
||||
try {
|
||||
storage.init()
|
||||
}
|
||||
catch (e: Exception) {
|
||||
} catch (e: Exception) {
|
||||
storage.dispose()
|
||||
throw e
|
||||
}
|
||||
return@withContext storage
|
||||
}
|
||||
|
||||
private const val SYSTEM_HIDDEN_DIRNAME_POSTFIX = "-enc-dir"
|
||||
const val STORAGE_INFO_FILE_POSTFIX = ".enc-meta"
|
||||
private val jackson = jacksonObjectMapper().apply { findAndRegisterModules() }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.nullptroma.wallenc.data.storages.encrypt
|
||||
|
||||
import android.util.Log
|
||||
import com.github.nullptroma.wallenc.data.utils.CloseHandledStreamExtension.Companion.onClosed
|
||||
import com.github.nullptroma.wallenc.data.utils.CloseHandledStreamExtension.Companion.onClosing
|
||||
import com.github.nullptroma.wallenc.domain.common.impl.CommonDirectory
|
||||
import com.github.nullptroma.wallenc.domain.common.impl.CommonFile
|
||||
import com.github.nullptroma.wallenc.domain.common.impl.CommonMetaInfo
|
||||
@@ -16,6 +17,7 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.DisposableHandle
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
@@ -27,12 +29,17 @@ import kotlin.io.path.pathString
|
||||
|
||||
class EncryptedStorageAccessor(
|
||||
private val source: IStorageAccessor,
|
||||
pathIv: ByteArray,
|
||||
pathIv: ByteArray?,
|
||||
key: EncryptKey,
|
||||
private val systemHiddenDirName: String,
|
||||
private val scope: CoroutineScope
|
||||
) : IStorageAccessor, DisposableHandle {
|
||||
override val size: StateFlow<Long?> = source.size
|
||||
override val numberOfFiles: StateFlow<Int?> = source.numberOfFiles
|
||||
private val _size = MutableStateFlow<Long?>(null)
|
||||
override val size: StateFlow<Long?> = _size
|
||||
|
||||
private val _numberOfFiles = MutableStateFlow<Int?>(null)
|
||||
override val numberOfFiles: StateFlow<Int?> = _numberOfFiles
|
||||
|
||||
override val isAvailable: StateFlow<Boolean> = source.isAvailable
|
||||
|
||||
private val _filesUpdates = MutableSharedFlow<DataPackage<List<IFile>>>()
|
||||
@@ -42,46 +49,70 @@ class EncryptedStorageAccessor(
|
||||
override val dirsUpdates: SharedFlow<DataPackage<List<IDirectory>>> = _dirsUpdates
|
||||
|
||||
private val dataEncryptor = Encryptor(key.toAesKey())
|
||||
private val pathEncryptor = EncryptorWithStaticIv(key.toAesKey(), pathIv)
|
||||
private val pathEncryptor: EncryptorWithStaticIv? = if(pathIv != null) EncryptorWithStaticIv(key.toAesKey(), pathIv) else null
|
||||
|
||||
private var systemHiddenFiles: List<IFile>? = null
|
||||
private var systemHiddenFilesIsActual = false
|
||||
|
||||
init {
|
||||
collectSourceState()
|
||||
|
||||
for(i in 1..5) {
|
||||
val orig = "/hello/path/test.txt"
|
||||
val enc = encryptPath(orig)
|
||||
val dec = decryptPath(enc)
|
||||
|
||||
Log.d("MyTag", "Path $orig to $enc to $dec")
|
||||
}
|
||||
}
|
||||
|
||||
private fun collectSourceState() {
|
||||
scope.launch {
|
||||
launch {
|
||||
source.filesUpdates.collect {
|
||||
val files = it.data.map(::decryptEntity)
|
||||
_filesUpdates.emit(DataPackage(
|
||||
data = files,
|
||||
isLoading = it.isLoading,
|
||||
isError = it.isError
|
||||
))
|
||||
val files = it.data.map(::decryptEntity).filterSystemHiddenFiles()
|
||||
_filesUpdates.emit(
|
||||
DataPackage(
|
||||
data = files,
|
||||
isLoading = it.isLoading,
|
||||
isError = it.isError
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
source.dirsUpdates.collect {
|
||||
val dirs = it.data.map(::decryptEntity)
|
||||
_dirsUpdates.emit(DataPackage(
|
||||
data = dirs,
|
||||
isLoading = it.isLoading,
|
||||
isError = it.isError
|
||||
))
|
||||
val dirs = it.data.map(::decryptEntity).filterSystemHiddenDirs()
|
||||
_dirsUpdates.emit(
|
||||
DataPackage(
|
||||
data = dirs,
|
||||
isLoading = it.isLoading,
|
||||
isError = it.isError
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
source.numberOfFiles.collect {
|
||||
if(it == null)
|
||||
_numberOfFiles.value = null
|
||||
else
|
||||
{
|
||||
_numberOfFiles.value = it - getSystemFiles().size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
launch {
|
||||
source.size.collect { sourceSize ->
|
||||
if(sourceSize == null)
|
||||
_size.value = null
|
||||
else
|
||||
{
|
||||
_size.value = sourceSize - getSystemFiles().sumOf { it.metaInfo.size }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getSystemFiles(): List<IFile> {
|
||||
return source.getFiles(encryptPath(systemHiddenDirName))
|
||||
}
|
||||
|
||||
private fun encryptEntity(file: IFile): IFile {
|
||||
return CommonFile(encryptMeta(file.metaInfo))
|
||||
}
|
||||
@@ -119,35 +150,40 @@ class EncryptedStorageAccessor(
|
||||
}
|
||||
|
||||
private fun encryptPath(pathStr: String): String {
|
||||
if(pathEncryptor == null)
|
||||
return pathStr
|
||||
val path = Path(pathStr)
|
||||
val segments = mutableListOf<String>()
|
||||
for (segment in path)
|
||||
segments.add(pathEncryptor.encryptString(segment.pathString))
|
||||
val res = Path("/",*(segments.toTypedArray()))
|
||||
val res = Path("/", *(segments.toTypedArray()))
|
||||
return res.pathString
|
||||
}
|
||||
|
||||
private fun decryptPath(pathStr: String): String {
|
||||
if(pathEncryptor == null)
|
||||
return pathStr
|
||||
|
||||
val path = Path(pathStr)
|
||||
val segments = mutableListOf<String>()
|
||||
for (segment in path)
|
||||
segments.add(pathEncryptor.decryptString(segment.pathString))
|
||||
val res = Path("/",*(segments.toTypedArray()))
|
||||
val res = Path("/", *(segments.toTypedArray()))
|
||||
return res.pathString
|
||||
}
|
||||
|
||||
override suspend fun getAllFiles(): List<IFile> {
|
||||
return source.getAllFiles().map(::decryptEntity)
|
||||
return source.getAllFiles().map(::decryptEntity).filterSystemHiddenFiles()
|
||||
}
|
||||
|
||||
override suspend fun getFiles(path: String): List<IFile> {
|
||||
return source.getFiles(encryptPath(path)).map(::decryptEntity)
|
||||
return source.getFiles(encryptPath(path)).map(::decryptEntity).filterSystemHiddenFiles()
|
||||
}
|
||||
|
||||
override fun getFilesFlow(path: String): Flow<DataPackage<List<IFile>>> {
|
||||
val flow = source.getFilesFlow(encryptPath(path)).map {
|
||||
DataPackage(
|
||||
data = it.data.map(::decryptEntity),
|
||||
data = it.data.map(::decryptEntity).filterSystemHiddenFiles(),
|
||||
isLoading = it.isLoading,
|
||||
isError = it.isError
|
||||
)
|
||||
@@ -156,17 +192,18 @@ class EncryptedStorageAccessor(
|
||||
}
|
||||
|
||||
override suspend fun getAllDirs(): List<IDirectory> {
|
||||
return source.getAllDirs().map(::decryptEntity)
|
||||
return source.getAllDirs().map(::decryptEntity).filterSystemHiddenDirs()
|
||||
}
|
||||
|
||||
override suspend fun getDirs(path: String): List<IDirectory> {
|
||||
return source.getDirs(encryptPath(path)).map(::decryptEntity)
|
||||
return source.getDirs(encryptPath(path)).map(::decryptEntity).filterSystemHiddenDirs()
|
||||
}
|
||||
|
||||
override fun getDirsFlow(path: String): Flow<DataPackage<List<IDirectory>>> {
|
||||
val flow = source.getDirsFlow(encryptPath(path)).map {
|
||||
DataPackage(
|
||||
data = it.data.map(::decryptEntity),
|
||||
// включать все папки, кроме системной
|
||||
data = it.data.map(::decryptEntity).filterSystemHiddenDirs(),
|
||||
isLoading = it.isLoading,
|
||||
isError = it.isError
|
||||
)
|
||||
@@ -220,4 +257,33 @@ class EncryptedStorageAccessor(
|
||||
dataEncryptor.dispose()
|
||||
}
|
||||
|
||||
suspend fun openReadSystemFile(name: String): InputStream = scope.run {
|
||||
val path = Path(systemHiddenDirName, name).pathString
|
||||
return@run openRead(path)
|
||||
}
|
||||
|
||||
suspend fun openWriteSystemFile(name: String): OutputStream = scope.run {
|
||||
val path = Path(systemHiddenDirName, name).pathString
|
||||
systemHiddenFilesIsActual = false
|
||||
return@run openWrite(path).onClosing {
|
||||
systemHiddenFilesIsActual = false
|
||||
}
|
||||
}
|
||||
|
||||
private fun Iterable<IFile>.filterSystemHiddenFiles(): List<IFile> {
|
||||
return this.filter { file ->
|
||||
!file.metaInfo.path.contains(
|
||||
systemHiddenDirName
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Iterable<IDirectory>.filterSystemHiddenDirs(): List<IDirectory> {
|
||||
return this.filter { dir ->
|
||||
!dir.metaInfo.path.contains(
|
||||
systemHiddenDirName
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package com.github.nullptroma.wallenc.data.storages.local
|
||||
import com.fasterxml.jackson.core.JacksonException
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import com.github.nullptroma.wallenc.data.utils.CloseHandledStreamExtension.Companion.onClose
|
||||
import com.github.nullptroma.wallenc.data.utils.CloseHandledStreamExtension.Companion.onClosed
|
||||
import com.github.nullptroma.wallenc.domain.common.impl.CommonDirectory
|
||||
import com.github.nullptroma.wallenc.domain.common.impl.CommonFile
|
||||
import com.github.nullptroma.wallenc.domain.common.impl.CommonMetaInfo
|
||||
@@ -433,6 +433,8 @@ class LocalStorageAccessor(
|
||||
if (file.exists() && file.isDirectory) {
|
||||
throw Exception("Что то пошло не так") // TODO
|
||||
} else if(!file.exists()) {
|
||||
val parent = Path(storagePath).parent
|
||||
createDir(parent.pathString)
|
||||
file.createNewFile()
|
||||
|
||||
val cur = _numberOfFiles.value
|
||||
@@ -441,7 +443,7 @@ class LocalStorageAccessor(
|
||||
|
||||
val pair = LocalStorageFilePair.from(_filesystemBasePath, file)
|
||||
?: throw Exception("Что то пошло не так") // TODO
|
||||
val newMeta = pair.meta.copy(lastModified = Clock.systemUTC().instant())
|
||||
val newMeta = pair.meta.copy(lastModified = Clock.systemUTC().instant(), size = Files.size(pair.file.toPath()))
|
||||
writeMeta(pair.metaFile, newMeta)
|
||||
_filesUpdates.emit(
|
||||
DataPage(
|
||||
@@ -503,9 +505,10 @@ class LocalStorageAccessor(
|
||||
touchFile(path)
|
||||
val pair = LocalStorageFilePair.from(_filesystemBasePath, path)
|
||||
?: throw Exception("Файла нет") // TODO
|
||||
return@withContext pair.file.outputStream().onClose {
|
||||
return@withContext pair.file.outputStream().onClosed {
|
||||
CoroutineScope(ioDispatcher).launch {
|
||||
touchFile(path)
|
||||
scanSizeAndNumOfFiles()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.OutputStream
|
||||
|
||||
private class CloseHandledOutputStream(
|
||||
private val stream: OutputStream,
|
||||
private val onClosing: () -> Unit,
|
||||
private val onClose: () -> Unit
|
||||
) : OutputStream() {
|
||||
|
||||
@@ -25,6 +26,7 @@ private class CloseHandledOutputStream(
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
onClosing()
|
||||
try {
|
||||
stream.close()
|
||||
} finally {
|
||||
@@ -35,6 +37,7 @@ private class CloseHandledOutputStream(
|
||||
|
||||
private class CloseHandledInputStream(
|
||||
private val stream: InputStream,
|
||||
private val onClosing: () -> Unit,
|
||||
private val onClose: () -> Unit
|
||||
) : InputStream() {
|
||||
|
||||
@@ -59,6 +62,7 @@ private class CloseHandledInputStream(
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
onClosing()
|
||||
try {
|
||||
stream.close()
|
||||
} finally {
|
||||
@@ -81,12 +85,20 @@ private class CloseHandledInputStream(
|
||||
|
||||
class CloseHandledStreamExtension {
|
||||
companion object {
|
||||
fun OutputStream.onClose(callback: ()->Unit): OutputStream {
|
||||
return CloseHandledOutputStream(this, callback)
|
||||
fun OutputStream.onClosed(callback: ()->Unit): OutputStream {
|
||||
return CloseHandledOutputStream(this, {}, callback)
|
||||
}
|
||||
|
||||
fun InputStream.onClose(callback: ()->Unit): InputStream {
|
||||
return CloseHandledInputStream(this, callback)
|
||||
fun InputStream.onClosed(callback: ()->Unit): InputStream {
|
||||
return CloseHandledInputStream(this, {}, callback)
|
||||
}
|
||||
|
||||
fun OutputStream.onClosing(callback: ()->Unit): OutputStream {
|
||||
return CloseHandledOutputStream(this, callback, {})
|
||||
}
|
||||
|
||||
fun InputStream.onClosing(callback: ()->Unit): InputStream {
|
||||
return CloseHandledInputStream(this, callback, {})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user