1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| package com.chat.module_chat.util
import android.content.ContentResolver import okhttp3.ResponseBody import retrofit2.Response
import android.content.Context import android.net.Uri import android.os.Environment import android.util.Log import android.webkit.MimeTypeMap import com.lin.baselib.util.TUIKitConstants import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.InternalCoroutinesApi import kotlinx.coroutines.flow.collect import kotlinx.coroutines.withContext import java.io.BufferedInputStream import java.io.File import java.io.FileOutputStream import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn import okhttp3.MediaType
typealias DOWLOAD_ERROR = (Throwable) -> Unit typealias DOWLOAD_PROCESS = (downloadedSize: Long, length: Long, process: Float) -> Unit typealias DOWLOAD_SUCCESS = (uri: Uri) -> Unit
public suspend fun dowload( context: Context, response: Response<ResponseBody>, block: DowloadBuild.() -> Unit ): DowloadBuild { val build = DowloadBuild(response, context) build.block() return build }
public class DowloadBuild(val response: Response<ResponseBody>, var mContext: Context) { private var error: DOWLOAD_ERROR = {} private var process: DOWLOAD_PROCESS = { downloadedSize, filsLength, process -> } private var success: DOWLOAD_SUCCESS = {} private val context: Context = mContext var setUri: () -> Uri? = { null } var setFileName: () -> String? = { null }
fun process(process: DOWLOAD_PROCESS) { this.process = process }
fun error(error: DOWLOAD_ERROR) { this.error = error }
fun success(success: DOWLOAD_SUCCESS) { this.success = success }
suspend fun startDowload() { withContext(Dispatchers.Main) { flow.flowOn(Dispatchers.IO) .collect { when (it) { is DowloadStatus.DowloadErron -> error(it.t) is DowloadStatus.DowloadProcess -> process( it.currentLength, it.length, it.process ) is DowloadStatus.DowloadSuccess -> success(it.uri) } } } }
val flow = flow<DowloadStatus> { try { val body = response.body() ?: throw RuntimeException("下载出错") val length = body.contentLength() val contentType = body.contentType() val ios = body.byteStream() var uri: Uri? = null var file: File? = null val ops = kotlin.run { setUri()?.let { uri = it context.contentResolver.openOutputStream(it) } ?: kotlin.run { val fileName = setFileName() ?: kotlin.run { "${System.currentTimeMillis()}.${ contentType!!.subtype }" } file = File("${TUIKitConstants.IMAGE_DOWNLOAD_DIR}$fileName") FileOutputStream(file) } } var currentLength: Int = 0 val bufferSize = 1024 * 8 val buffer = ByteArray(bufferSize) val bufferedInputStream = BufferedInputStream(ios, bufferSize) var readLength: Int = 0 while (bufferedInputStream.read(buffer, 0, bufferSize) .also { readLength = it } != -1 ) { ops.write(buffer, 0, readLength) currentLength += readLength emit( DowloadStatus.DowloadProcess( currentLength.toLong(), length, currentLength.toFloat() / length.toFloat() ) ) } bufferedInputStream.close() ops.close() ios.close() if (uri != null) { emit(DowloadStatus.DowloadSuccess(uri!!)) } else if (file != null) { emit(DowloadStatus.DowloadSuccess(Uri.fromFile(file))) }
} catch (e: Exception) { emit(DowloadStatus.DowloadErron(e)) } }
}
sealed class DowloadStatus { class DowloadProcess(val currentLength: Long, val length: Long, val process: Float) : DowloadStatus()
class DowloadErron(val t: Throwable) : DowloadStatus() class DowloadSuccess(val uri: Uri) : DowloadStatus() }
|