[下载使用临时文件]
This commit is contained in:
yangyakun
2024-08-20 12:06:34 +08:00
parent a57dcc151d
commit a182ecaa07
3 changed files with 66 additions and 24 deletions

View File

@@ -3,24 +3,17 @@ package com.mogo.och.common.module.manager.download
import android.annotation.SuppressLint
import android.content.Context
import android.os.Environment
import android.text.TextUtils
import android.util.Log
import com.mogo.commons.AbsMogoApplication
import com.mogo.eagle.core.function.call.chat.CallerChatManager
import com.mogo.eagle.core.utilcode.download.DownloadUtils
import com.mogo.eagle.core.utilcode.download.callback.IDownloadListener
import com.mogo.eagle.core.utilcode.kotlin.lifeCycleScope
import com.mogo.eagle.core.utilcode.util.StringUtils
import com.mogo.eagle.core.utilcode.util.ThreadUtils
import com.mogo.eagle.core.utilcode.util.EncryptUtils
import com.mogo.och.common.module.manager.logchainanalytic.OchChainLogManager
import com.mogo.och.common.module.manager.loop.BizLoopManager
import com.mogo.och.common.module.manager.loop.LoopInfo
import com.mogo.och.common.module.utils.FileUtils
import com.mogo.och.common.module.wigets.media.MediaFileCacheManager.getCacheFileName
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import java.io.File
@SuppressLint("StaticFieldLeak")
@@ -50,11 +43,47 @@ object DownloadManager {
return FileUtils.getCacheDirectory(context, "") + relativePath
}
/**
* 本地是否已经存在下载完成的文件
*/
private fun isLocalCacheFileExists(mediaUrl: String): Boolean {
val localVideoCacheFilePath =
getCacheFileFullPathByUrl(mediaUrl)
Log.e(TAG, "文件是否存在mediaUrl=$mediaUrl------本地文件:${localVideoCacheFilePath}")
return com.mogo.eagle.core.utilcode.util.FileUtils.isFileExists(localVideoCacheFilePath)
}
/**
* 获取文件缓存的缓存path, 文件名以base64编码避免 中文命名,重复文件名的影响
*/
private fun getCacheFileFullPathByUrl(mediaUrl: String): String {
return getFileCacheDir() +File.separator +getCacheFileName(mediaUrl)
}
/**
* 本地缓存文件的文件名md5编码避免文件名重复或者特殊字符编码问题
*/
private fun getCacheFileName(mediaUrl: String): String {
val fileSuffix = FileUtils.getExtension(mediaUrl)
if (TextUtils.isEmpty(fileSuffix)) {
Log.e(TAG, "getCacheFileName 根据url获取文件后缀不合法mediaUrl=$mediaUrl")
return ""
}
return EncryptUtils.encryptMD5ToString(mediaUrl) + FileUtils.EXTENSION_SEPARATOR + fileSuffix
}
fun clearLocalFile(localVideoPath:String) {
if (com.mogo.eagle.core.utilcode.util.FileUtils.isFileExists(localVideoPath)) {
com.mogo.eagle.core.utilcode.util.FileUtils.delete(localVideoPath)
}
OchChainLogManager.writechainLogDownload("删除本地文件","localVideoPath:${localVideoPath}")
}
fun downloadVideoFile(mediaUrl: String, listener: IDownloadListener?) {
val downloadUrl = mediaUrl
val downloadDir = getFileCacheDir()
val downloadFileName = getCacheFileName(mediaUrl)
val downloadFileName = getCacheFileName(mediaUrl)+"_temp"
waitDownLoadList.add(OchDownLoad(downloadUrl,downloadDir,downloadFileName,listener))
@@ -69,7 +98,19 @@ object DownloadManager {
return
}
val first = waitDownLoadList.first()
OchChainLogManager.writechainLogDownload("准备下周","信息:${first}")
// 临时文件存在 需要删除重新下载
if (com.mogo.eagle.core.utilcode.util.FileUtils.isFileExists(first.downloadDir+File.separator+first.downloadFileName)) {
OchChainLogManager.writechainLogDownload("删除临时文件","文件名称:${first.downloadDir+File.separator+first.downloadFileName}")
clearLocalFile(first.downloadDir+File.separator+first.downloadFileName)
}
// 改名后文件存在 不需要下载
if(isLocalCacheFileExists(first.downloadUrl)){
waitDownLoadList.removeFirst()
OchChainLogManager.writechainLogDownload("取消下载文件已存在","信息:${first}")
go2Download()
return
}
OchChainLogManager.writechainLogDownload("准备下载","信息:${first}")
DownloadUtils.downLoad(
context,
first.downloadUrl,
@@ -87,15 +128,21 @@ object DownloadManager {
}
override fun onFinished(url: String, path: String) {
first.listener?.onFinished(url,path)
OchChainLogManager.writechainLogDownload("下载结束","url:${url}---path:${path}")
OchChainLogManager.writechainLogDownload("下载成功","url:${url}---path:${path}")
waitDownLoadList.removeFirst()
if(path.endsWith("_temp")){
val tempFile = File(path)
val newName = tempFile.name.replace("_temp","")
com.mogo.eagle.core.utilcode.util.FileUtils.rename(tempFile,newName)
first.listener?.onFinished(url,path.replace("_temp",""))
}
go2Download()
}
override fun onError(url: String, error: String?) {
first.listener?.onError(url,error)
OchChainLogManager.writechainLogDownload("下载错误","url:${url}---error:${error}")
clearLocalFile(first.downloadDir+File.separator+first.downloadFileName)
if(first.downLoadCount>=5){
OchChainLogManager.writechainLogDownload("下载错误5次可终点关注","url:${url}---error:${error}")
waitDownLoadList.removeFirst()

View File

@@ -237,15 +237,8 @@ class AdvancePagerAdapter(context: Context, viewPager: ViewPager) : PagerAdapter
mDataList.forEachIndexed { index, mediaItem ->
// 广告中有视频播放器
if(mediaItem.fileType==MediaItem.MEDIA_TYPE_VIDEO){
if (currentPosition == mItemViewList.size - 1) {
//已经到最后一个, 从头开始
mViewPager.post {
mViewPager.setCurrentItem(0, true)
}
} else {
mViewPager.post {
mViewPager.setCurrentItem(mViewPager.currentItem + 1, true)
}
mViewPager.post {
mViewPager.setCurrentItem(index, true)
}
return
}

View File

@@ -72,7 +72,9 @@ class MediaPlayerFragment :
fun setHightPriorityMediaItem(list: MutableList<MediaItem>){
context?.let {context->
val cacheList = list.filter {
DownloadManager.downloadVideoFile(it.fileUrl,null)
if(!MediaFileCacheManager.isLocalCacheFileExists(context,it.fileUrl)){
DownloadManager.downloadVideoFile(it.fileUrl,null)
}
MediaFileCacheManager.isLocalCacheFileExists(context,it.fileUrl)
}
ThreadUtils.runOnUiThread {