Hi , has anyone ever worked with streaming files a...
# grpc
s
Hi , has anyone ever worked with streaming files as a byte array in grpc (Sending file as list of chunks from client and reconstructing it on the server) ? This is what i have on the client side
Copy code
val 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 ?
Copy code
requests.collect {
	if (it.hasContent()) {
		file.writeBytes(it.content.toByteArray())
	} else {
		metadata = it.metadata
		file = File(metadata.path)
	}
}