浏览代码

fix: Content-MD5下载进度回调

drake 2 年之前
父节点
当前提交
9028ac465a

+ 57 - 28
net/src/main/java/com/drake/net/component/Progress.kt

@@ -1,3 +1,20 @@
+/*
+ * Copyright (C) 2018 Drake, https://github.com/liangjingkanji
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
 @file:Suppress("unused", "MemberVisibilityCanBePrivate", "NAME_SHADOWING", "RedundantSetter")
 
 package com.drake.net.component
@@ -7,22 +24,47 @@ import android.text.format.DateUtils
 import android.text.format.Formatter
 import com.drake.net.NetConfig
 
+
 /**
- * @property currentByteCount 当前已经完成的字节数
- * @property totalByteCount 当前已经完成的字节数
- * @property intervalByteCount 进度间隔时间内完成的字节数
- * @property intervalTime 距离上次进度变化间隔时间
- * @property startElapsedRealtime 开始下载的时间
- * @property finish 是否完成
+ * 下载/上传进度信息
  */
-data class Progress(
-    var currentByteCount: Long = 0,
-    var totalByteCount: Long = 0,
-    var intervalByteCount: Long = 0,
-    var intervalTime: Long = 0,
-    val startElapsedRealtime: Long = SystemClock.elapsedRealtime(),
-    var finish: Boolean = false,
-) {
+class Progress {
+
+    /** 当前已经完成的字节数 */
+    var currentByteCount: Long = 0
+        internal set
+
+    /** 当前已经完成的字节数 */
+    var totalByteCount: Long = 0
+        internal set
+
+    /** 进度间隔时间内完成的字节数 */
+    var intervalByteCount: Long = 0
+        internal set
+
+    /** 距离上次进度变化间隔时间 */
+    var intervalTime: Long = 0
+        internal set
+
+    /** 是否完成 */
+    var finish: Boolean = false
+        internal set
+
+    /** 开始下载的时间 */
+    val startElapsedRealtime: Long = SystemClock.elapsedRealtime()
+
+    /**
+     * 每秒下载速度, 字节单位
+     */
+    var speedBytes = 0L
+        get() {
+            return if (intervalTime <= 0L || intervalByteCount <= 0) {
+                field
+            } else {
+                field = intervalByteCount * 1000 / intervalTime
+                field
+            }
+        }
 
     /**
      * 文件全部大小
@@ -50,6 +92,7 @@ data class Progress(
         return Formatter.formatFileSize(NetConfig.app, remain)
     }
 
+
     /**
      * 每秒下载速度
      * 根据字节数自动显示内存单位, 例如 19MB 或者 27KB
@@ -58,20 +101,6 @@ data class Progress(
         return Formatter.formatFileSize(NetConfig.app, speedBytes)
     }
 
-
-    /**
-     * 每秒下载速度, 字节单位
-     */
-    var speedBytes = 0L
-        get() {
-            return if (intervalTime <= 0L || intervalByteCount <= 0) {
-                field
-            } else {
-                field = intervalByteCount * 1000 / intervalTime
-                field
-            }
-        }
-
     /**
      * 请求或者响应的进度, 值范围在0-100
      * 如果服务器返回的响应体没有包含Content-Length(比如启用gzip压缩后Content-Length会被删除), 则无法计算进度, 始终返回0

+ 18 - 1
net/src/main/java/com/drake/net/response/ResponseExtension.kt

@@ -17,12 +17,14 @@
 package com.drake.net.response
 
 import com.drake.net.body.peekString
+import com.drake.net.component.Progress
 import com.drake.net.convert.NetConverter
 import com.drake.net.exception.ConvertException
 import com.drake.net.exception.DownloadFileException
 import com.drake.net.exception.NetException
 import com.drake.net.reflect.typeTokenOf
 import com.drake.net.request.*
+import com.drake.net.tag.NetTag
 import com.drake.net.utils.md5
 import okhttp3.Response
 import okio.buffer
@@ -87,7 +89,22 @@ fun Response.file(): File? {
             // MD5校验匹配文件
             if (request.downloadMd5Verify()) {
                 val md5Header = header("Content-MD5")
-                if (md5Header != null && file.md5(true) == md5Header) return file
+                if (md5Header != null && file.md5(true) == md5Header) {
+                    val downloadListeners = request.tagOf<NetTag.DownloadListeners>()
+                    if (!downloadListeners.isNullOrEmpty()) {
+                        val fileSize = file.length()
+                        val progress = Progress().apply {
+                            currentByteCount = fileSize
+                            totalByteCount = fileSize
+                            intervalByteCount = fileSize
+                            finish = true
+                        }
+                        downloadListeners.forEach {
+                            it.onProgress(progress)
+                        }
+                    }
+                    return file
+                }
             }
             // 命名冲突添加序列数字的后缀
             if (request.downloadConflictRename() && file.name == fileName) {

+ 2 - 3
net/src/main/java/com/drake/net/utils/Uri.kt

@@ -9,7 +9,6 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
 import okhttp3.RequestBody
 import okio.BufferedSink
 import okio.source
-import okio.use
 
 fun Uri.fileName(): String? {
     return DocumentFile.fromSingleUri(NetConfig.app, this)?.name
@@ -28,9 +27,9 @@ fun Uri.toRequestBody(): RequestBody {
         override fun contentType(): MediaType? {
             return mediaType()
         }
-        
+
         override fun contentLength() = length
-        
+
         override fun writeTo(sink: BufferedSink) {
             NetConfig.app.contentResolver.openInputStream(this@toRequestBody)?.source()?.use {
                 sink.writeAll(it)