sarvagya agarwal
10/13/2022, 9:45 AMval chunk = ByteArray(BYTE_ARRAY_SIZE)
val inputStream = FileInputStream(file)
val fileChunks = flow {
val metadata = MetaData.newBuilder()
.setPath(file.path)
.setConvertToPdf(BoolValue.of(convertToPdf))
.build()
emit(FileUploadRequest.newBuilder().setMetadata(metadata).build())
// It works if i dont break the file into chunks
// val fileChunk = FileChunk.newBuilder().setData(ByteString.copyFrom(byteArray)).build()
// emit(FileUploadRequest.newBuilder().setContent(fileChunk).build())
while(true) {
val size = inputStream.read(chunk)
if(size <= 0) break
val fileChunk = FileChunk.newBuilder()
.setData(ByteString.copyFrom(chunk, 0, size)).build()
emit(FileUploadRequest.newBuilder().setContent(fileChunk).build())
}
}
On the server side , i reconstruct the file and process it , the file is constructed properly if i send it all at once instead of breaking it down into smaller chunks. Not sure how to debug this either , does anyone have any idea what i am doing wrong ?
requests.collect {
if (it.hasContent()) {
file.writeBytes(it.content.toByteArray())
} else {
metadata = it.metadata
file = File(metadata.path)
}
}