diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFileDownload.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFileDownload.kt new file mode 100644 index 00000000..57cfd80c --- /dev/null +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFileDownload.kt @@ -0,0 +1,90 @@ +package com.faceclaw.app + +import android.os.Handler +import android.os.Looper + +import java.io.File +import java.io.FileOutputStream +import java.util.concurrent.TimeUnit + +import okhttp3.Call +import okhttp3.OkHttpClient +import okhttp3.Request + +/** + * One GET streamed straight to a file on a worker thread. For binary bodies + * too large to cross into JS through NativeScript's fetch(), which copies the + * Java byte[] into a Uint8Array one bridge call per byte (a 43MB EvenHub + * package ran the process out of native memory). JS reads the finished file + * back with one bulk copy (readBinaryFile). + */ +class FaceclawFileDownload( + private val url: String, + private val destPath: String, + private val timeoutMs: Long, + listener: FaceclawFileDownloadListener? +) { + private val callbackHandler = Handler(Looper.myLooper() ?: Looper.getMainLooper()) + private val listener: FaceclawFileDownloadListener = + listener ?: throw IllegalArgumentException("listener is required") + + @Volatile + private var call: Call? = null + + @Volatile + private var cancelled = false + + fun start() { + Thread({ run() }, "FaceclawFileDl").start() + } + + /** Aborts the request; no callback follows. */ + fun cancel() { + cancelled = true + call?.cancel() + } + + private fun run() { + val dest = File(destPath) + try { + dest.parentFile?.mkdirs() + val request = Request.Builder().url(url).build() + val call = getClient().newCall(request) + call.timeout().timeout(timeoutMs, TimeUnit.MILLISECONDS) + this.call = call + call.execute().use { response -> + val body = response.body + val contentLength = body?.contentLength() ?: -1L + if (response.isSuccessful && body != null) { + body.byteStream().use { input -> + FileOutputStream(dest).use { output -> input.copyTo(output, 1 shl 16) } + } + } + if (!cancelled) callbackHandler.post { listener.onDone(response.code, contentLength) } + } + } catch (e: Exception) { + dest.delete() + if (!cancelled) callbackHandler.post { listener.onFailure(e.message ?: e.toString()) } + } + } + + companion object { + @Volatile + private var sharedClient: OkHttpClient? = null + + private fun getClient(): OkHttpClient { + if (sharedClient == null) { + synchronized(FaceclawFileDownload::class.java) { + if (sharedClient == null) { + sharedClient = OkHttpClient.Builder() + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(60, TimeUnit.SECONDS) + .addInterceptor(FaceclawHttp.userAgentInterceptor()) + .build() + } + } + } + return sharedClient!! + } + } +} diff --git a/app/native/binary-http.ts b/app/native/binary-http.ts index b70b832b..12a79d32 100644 --- a/app/native/binary-http.ts +++ b/app/native/binary-http.ts @@ -1,15 +1,37 @@ -import { fetchWithUserAgent } from '../util/http'; +declare const com: any; +declare const java: any; + export type BinaryResponse = { status: number; bytes: Uint8Array; contentLength: number }; -export async function downloadBinary(url: string, timeoutMs: number): Promise { - let timer: ReturnType; - const request = async () => { - const response = await fetchWithUserAgent(url); - return { status: response.status, bytes: new Uint8Array(await response.arrayBuffer()), - contentLength: Number(response.headers.get('Content-Length') ?? 0) }; - }; - try { - return await Promise.race([request(), new Promise((_, reject) => { - timer = setTimeout(() => reject(new Error('Download timed out.')), timeoutMs); - })]); - } finally { clearTimeout(timer!); } + +let nextDownloadSerial = 0; + +/** + * GET a binary body. Not fetch(): NativeScript's polyfill copies the Java + * byte[] into JS one bridge call per byte (http-request's toArrayBuffer), which + * for a 43MB EvenHub package pinned the main thread for ~20s and then ran the + * process out of native memory. FaceclawFileDownload streams the body to a + * cache file instead, and readBinaryFile copies it back in bulk. + */ +export function downloadBinary(url: string, timeoutMs: number): Promise { + // Required lazily so Node tests can load this module's importers. + const { appCacheDirPath, readBinaryFile } = require("./file-access") as typeof import("./file-access"); + const path = `${appCacheDirPath()}/binary-download-${Date.now()}-${nextDownloadSerial++}`; + const remove = () => { try { new java.io.File(path).delete(); } catch { /* best effort */ } }; + return new Promise((resolve, reject) => { + const listener = new com.faceclaw.app.FaceclawFileDownloadListener({ + onDone: (status: number, contentLength: number) => { + try { + const ok = status >= 200 && status < 300; + const bytes = ok ? readBinaryFile(path) : new Uint8Array(0); + if (!bytes) reject(new Error("Could not read the downloaded file.")); + else resolve({ status, bytes, contentLength: Math.max(0, Number(contentLength)) }); + } finally { remove(); } + }, + onFailure: (message: string) => { + remove(); + reject(new Error(/timeout|timed out/i.test(String(message)) ? "Download timed out." : `Download failed: ${message}`)); + }, + }); + new com.faceclaw.app.FaceclawFileDownload(url, path, timeoutMs, listener).start(); + }); } diff --git a/app/native/file-access.ts b/app/native/file-access.ts index 5cf818e5..c0522646 100644 --- a/app/native/file-access.ts +++ b/app/native/file-access.ts @@ -124,19 +124,19 @@ export function readBinaryFile(path: string): Uint8Array | null { try { const file = new java.io.File(path); if (!file.isFile()) return null; - const bytes = java.nio.file.Files.readAllBytes(file.toPath()); + // Read into a direct buffer, which the V8 runtime can view in place, then + // copy once so the result outlives it. Nothing lands on the Java heap, and + // no path copies per byte (EvenHub packages can be tens of MB). + const length = Number(file.length()); + const buffer = java.nio.ByteBuffer.allocateDirect(Math.max(length, 1)); + const channel = new java.io.FileInputStream(file).getChannel(); try { - // The V8 runtime can wrap a ByteBuffer's memory directly; copy out of it - // so the result outlives the Java array. - const arrayBuffer = (ArrayBuffer as any).from(java.nio.ByteBuffer.wrap(bytes)); - return new Uint8Array(arrayBuffer).slice(); - } catch { - const out = new Uint8Array(bytes.length); - for (let i = 0; i < bytes.length; i++) { - out[i] = bytes[i] & 0xff; - } - return out; + while (buffer.position() < length && channel.read(buffer) >= 0) { /* fill */ } + } finally { + channel.close(); } + const read = Number(buffer.position()); + return new Uint8Array((ArrayBuffer as any).from(buffer), 0, read).slice(); } catch (error) { console.warn(`readBinaryFile failed for ${path}: ${error}`); return null; @@ -189,6 +189,12 @@ export function appFilesDirPath(): string { return String(context.getFilesDir().getAbsolutePath()); } +/** The app-private cache directory, for scratch files the OS may reclaim. */ +export function appCacheDirPath(): string { + const context = Utils.android.getApplicationContext(); + return String(context.getCacheDir().getAbsolutePath()); +} + /** Read a UTF-8 text file (size-capped); null when unreadable or too large. */ export function readTextFile(path: string): string | null { try { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFileDownloadListener.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFileDownloadListener.kt new file mode 100644 index 00000000..02c3f5eb --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFileDownloadListener.kt @@ -0,0 +1,10 @@ +package com.faceclaw.app + +/** Callbacks for FaceclawFileDownload, delivered on the constructing thread's Looper. */ +interface FaceclawFileDownloadListener { + /** The response finished; for a 2xx status the body is at the destination path, otherwise nothing is written. */ + fun onDone(status: Int, contentLength: Long): Unit + + /** Network-level failure (connect, TLS, timeout, mid-body drop); the destination is removed. */ + fun onFailure(message: String?): Unit +}