Add ImageWrite to write images, move to images namespace
This commit is contained in:
@@ -38,6 +38,8 @@ import app.klottr.create.QueueOutboxOutcome
|
|||||||
import app.klottr.create.createNoteFlow
|
import app.klottr.create.createNoteFlow
|
||||||
import app.klottr.create.queueNoteDocOutbox
|
import app.klottr.create.queueNoteDocOutbox
|
||||||
import app.klottr.events.FoundEvents
|
import app.klottr.events.FoundEvents
|
||||||
|
import app.klottr.images.ImageWriter
|
||||||
|
import app.klottr.images.ImageReader
|
||||||
import app.klottr.location.LastLocationStore
|
import app.klottr.location.LastLocationStore
|
||||||
import app.klottr.location.LocationServiceProvider
|
import app.klottr.location.LocationServiceProvider
|
||||||
import app.klottr.nearby.FoundOpenAction
|
import app.klottr.nearby.FoundOpenAction
|
||||||
@@ -55,8 +57,6 @@ import app.klottr.net.DigitaltMuseum
|
|||||||
import app.klottr.net.DigitaltMuseumItem
|
import app.klottr.net.DigitaltMuseumItem
|
||||||
import app.klottr.net.FornsokShared
|
import app.klottr.net.FornsokShared
|
||||||
import app.klottr.net.Geocache
|
import app.klottr.net.Geocache
|
||||||
import app.klottr.net.ImageHandle
|
|
||||||
import app.klottr.net.ImageReader
|
|
||||||
import app.klottr.net.Wikipedia
|
import app.klottr.net.Wikipedia
|
||||||
import app.klottr.net.createNoteDoc
|
import app.klottr.net.createNoteDoc
|
||||||
import app.klottr.net.fetchImageStreamPost
|
import app.klottr.net.fetchImageStreamPost
|
||||||
@@ -695,7 +695,7 @@ fun KlottrAppShell(
|
|||||||
hasImage = draft.image != null,
|
hasImage = draft.image != null,
|
||||||
uploadImage = {
|
uploadImage = {
|
||||||
// Plattformoberoende läsning via shared ImageReader
|
// Plattformoberoende läsning via shared ImageReader
|
||||||
val read = ImageReader.read(draft.image!!)
|
val read = app.klottr.images.ImageReader.read(draft.image!!)
|
||||||
?: run {
|
?: run {
|
||||||
Api.lastUploadError = "Kunde inte läsa bilddata."
|
Api.lastUploadError = "Kunde inte läsa bilddata."
|
||||||
return@createNoteFlow null
|
return@createNoteFlow null
|
||||||
@@ -793,21 +793,13 @@ fun KlottrAppShell(
|
|||||||
hasImage = (img != null), // Använd snapshotten
|
hasImage = (img != null), // Använd snapshotten
|
||||||
persistImageIfAny = {
|
persistImageIfAny = {
|
||||||
val handle = img ?: return@queueNoteDocOutbox null
|
val handle = img ?: return@queueNoteDocOutbox null
|
||||||
val read = ImageReader.read(handle) ?: return@queueNoteDocOutbox null
|
val read = app.klottr.images.ImageReader.read(handle) ?: return@queueNoteDocOutbox null
|
||||||
val mime = read.mime
|
val saved = ImageWriter.save(
|
||||||
val ext = when {
|
read = read,
|
||||||
mime.equals("image/jpeg", true) || mime.equals("image/jpg", true) -> ".jpg"
|
subdir = "outbox",
|
||||||
mime.equals("image/png", true) -> ".png"
|
baseName = "img_${created}"
|
||||||
mime.equals("image/webp", true) -> ".webp"
|
) ?: return@queueNoteDocOutbox null
|
||||||
mime.equals("image/gif", true) -> ".gif"
|
saved.absolutePath to saved.mime
|
||||||
else -> ".img"
|
|
||||||
}
|
|
||||||
val outDir = File(context.filesDir, "outbox").apply { mkdirs() }
|
|
||||||
val outFile = File(outDir, "img_${created}$ext")
|
|
||||||
try {
|
|
||||||
outFile.outputStream().use { outs -> outs.write(read.bytes) }
|
|
||||||
outFile.absolutePath to mime
|
|
||||||
} catch (_: Exception) { null }
|
|
||||||
},
|
},
|
||||||
addOutboxDoc = { la, lo, t, b, ts, rad, imagePath, imageMime ->
|
addOutboxDoc = { la, lo, t, b, ts, rad, imagePath, imageMime ->
|
||||||
OutboxStore.addDoc(
|
OutboxStore.addDoc(
|
||||||
@@ -866,20 +858,12 @@ fun KlottrAppShell(
|
|||||||
persistImageIfAny = {
|
persistImageIfAny = {
|
||||||
val handle = img ?: return@queueNoteDocOutbox null
|
val handle = img ?: return@queueNoteDocOutbox null
|
||||||
val read = ImageReader.read(handle) ?: return@queueNoteDocOutbox null
|
val read = ImageReader.read(handle) ?: return@queueNoteDocOutbox null
|
||||||
val mime = read.mime
|
val saved = ImageWriter.save(
|
||||||
val ext = when {
|
read = read,
|
||||||
mime.equals("image/jpeg", true) || mime.equals("image/jpg", true) -> ".jpg"
|
subdir = "outbox",
|
||||||
mime.equals("image/png", true) -> ".png"
|
baseName = "img_${created}"
|
||||||
mime.equals("image/webp", true) -> ".webp"
|
) ?: return@queueNoteDocOutbox null
|
||||||
mime.equals("image/gif", true) -> ".gif"
|
saved.absolutePath to saved.mime
|
||||||
else -> ".img"
|
|
||||||
}
|
|
||||||
val outDir = File(context.filesDir, "outbox").apply { mkdirs() }
|
|
||||||
val outFile = File(outDir, "img_${created}$ext")
|
|
||||||
try {
|
|
||||||
outFile.outputStream().use { outs -> outs.write(read.bytes) }
|
|
||||||
outFile.absolutePath to mime
|
|
||||||
} catch (_: Exception) { null }
|
|
||||||
},
|
},
|
||||||
addOutboxDoc = { la, lo, t, b, ts, rad, imagePath, imageMime ->
|
addOutboxDoc = { la, lo, t, b, ts, rad, imagePath, imageMime ->
|
||||||
OutboxStore.addDoc(
|
OutboxStore.addDoc(
|
||||||
|
|||||||
@@ -19,10 +19,11 @@ import androidx.compose.runtime.setValue
|
|||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import app.klottr.db.DatabaseFactory
|
import app.klottr.db.DatabaseFactory
|
||||||
import app.klottr.db.KlottrDatabase
|
import app.klottr.db.KlottrDatabase
|
||||||
import app.klottr.net.ImageReader
|
|
||||||
import app.klottr.shared.SharedPing
|
import app.klottr.shared.SharedPing
|
||||||
import app.klottr.theme.KlottrTheme
|
import app.klottr.theme.KlottrTheme
|
||||||
import app.klottr.auth.AuthPlatform
|
import app.klottr.auth.AuthPlatform
|
||||||
|
import app.klottr.images.ImageReader
|
||||||
|
import app.klottr.images.ImageWriter
|
||||||
import app.klottr.util.AuthShared
|
import app.klottr.util.AuthShared
|
||||||
import app.klottr.util.PlatformAuthStorage
|
import app.klottr.util.PlatformAuthStorage
|
||||||
import app.klottr.util.AuthStorageDelegate
|
import app.klottr.util.AuthStorageDelegate
|
||||||
@@ -120,7 +121,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
|
|
||||||
// Säkerställ att delad ImageReader har appContext på Android
|
// Säkerställ att delad ImageReader har appContext på Android
|
||||||
ImageReader.init(applicationContext)
|
ImageReader.init(applicationContext)
|
||||||
|
ImageWriter.init(applicationContext)
|
||||||
|
|
||||||
KlottrTheme(darkTheme = night) {
|
KlottrTheme(darkTheme = night) {
|
||||||
KlottrAppShell(
|
KlottrAppShell(
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import app.klottr.events.FoundEvents
|
|||||||
import app.klottr.net.Geocache
|
import app.klottr.net.Geocache
|
||||||
import app.klottr.net.createNote
|
import app.klottr.net.createNote
|
||||||
import app.klottr.net.createNoteDoc
|
import app.klottr.net.createNoteDoc
|
||||||
import app.klottr.net.ImageHandle
|
import app.klottr.images.ImageHandle
|
||||||
import app.klottr.net.ImageReader
|
import app.klottr.images.ImageReader
|
||||||
import app.klottr.net.uploadImage
|
import app.klottr.net.uploadImage
|
||||||
import app.klottr.util.FoundStore
|
import app.klottr.util.FoundStore
|
||||||
import app.klottr.util.OutboxStore
|
import app.klottr.util.OutboxStore
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package app.klottr.net
|
package app.klottr.images
|
||||||
|
|
||||||
import android.content.ContentResolver
|
import android.content.ContentResolver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import app.klottr.net.inferUploadFilename
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
actual object ImageReader {
|
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.Composable
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import app.klottr.net.ImageHandle
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
actual fun rememberVisualMediaPicker(): VisualMediaPicker {
|
actual fun rememberVisualMediaPicker(): VisualMediaPicker {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ import app.klottr.ui.WriteDocContent
|
|||||||
import app.klottr.util.SettingsStore
|
import app.klottr.util.SettingsStore
|
||||||
import androidx.compose.ui.graphics.ImageBitmap
|
import androidx.compose.ui.graphics.ImageBitmap
|
||||||
import app.klottr.images.loadImageBitmap
|
import app.klottr.images.loadImageBitmap
|
||||||
import app.klottr.net.ImageHandle
|
import app.klottr.images.ImageHandle
|
||||||
import app.klottr.images.rememberVisualMediaPicker
|
import app.klottr.images.rememberVisualMediaPicker
|
||||||
import kotlin.time.Clock
|
import kotlin.time.Clock
|
||||||
import kotlin.time.ExperimentalTime
|
import kotlin.time.ExperimentalTime
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package app.klottr.images
|
|||||||
|
|
||||||
import androidx.compose.ui.graphics.ImageBitmap
|
import androidx.compose.ui.graphics.ImageBitmap
|
||||||
import org.jetbrains.compose.resources.decodeToImageBitmap
|
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),
|
* 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
|
package app.klottr.images
|
||||||
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import app.klottr.net.ImageHandle
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple, callback-based image picker.
|
* 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.
|
* 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