11package com.penumbraos.bridge_settings.server
22
3+ import android.os.ParcelFileDescriptor
4+ import android.system.Os
5+ import android.system.OsConstants
36import com.penumbraos.bridge.callback.IHttpEndpointCallback
47import com.penumbraos.bridge.callback.IHttpResponseCallback
58import com.penumbraos.bridge_settings.SettingsWebServer
9+ import kotlinx.coroutines.CancellableContinuation
610import kotlinx.coroutines.suspendCancellableCoroutine
11+ import java.io.ByteArrayOutputStream
12+ import java.io.FileInputStream
713import kotlin.coroutines.resume
814
915data class EndpointRequest (
@@ -100,6 +106,7 @@ class AidlEndpointCallback(
100106 statusCode : Int ,
101107 headers : MutableMap <Any ?, Any ?>? ,
102108 body : ByteArray? ,
109+ file : ParcelFileDescriptor ? ,
103110 contentType : String?
104111 ) {
105112 val headers = (headers?.mapKeys { it.key.toString() }
@@ -108,13 +115,41 @@ class AidlEndpointCallback(
108115 // Disable CORS
109116 headers[" Access-Control-Allow-Origin" ] = " *"
110117
111- val response = EndpointResponse (
112- statusCode = statusCode,
113- headers = headers,
114- body = body,
115- contentType = contentType ? : " application/json"
116- )
117- continuation.resume(response)
118+ if (file != null ) {
119+ try {
120+ Os .lseek(
121+ file.fileDescriptor,
122+ 0 ,
123+ OsConstants .SEEK_SET
124+ )
125+ val fileBytes =
126+ FileInputStream (file.fileDescriptor)
127+ val byteArrayOutputStream = ByteArrayOutputStream ()
128+ val buffer = ByteArray (4096 )
129+ var bytesRead: Int
130+ while (fileBytes.read(buffer).also { bytesRead = it } != - 1 ) {
131+ byteArrayOutputStream.write(buffer, 0 , bytesRead)
132+ }
133+
134+ val response = EndpointResponse (
135+ statusCode = statusCode,
136+ headers = headers,
137+ body = byteArrayOutputStream.toByteArray(),
138+ contentType = contentType ? : " application/json"
139+ )
140+ continuation.resume(response)
141+ } catch (e: Exception ) {
142+ sendError(continuation, e.message ? : " Unknown error" )
143+ }
144+ } else {
145+ val response = EndpointResponse (
146+ statusCode = statusCode,
147+ headers = headers,
148+ body = body,
149+ contentType = contentType ? : " application/json"
150+ )
151+ continuation.resume(response)
152+ }
118153 }
119154 }
120155
@@ -129,13 +164,17 @@ class AidlEndpointCallback(
129164 responseCallback
130165 )
131166 } catch (e: Exception ) {
132- val errorResponse = EndpointResponse (
133- statusCode = 500 ,
134- body = " {\" error\" : \" Internal server error: ${e.message} \" }" .toByteArray(),
135- contentType = " application/json"
136- )
137- continuation.resume(errorResponse)
167+ sendError(continuation, e.message ? : " Unknown error" )
138168 }
139169 }
140170 }
141171}
172+
173+ fun sendError (continuation : CancellableContinuation <EndpointResponse >, message : String ) {
174+ val errorResponse = EndpointResponse (
175+ statusCode = 500 ,
176+ body = " {\" error\" : \" Internal server error: ${message} \" }" .toByteArray(),
177+ contentType = " application/json"
178+ )
179+ continuation.resume(errorResponse)
180+ }
0 commit comments