feat(shared): add KMP get_image.php (form stream) and delegate Android fetchImageStreamPost

This commit is contained in:
2025-09-06 19:48:36 +02:00
parent 1bfdc3f037
commit ec4d3d2746
2 changed files with 44 additions and 12 deletions

View File

@@ -13,6 +13,7 @@ import okhttp3.Interceptor
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Response import okhttp3.Response
import okhttp3.ResponseBody import okhttp3.ResponseBody
import okhttp3.ResponseBody.Companion.toResponseBody
import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Response as RResponse import retrofit2.Response as RResponse
import retrofit2.Retrofit import retrofit2.Retrofit
@@ -514,33 +515,31 @@ suspend fun setUsernameRemote(input: String): UsernameSetResp? {
return sh return sh
} }
/** Hämta bildström via delad KMP-klient (form-POST) och bygg ResponseBody */
/** Hjälpfunktion för att hämta en bildström via POST (form-POST till PHP). */
suspend fun fetchImageStreamPost( suspend fun fetchImageStreamPost(
imageId: Long, imageId: Long,
noteId: Long, noteId: Long,
lat: Double, lat: Double,
lon: Double lon: Double
): ResponseBody? { ): ResponseBody? {
return try { val bytes = try {
val resp = Api.svc.getImagePost( KlottrApiShared.getImageStreamPost(
baseUrl = ApiConfig.SERVER_API_BASE_URL,
apiKey = ApiConfig.SERVER_API_KEY,
bearerToken = app.klottr.util.Auth.idToken,
id = imageId, id = imageId,
noteId = noteId, noteId = noteId,
lat = lat, lat = lat,
lon = lon lon = lon
) )
if (resp.isSuccessful) {
resp.body()
} else {
app.klottr.util.AppLog.d { "get_image FAIL code=${resp.code()} err=${resp.errorBody()?.string()}" }
null
}
} catch (t: Throwable) { } catch (t: Throwable) {
app.klottr.util.AppLog.d { "get_image exception: ${t.message}" } app.klottr.util.AppLog.d { "get_image exception: ${t.message}" }
null null
} } ?: return null
}
// Okänd MIME → använd generisk
return bytes.toResponseBody("application/octet-stream".toMediaTypeOrNull())
}
private data class ApiErr( private data class ApiErr(
val error: String? = null, val error: String? = null,

View File

@@ -556,6 +556,39 @@ object KlottrApiShared {
ok = ok, imageId = imageId, width = width, height = height, errorMessage = if (ok) null else errMsg ok = ok, imageId = imageId, width = width, height = height, errorMessage = if (ok) null else errMsg
) )
} }
/**
* KMP-variant av get_image.php (form-POST med id/note/lat/lon).
* Returnerar bildbytes eller null vid fel.
*/
suspend fun getImageStreamPost(
baseUrl: String,
apiKey: String,
bearerToken: String?,
id: Long,
noteId: Long,
lat: Double,
lon: Double
): ByteArray? {
val url = ensureSlash(baseUrl) + "get_image.php"
val resp = try {
client.submitForm(
url = url,
formParameters = Parameters.build {
append("id", id.toString())
append("note", noteId.toString())
append("lat", lat.toString())
append("lon", lon.toString())
}
) {
header("X-API-Key", apiKey)
if (bearerToken != null) header(HttpHeaders.Authorization, "Bearer $bearerToken")
}
} catch (_: Throwable) {
return null
}
if (resp.status.value != 200) return null
return try { resp.body<ByteArray>() } catch (_: Throwable) { null }
}
} }