Colton Idle
08/30/2022, 7:56 AMTransfer-Encoding: chunked
header. If I replay that request, omitting that header via charles, then the upload succeeds. If I try to remove the header programatically, it still appears in the request (according to charles). Any idea whats up with that? Am I taking the wrong path here?
val resolver = getApplication<Application>().contentResolver
val doc = DocumentFile.fromSingleUri(getApplication(), content)!!
val type = (doc.type ?: DEFAULT_TYPE).toMediaType()
val contentPart = InputStreamRequestBody(type, resolver, content)
val request = Request.Builder().url(url).addHeader("DOES", "THIS WORK").put(contentPart).removeHeader("Transfer-Encoding").build()
yschimke
08/30/2022, 8:59 AMColton Idle
08/30/2022, 3:10 PMyschimke
08/30/2022, 4:50 PMyschimke
08/30/2022, 4:52 PMyschimke
08/30/2022, 4:52 PMval contentLength = body.contentLength()
if (contentLength != -1L) {
requestBuilder.header("Content-Length", contentLength.toString())
requestBuilder.removeHeader("Transfer-Encoding")
} else {
requestBuilder.header("Transfer-Encoding", "chunked")
requestBuilder.removeHeader("Content-Length")
}
ephemient
08/30/2022, 5:02 PMephemient
08/30/2022, 5:09 PMColton Idle
08/30/2022, 10:22 PMprivate val TAG = "MultipartUpload"
private val DEFAULT_TYPE = "application/octet-stream"
private suspend fun uploadMultipart(content: Uri, url: String, fileSize: Long) =
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val resolver = getApplication<Application>().contentResolver
val doc = DocumentFile.fromSingleUri(getApplication(), content)!!
val type = (doc.type ?: DEFAULT_TYPE).toMediaType()
val contentPart = InputStreamRequestBody(type, resolver, content, fileSize)
val request = Request.Builder().url(url).put(contentPart)
.build()
ok.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
// success
}
}
class InputStreamRequestBody(
private val contentType: MediaType,
private val contentResolver: ContentResolver,
private val uri: Uri,
private val fileSize: Long
) : RequestBody() {
override fun contentType() = contentType
override fun contentLength(): Long = fileSize
@Throws(IOException::class)
override fun writeTo(sink: BufferedSink) {
val input = contentResolver.openInputStream(uri)
input?.use { sink.writeAll(it.source()) }
?: throw IOException("Could not open $uri")
}
}
I honestly have no idea what I'm doing with sinks and sources and stuff so I'd appreciate a once over to make sure I'm doing anything horribly wrong.... but IT WORKS!