From 521b132ceda79241b9fd50f5a531102e3e97283f Mon Sep 17 00:00:00 2001 From: Karan Sharma <55722391+ksharma-xyz@users.noreply.github.com> Date: Sun, 26 Jan 2025 22:00:31 +1100 Subject: [PATCH] Use same logic for android and ios to create path for unzipping file --- core/io/build.gradle.kts | 1 - .../krail/core/io/RealZipFileManager.kt | 34 ++++++------------- .../xyz/ksharma/krail/core/io/di/IOModule.kt | 6 +--- .../krail/gtfs_static/RealNswGtfsService.kt | 1 - 4 files changed, 11 insertions(+), 31 deletions(-) diff --git a/core/io/build.gradle.kts b/core/io/build.gradle.kts index 49cf0f9b..5a6f3926 100644 --- a/core/io/build.gradle.kts +++ b/core/io/build.gradle.kts @@ -25,7 +25,6 @@ kotlin { commonMain { dependencies { - implementation(projects.core.appInfo) implementation(projects.core.di) implementation(projects.core.log) diff --git a/core/io/src/commonMain/kotlin/xyz/ksharma/krail/core/io/RealZipFileManager.kt b/core/io/src/commonMain/kotlin/xyz/ksharma/krail/core/io/RealZipFileManager.kt index 6c6f4e8f..512f884d 100644 --- a/core/io/src/commonMain/kotlin/xyz/ksharma/krail/core/io/RealZipFileManager.kt +++ b/core/io/src/commonMain/kotlin/xyz/ksharma/krail/core/io/RealZipFileManager.kt @@ -7,8 +7,6 @@ import okio.Path.Companion.toPath import okio.buffer import okio.openZip import okio.use -import xyz.ksharma.krail.core.appinfo.AppInfoProvider -import xyz.ksharma.krail.core.appinfo.DevicePlatformType import xyz.ksharma.krail.core.di.DispatchersComponent import xyz.ksharma.krail.core.log.log @@ -16,29 +14,14 @@ import xyz.ksharma.krail.core.log.log * A real implementation of [ZipFileManager] that uses Okio to unzip files. */ internal class RealZipFileManager( - private val appInfoProvider: AppInfoProvider, private val ioDispatcher: CoroutineDispatcher = DispatchersComponent().ioDispatcher, ) : ZipFileManager { override suspend fun unzip(zipPath: Path, destinationPath: Path?) = withContext(ioDispatcher) { log("Unpacking Zip: $zipPath") - var filePrefix: String? = null - - val destDir: Path = - if (appInfoProvider.getAppInfo().devicePlatformType == DevicePlatformType.IOS) { - // Note: For iOS, use the zip file name as a prefix to identify the unzipped files. - // This prevents conflicts when multiple files with the same name are extracted - // to the same directory from different zip files. - // E.g. iOS : sydneytrains.zip -> sydneytrains_*.txt - // E.g. Android : sydneytrains.zip -> sydneytrains/*.txt -// filePrefix = zipPath.name.dropExtension() + "_" - - destinationPath ?: zipPath.parent?.resolve(zipPath.name.dropExtension()) - ?: throw IllegalArgumentException("Invalid path: $zipPath") - } else { - destinationPath ?: zipPath.parent?.resolve(zipPath.name.dropExtension()) - ?: throw IllegalArgumentException("Invalid path: $zipPath") - } + + val destDir: Path = destinationPath ?: zipPath.parent?.resolve(zipPath.name.dropExtension()) + ?: throw IllegalArgumentException("Invalid path: $zipPath") log("Zip Unpack Destination: $destDir") @@ -51,7 +34,7 @@ internal class RealZipFileManager( log("\t" + paths.joinToString("\n")) // endregion Debugging Code end - unpackZipToDirectory(zipPath, destDir, prefix = filePrefix) + unpackZipToDirectory(zipPath, destDir) } /** @@ -60,7 +43,7 @@ internal class RealZipFileManager( * @param zipFile The path to the zip file to be unpacked. * @param destDir The path to the destination directory where the contents will be unpacked. */ - private suspend fun unpackZipToDirectory(zipFile: Path, destDir: Path, prefix: String? = null) = + private suspend fun unpackZipToDirectory(zipFile: Path, destDir: Path) = withContext(ioDispatcher) { // Open the zip file as a file system val zipFileSystem = fileSystem.openZip(zipFile) @@ -78,8 +61,11 @@ internal class RealZipFileManager( zipFileSystem.source(zipFilePath).buffer().use { source -> // Determine the relative file path and resolve it to the destination directory val relativeFilePath = zipFilePath.toString().trimStart('/') - val prefixedFilePath = prefix?.let { "$prefix$relativeFilePath" } ?: relativeFilePath - val fileToWrite = destDir.resolve(prefixedFilePath) + + // The resolve method in the context of file paths is used to combine two paths. + // It appends the given path to the current path, effectively creating a new path + // that represents a sub-path or a file within the current path. + val fileToWrite = destDir.resolve(relativeFilePath) log("Writing to: $fileToWrite") // Create parent directories for the destination file diff --git a/core/io/src/commonMain/kotlin/xyz/ksharma/krail/core/io/di/IOModule.kt b/core/io/src/commonMain/kotlin/xyz/ksharma/krail/core/io/di/IOModule.kt index 2bacf587..d63397e9 100644 --- a/core/io/src/commonMain/kotlin/xyz/ksharma/krail/core/io/di/IOModule.kt +++ b/core/io/src/commonMain/kotlin/xyz/ksharma/krail/core/io/di/IOModule.kt @@ -8,9 +8,5 @@ import xyz.ksharma.krail.core.io.ZipFileManager expect val fileStorageModule: Module val ioModule = module { - single { - RealZipFileManager( - appInfoProvider = get(), - ) - } + single { RealZipFileManager() } } diff --git a/gtfs-static/src/commonMain/kotlin/xyz/ksharma/krail/gtfs_static/RealNswGtfsService.kt b/gtfs-static/src/commonMain/kotlin/xyz/ksharma/krail/gtfs_static/RealNswGtfsService.kt index 645a64d8..eda469c2 100644 --- a/gtfs-static/src/commonMain/kotlin/xyz/ksharma/krail/gtfs_static/RealNswGtfsService.kt +++ b/gtfs-static/src/commonMain/kotlin/xyz/ksharma/krail/gtfs_static/RealNswGtfsService.kt @@ -12,7 +12,6 @@ import okio.Path import xyz.ksharma.krail.core.di.DispatchersComponent import xyz.ksharma.krail.core.io.FileStorage import xyz.ksharma.krail.core.io.ZipFileManager -import xyz.ksharma.krail.core.io.fileSystem import xyz.ksharma.krail.core.log.log internal class RealNswGtfsService(