Skip to content

Commit

Permalink
Return okio.Path from FileStorage.saveFile
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Jan 27, 2025
1 parent e86fef5 commit b1b6d38
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ actual fun httpClient(appInfoProvider: AppInfoProvider): HttpClient {
expectSuccess = true
install(ContentNegotiation) {
json(Json {

ignoreUnknownKeys = true
isLenient = true
prettyPrint = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ actual fun httpClient(appInfoProvider: AppInfoProvider): HttpClient {
level = LogLevel.BODY
logger = object : Logger {
override fun log(message: String) {
println(message)
// println(message)
}
}
sanitizeHeader { header -> header == HttpHeaders.Authorization }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ package xyz.ksharma.krail.gtfs_static
import android.content.Context
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.crashlytics.crashlytics
import okio.FileSystem
import okio.Path
import okio.Path.Companion.toOkioPath
import okio.buffer
import xyz.ksharma.krail.core.log.log
import java.io.File

class AndroidFileStorage(private val context: Context) : FileStorage {
override suspend fun saveFile(fileName: String, data: ByteArray) {
runCatching {

private val fileSystem = FileSystem.SYSTEM

override suspend fun saveFile(fileName: String, data: ByteArray): Path {
return runCatching {
println("Try Saving file: $fileName")
val file = File(context.filesDir, fileName)
file.writeBytes(data)
println("File saved at: ${file.absolutePath}")
readZip(file.absolutePath)
val filePath: Path = context.filesDir.toPath().resolve(fileName).toOkioPath()
fileSystem.sink(filePath).buffer().use { it.write(data) }
println("File saved at: $filePath")
filePath
}.onFailure {
log("Failed to save file: $it")
Firebase.crashlytics.recordException(it)
}.onSuccess {
log("File saved at: ${context.filesDir.absolutePath}")
}
}.getOrThrow()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package xyz.ksharma.krail.gtfs_static

import okio.Path

interface FileStorage {
suspend fun saveFile(fileName: String, data: ByteArray)

/**
* Save a file with the given name and data
*
* @param fileName the name of the file to save
* @param data the data to save
*
* @return the path where the file was saved
*/
suspend fun saveFile(fileName: String, data: ByteArray) : Path
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.readRawBytes
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import okio.Path
import xyz.ksharma.krail.core.di.DispatchersComponent
import xyz.ksharma.krail.core.log.log

Expand All @@ -22,7 +23,8 @@ internal class RealNswGtfsService(
if (response.status.value == 200) {
log("Downloading file: ")
val data = response.readRawBytes()
fileStorage.saveFile("sydneytrains.zip", data)
val path: Path = fileStorage.saveFile("sydneytrains.zip", data)
readZip(path)
log("File downloaded")
} else {
throw Exception("Failed to download file: ${response.status}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import okio.openZip
import okio.use
import xyz.ksharma.krail.core.log.log

fun readZip(zipPath: String) {
fun readZip(zipPath: Path) {
println("Reading Zip: $zipPath")

// Use the same directory as the zip file for unpacking
val zipFilePath = zipPath.toPath()
val parentDir = zipFilePath.parent ?: throw IllegalArgumentException("Invalid path: $zipPath")
val parentDir = zipPath.parent ?: throw IllegalArgumentException("Invalid path: $zipPath")
val destDir = parentDir.resolve("sydneyTrains")

// Create the "bus" directory
fileSystem.createDirectories(destDir)

// Unpack the zip into the "bus" directory
unpackZip(zipFilePath, destDir)
unpackZip(zipPath, destDir)
}

fun unpackZip(zipFile: Path, destDir: Path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import dev.gitlive.firebase.crashlytics.crashlytics
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned
import okio.Path
import okio.Path.Companion.toPath
import platform.Foundation.NSData
import platform.Foundation.NSDocumentDirectory
import platform.Foundation.NSFileManager
Expand All @@ -14,10 +16,11 @@ import platform.Foundation.writeToFile
import xyz.ksharma.krail.core.log.log

class IosFileStorage : FileStorage {

@OptIn(ExperimentalForeignApi::class)
override suspend fun saveFile(fileName: String, data: ByteArray) {
override suspend fun saveFile(fileName: String, data: ByteArray): Path {
log("Trying to Save file: $fileName")
runCatching {
return runCatching {
val fileManager = NSFileManager.defaultManager
val directory = fileManager.URLForDirectory(
directory = NSDocumentDirectory,
Expand All @@ -33,11 +36,10 @@ class IosFileStorage : FileStorage {
.writeToFile(filePath, true)
}
log("File saved: $filePath")
filePath.toPath()
}.onFailure {
log("Failed to save file: $it")
Firebase.crashlytics.recordException(it) // todo - move to another module
}.onSuccess {
log("File saved successfully")
}
}.getOrThrow()
}
}

0 comments on commit b1b6d38

Please sign in to comment.