mirror of
https://github.com/fromchat-messenger/app.git
synced 2026-09-22 19:15:05 +03:00
+32
@@ -0,0 +1,32 @@
|
||||
package com.pr0gramm3r101.utils.files
|
||||
|
||||
import com.pr0gramm3r101.utils.UtilsLibrary
|
||||
import java.io.File
|
||||
|
||||
internal actual fun expectExists(path: String): Boolean =
|
||||
File(path).exists()
|
||||
|
||||
internal actual fun expectWriteBytes(path: String, bytes: ByteArray) {
|
||||
File(path).writeBytes(bytes)
|
||||
}
|
||||
|
||||
internal actual fun expectDelete(path: String) {
|
||||
File(path).delete()
|
||||
}
|
||||
|
||||
internal actual fun expectDeleteFilesWithPrefix(dirPath: String, namePrefix: String) {
|
||||
val dir = File(dirPath)
|
||||
if (!dir.exists()) return
|
||||
dir.listFiles()?.forEach { file ->
|
||||
if (file.name.startsWith(namePrefix)) {
|
||||
file.delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal actual fun expectGetAppCacheDirectory(): String =
|
||||
UtilsLibrary.context.cacheDir.absolutePath
|
||||
|
||||
internal actual fun expectEnsureDirectory(path: String) {
|
||||
File(path).mkdirs()
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.pr0gramm3r101.utils.files
|
||||
|
||||
/**
|
||||
* Multiplatform file system API for basic file operations.
|
||||
* Use [getAppCacheDirectory] to obtain the platform cache directory.
|
||||
*/
|
||||
object PlatformFileSystem {
|
||||
|
||||
/**
|
||||
* Returns true if a file exists at the given path.
|
||||
*/
|
||||
fun exists(path: String): Boolean = expectExists(path)
|
||||
|
||||
/**
|
||||
* Writes [bytes] to the file at [path], overwriting if it exists.
|
||||
*/
|
||||
fun writeBytes(path: String, bytes: ByteArray) {
|
||||
expectWriteBytes(path, bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the file at [path]. No-op if the file does not exist.
|
||||
*/
|
||||
fun delete(path: String) {
|
||||
expectDelete(path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all files in the directory [dirPath] whose filename starts with [namePrefix].
|
||||
*/
|
||||
fun deleteFilesWithPrefix(dirPath: String, namePrefix: String) {
|
||||
expectDeleteFilesWithPrefix(dirPath, namePrefix)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the platform-specific application cache directory path.
|
||||
* On Android: context.cacheDir (requires UtilsLibrary.init to be called).
|
||||
* On iOS: NSCachesDirectory.
|
||||
*/
|
||||
fun getAppCacheDirectory(): String = expectGetAppCacheDirectory()
|
||||
|
||||
/**
|
||||
* Creates the directory at [path] if it does not exist.
|
||||
* Returns the absolute path.
|
||||
*/
|
||||
fun ensureDirectory(path: String): String {
|
||||
expectEnsureDirectory(path)
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
||||
internal expect fun expectExists(path: String): Boolean
|
||||
internal expect fun expectWriteBytes(path: String, bytes: ByteArray)
|
||||
internal expect fun expectDelete(path: String)
|
||||
internal expect fun expectDeleteFilesWithPrefix(dirPath: String, namePrefix: String)
|
||||
internal expect fun expectGetAppCacheDirectory(): String
|
||||
internal expect fun expectEnsureDirectory(path: String)
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.pr0gramm3r101.utils.files
|
||||
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
import kotlinx.cinterop.addressOf
|
||||
import kotlinx.cinterop.usePinned
|
||||
import platform.Foundation.NSData
|
||||
import platform.Foundation.NSFileManager
|
||||
import platform.Foundation.NSCachesDirectory
|
||||
import platform.Foundation.NSSearchPathForDirectoriesInDomains
|
||||
import platform.Foundation.NSUserDomainMask
|
||||
import platform.Foundation.create
|
||||
import platform.Foundation.writeToFile
|
||||
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
internal actual fun expectExists(path: String): Boolean =
|
||||
NSFileManager.defaultManager.fileExistsAtPath(path)
|
||||
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
internal actual fun expectWriteBytes(path: String, bytes: ByteArray) {
|
||||
val nsData = bytes.usePinned { pinned ->
|
||||
NSData.create(bytes = pinned.addressOf(0), length = bytes.size.toULong())
|
||||
}
|
||||
nsData?.writeToFile(path, true)
|
||||
}
|
||||
|
||||
internal actual fun expectDelete(path: String) {
|
||||
NSFileManager.defaultManager.removeItemAtPath(path, null)
|
||||
}
|
||||
|
||||
internal actual fun expectDeleteFilesWithPrefix(dirPath: String, namePrefix: String) {
|
||||
val contents = NSFileManager.defaultManager.contentsOfDirectoryAtPath(dirPath, null)
|
||||
?: return
|
||||
(contents as List<*>).filterIsInstance<String>().forEach { name ->
|
||||
if (name.startsWith(namePrefix)) {
|
||||
NSFileManager.defaultManager.removeItemAtPath("$dirPath/$name", null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal actual fun expectGetAppCacheDirectory(): String {
|
||||
val paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)
|
||||
return (paths.firstOrNull() as? String) ?: ""
|
||||
}
|
||||
|
||||
internal actual fun expectEnsureDirectory(path: String) {
|
||||
NSFileManager.defaultManager.createDirectoryAtPath(path, true, null, null)
|
||||
}
|
||||
Reference in New Issue
Block a user