Skip to content

Commit

Permalink
added runCatching Wrappings to FileStorage
Browse files Browse the repository at this point in the history
Signed-off-by: Basler182 <[email protected]>
  • Loading branch information
Basler182 committed Jun 23, 2024
1 parent 58b191d commit cf52b3e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,36 @@ class EncryptedFileStorage @Inject constructor(
).build()
}

override suspend fun saveFile(fileName: String, data: ByteArray) = withContext(ioDispatcher) {
deleteFile(fileName)
val encryptedFile = getEncryptedFile(fileName)
encryptedFile.openFileOutput().use { outputStream ->
outputStream.write(data)
override suspend fun saveFile(fileName: String, data: ByteArray): Result<Unit> =
withContext(ioDispatcher) {
runCatching {
deleteFile(fileName)
val encryptedFile = getEncryptedFile(fileName)
encryptedFile.openFileOutput().use { outputStream ->
outputStream.write(data)
}
}
}
}

override suspend fun readFile(fileName: String): ByteArray? = withContext(ioDispatcher) {
val file = File(context.filesDir, fileName)
if (!file.exists()) return@withContext null
override suspend fun readFile(fileName: String): Result<ByteArray?> =
withContext(ioDispatcher) {
runCatching {
val file = File(context.filesDir, fileName)
if (!file.exists()) return@runCatching null

val encryptedFile = getEncryptedFile(fileName)
return@withContext encryptedFile.openFileInput().use { inputStream ->
inputStream.readBytes()
val encryptedFile = getEncryptedFile(fileName)
encryptedFile.openFileInput().use { inputStream ->
inputStream.readBytes()
}
}
}
}

override suspend fun deleteFile(fileName: String) = withContext(ioDispatcher) {
val file = File(context.filesDir, fileName)
if (file.exists()) {
file.delete()
override suspend fun deleteFile(fileName: String): Result<Unit> = withContext(ioDispatcher) {
runCatching {
val file = File(context.filesDir, fileName)
if (file.exists()) {
file.delete()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.stanford.spezi.modules.storage.file

interface FileStorage {
suspend fun readFile(fileName: String): ByteArray?
suspend fun deleteFile(fileName: String)
suspend fun saveFile(fileName: String, data: ByteArray)
suspend fun readFile(fileName: String): Result<ByteArray?>
suspend fun deleteFile(fileName: String): Result<Unit>
suspend fun saveFile(fileName: String, data: ByteArray): Result<Unit>
}

0 comments on commit cf52b3e

Please sign in to comment.