Video download and Resume Video download function
/**
*
* @param url 파일 Url
* @param destPath 저장 위치
* @param info 파일 아이템
*/
private fun clonSaveToDiskRx(
url: String,
destPath: String,
info: FileItem,
context:Context
): Observable<DownloadItem> {
sinkClosing = false
return Observable.create { emitter ->
val request = Request.Builder().url(url).build()
val response = FileNetworkGenerator.build().newCall(request).execute()
val destFile = File(destPath, info.fileName)
var contentLength = 0L
var totalBytesRead: Long = 0
var bytesRead: Long
try {
val body = response.body()
contentLength = body!!.contentLength()
source = body.source()
sink = Okio.buffer(Okio.appendingSink(destFile))
val sinkBuffer = sink?.buffer()
val bufferSize = 15 * 1024 * 1024 // 15MB로 수정
do {
bytesRead = source!!.read(sinkBuffer, bufferSize.toLong())
totalBytesRead += bytesRead
emitter.onNext(
DownloadItem(
state = BBXFileManager.Status.DOWNLOADING,
downloadSize = totalBytesRead,
totalSize = contentLength
)
)
sink?.emit()
} while (bytesRead != -1L && !sinkClosing)
} catch (e: IOException) {
debug("save file e >>> $e")
e.printStackTrace()
emitter.error(e)
} finally {
Util.closeQuietly(sink)
Util.closeQuietly(source)
MediaScannerConnection.scanFile(
context,
arrayOf(destFile.absolutePath),
arrayOf("video/*"),
null,
)
emitter.onNext(
DownloadItem(
state = BBXFileManager.Status.SUCCEED,
downloadSize = totalBytesRead,
totalSize = contentLength
)
)
emitter.onComplete()
}
}
}