Add ImageWrite to write images, move to images namespace
This commit is contained in:
@@ -7,8 +7,8 @@ import app.klottr.events.FoundEvents
|
||||
import app.klottr.net.Geocache
|
||||
import app.klottr.net.createNote
|
||||
import app.klottr.net.createNoteDoc
|
||||
import app.klottr.net.ImageHandle
|
||||
import app.klottr.net.ImageReader
|
||||
import app.klottr.images.ImageHandle
|
||||
import app.klottr.images.ImageReader
|
||||
import app.klottr.net.uploadImage
|
||||
import app.klottr.util.FoundStore
|
||||
import app.klottr.util.OutboxStore
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package app.klottr.net
|
||||
package app.klottr.images
|
||||
|
||||
import android.content.ContentResolver
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import app.klottr.net.inferUploadFilename
|
||||
import java.io.File
|
||||
|
||||
actual object ImageReader {
|
||||
@@ -0,0 +1,43 @@
|
||||
// shared/src/androidMain/kotlin/app/klottr/net/ImageWrite.android.kt
|
||||
package app.klottr.images
|
||||
|
||||
import android.content.Context
|
||||
import java.io.File
|
||||
|
||||
actual object ImageWriter {
|
||||
@Volatile
|
||||
private var appContext: Context? = null
|
||||
|
||||
actual fun init(platformContext: Any?) {
|
||||
if (platformContext is Context) appContext = platformContext.applicationContext
|
||||
}
|
||||
|
||||
private fun ensureContext(): Context? = appContext
|
||||
|
||||
actual suspend fun saveImage(
|
||||
bytes: ByteArray,
|
||||
mime: String,
|
||||
subdir: String,
|
||||
baseName: String
|
||||
): ImageSaveResult? {
|
||||
val ctx = ensureContext() ?: return null
|
||||
val root = File(ctx.filesDir, subdir).apply { mkdirs() }
|
||||
val file = File(root, baseName + extFromMime(mime))
|
||||
return try {
|
||||
file.outputStream().use { it.write(bytes) }
|
||||
ImageSaveResult(
|
||||
absolutePath = file.absolutePath,
|
||||
mime = mime,
|
||||
filename = file.name
|
||||
)
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun save(
|
||||
read: ImageReadResult,
|
||||
subdir: String,
|
||||
baseName: String
|
||||
): ImageSaveResult? = saveImage(read.bytes, read.mime, subdir, baseName)
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import app.klottr.net.ImageHandle
|
||||
|
||||
@Composable
|
||||
actual fun rememberVisualMediaPicker(): VisualMediaPicker {
|
||||
|
||||
@@ -38,7 +38,7 @@ import app.klottr.ui.WriteDocContent
|
||||
import app.klottr.util.SettingsStore
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import app.klottr.images.loadImageBitmap
|
||||
import app.klottr.net.ImageHandle
|
||||
import app.klottr.images.ImageHandle
|
||||
import app.klottr.images.rememberVisualMediaPicker
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.ExperimentalTime
|
||||
|
||||
@@ -2,8 +2,6 @@ package app.klottr.images
|
||||
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import org.jetbrains.compose.resources.decodeToImageBitmap
|
||||
import app.klottr.net.ImageHandle
|
||||
import app.klottr.net.ImageReader
|
||||
|
||||
/**
|
||||
* Platform-independent: reads image bytes via ImageReader (expect/actual),
|
||||
|
||||
46
shared/src/commonMain/kotlin/app/klottr/images/ImageWrite.kt
Normal file
46
shared/src/commonMain/kotlin/app/klottr/images/ImageWrite.kt
Normal file
@@ -0,0 +1,46 @@
|
||||
package app.klottr.images
|
||||
|
||||
data class ImageSaveResult(
|
||||
val absolutePath: String,
|
||||
val mime: String,
|
||||
val filename: String
|
||||
)
|
||||
|
||||
/**
|
||||
* Persist image bytes to a private, app-scoped directory.
|
||||
* Must be init():ed on Android with ApplicationContext, no-op on iOS (matches ImageReader).
|
||||
*/
|
||||
expect object ImageWriter {
|
||||
/** Android: pass ApplicationContext. iOS: no-op. */
|
||||
fun init(platformContext: Any?)
|
||||
|
||||
/**
|
||||
* Save raw [bytes] with [mime] to a platform-private subdirectory [subdir].
|
||||
* The concrete platform chooses the parent root (e.g. filesDir on Android, Application Support on iOS).
|
||||
* [baseName] should be a filename stem without extension; the appropriate extension is derived from [mime].
|
||||
*/
|
||||
suspend fun saveImage(
|
||||
bytes: ByteArray,
|
||||
mime: String,
|
||||
subdir: String,
|
||||
baseName: String
|
||||
): ImageSaveResult?
|
||||
|
||||
/**
|
||||
* Convenience overload for your existing ImageReader result.
|
||||
*/
|
||||
suspend fun save(
|
||||
read: ImageReadResult,
|
||||
subdir: String,
|
||||
baseName: String
|
||||
): ImageSaveResult?
|
||||
}
|
||||
|
||||
/** Shared helper: translate mime -> file extension. */
|
||||
internal fun extFromMime(mime: String): String = when {
|
||||
mime.equals("image/jpeg", true) || mime.equals("image/jpg", true) -> ".jpg"
|
||||
mime.equals("image/png", true) -> ".png"
|
||||
mime.equals("image/webp", true) -> ".webp"
|
||||
mime.equals("image/gif", true) -> ".gif"
|
||||
else -> ".img"
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package app.klottr.images
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import app.klottr.net.ImageHandle
|
||||
|
||||
/**
|
||||
* Simple, callback-based image picker.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package app.klottr.net
|
||||
package app.klottr.images
|
||||
|
||||
/**
|
||||
* Plattformoberoende inläsning av bildbytes inför uppladdning.
|
||||
@@ -0,0 +1,68 @@
|
||||
@file:OptIn(ExperimentalForeignApi::class)
|
||||
|
||||
package app.klottr.images
|
||||
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
import kotlinx.cinterop.addressOf
|
||||
import kotlinx.cinterop.pin
|
||||
import platform.Foundation.*
|
||||
import platform.posix.memmove
|
||||
|
||||
actual object ImageWriter {
|
||||
actual fun init(platformContext: Any?) {
|
||||
// iOS: nothing needed (parity with ImageReader.init()).
|
||||
}
|
||||
|
||||
private fun appSupportDir(): String? {
|
||||
val urls = NSFileManager.defaultManager.URLsForDirectory(
|
||||
directory = NSApplicationSupportDirectory,
|
||||
inDomains = NSUserDomainMask
|
||||
)
|
||||
val base = urls.firstOrNull() as? NSURL ?: return null
|
||||
// Ensure base exists
|
||||
NSFileManager.defaultManager.createDirectoryAtURL(
|
||||
url = base,
|
||||
withIntermediateDirectories = true,
|
||||
attributes = null,
|
||||
error = null
|
||||
)
|
||||
return base.path
|
||||
}
|
||||
|
||||
actual suspend fun saveImage(
|
||||
bytes: ByteArray,
|
||||
mime: String,
|
||||
subdir: String,
|
||||
baseName: String
|
||||
): ImageSaveResult? {
|
||||
val rootPath = appSupportDir() ?: return null
|
||||
val dirUrl = NSURL.fileURLWithPath("$rootPath/$subdir", isDirectory = true)
|
||||
NSFileManager.defaultManager.createDirectoryAtURL(
|
||||
url = dirUrl, withIntermediateDirectories = true, attributes = null, error = null
|
||||
)
|
||||
|
||||
val filename = baseName + extFromMime(mime)
|
||||
val fileUrl = dirUrl.URLByAppendingPathComponent(filename, isDirectory = false) !!
|
||||
|
||||
val data = bytes.toNSData()
|
||||
val ok = data.writeToURL(fileUrl, atomically = true)
|
||||
return if (ok) {
|
||||
ImageSaveResult(
|
||||
absolutePath = fileUrl.path ?: return null,
|
||||
mime = mime,
|
||||
filename = filename
|
||||
)
|
||||
} else null
|
||||
}
|
||||
|
||||
actual suspend fun save(
|
||||
read: ImageReadResult,
|
||||
subdir: String,
|
||||
baseName: String
|
||||
): ImageSaveResult? = saveImage(read.bytes, read.mime, subdir, baseName)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
private fun ByteArray.toNSData(): NSData {
|
||||
return NSData.create(bytes = this.copyOf().pin().addressOf(0), length = this.size.toULong())
|
||||
}
|
||||
Reference in New Issue
Block a user