Skip to content

Commit

Permalink
Use same logic for android and ios to create path for unzipping file
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Jan 26, 2025
1 parent f2292ea commit 521b132
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 31 deletions.
1 change: 0 additions & 1 deletion core/io/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ kotlin {

commonMain {
dependencies {
implementation(projects.core.appInfo)
implementation(projects.core.di)
implementation(projects.core.log)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,21 @@ 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

/**
* 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")

Expand All @@ -51,7 +34,7 @@ internal class RealZipFileManager(
log("\t" + paths.joinToString("\n"))
// endregion Debugging Code end

unpackZipToDirectory(zipPath, destDir, prefix = filePrefix)
unpackZipToDirectory(zipPath, destDir)
}

/**
Expand All @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@ import xyz.ksharma.krail.core.io.ZipFileManager
expect val fileStorageModule: Module

val ioModule = module {
single<ZipFileManager> {
RealZipFileManager(
appInfoProvider = get(),
)
}
single<ZipFileManager> { RealZipFileManager() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 521b132

Please sign in to comment.