fix(file): 解决文件下载和时间戳处理问题
- 实现文件名扩展名推断逻辑,从URL路径自动提取并添加扩展名 - 添加下载错误处理和日志记录功能 - 在下载失败时显示错误通知而非成功通知 - 将服务器时间戳处理统一调整为CST(亚洲/上海)时区 - 更新sdk-core版本号至1.1.6-SNAPSHOT
这个提交包含在:
父节点
3d62a6ed72
当前提交
7306041d79
@ -3,7 +3,7 @@ android.useAndroidX=true
|
|||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
android.nonTransitiveRClass=true
|
android.nonTransitiveRClass=true
|
||||||
PUBLISH_VERSION=1.0.5
|
PUBLISH_VERSION=1.0.5
|
||||||
SDK_CORE_VERSION=1.1.5
|
SDK_CORE_VERSION=1.1.6-SNAPSHOT
|
||||||
SDK_IM_VERSION=1.1.3
|
SDK_IM_VERSION=1.1.3
|
||||||
SDK_PUSH_VERSION=1.1.3
|
SDK_PUSH_VERSION=1.1.3
|
||||||
SDK_UPDATE_VERSION=1.1.8
|
SDK_UPDATE_VERSION=1.1.8
|
||||||
|
|||||||
@ -160,9 +160,16 @@ object FileSDK {
|
|||||||
notificationTitle: String? = null,
|
notificationTitle: String? = null,
|
||||||
onProgress: (Int) -> Unit = {},
|
onProgress: (Int) -> Unit = {},
|
||||||
): File = withContext(Dispatchers.IO) {
|
): File = withContext(Dispatchers.IO) {
|
||||||
val resolvedName = fileName?.takeIf { it.isNotBlank() }
|
val rawName = fileName?.takeIf { it.isNotBlank() }
|
||||||
?: downloadUrl.substringAfterLast('/').substringBefore('?').takeIf { it.isNotBlank() }
|
?: downloadUrl.substringAfterLast('/').substringBefore('?').takeIf { it.isNotBlank() }
|
||||||
?: "download.bin"
|
?: "download.bin"
|
||||||
|
// If the provided name has no extension, infer it from the URL path.
|
||||||
|
val resolvedName = if (!rawName.contains('.')) {
|
||||||
|
val urlExt = downloadUrl.substringAfterLast('/').substringBefore('?')
|
||||||
|
.substringAfterLast('.', "").lowercase()
|
||||||
|
.takeIf { it.isNotBlank() && it.length <= 10 && !it.contains('/') }
|
||||||
|
if (urlExt != null) "$rawName.$urlExt" else rawName
|
||||||
|
} else rawName
|
||||||
|
|
||||||
val baseDir = when (destination) {
|
val baseDir = when (destination) {
|
||||||
FileDownloadDestination.PublicDownloads -> resolveBaseDir(context, destination)
|
FileDownloadDestination.PublicDownloads -> resolveBaseDir(context, destination)
|
||||||
@ -192,6 +199,7 @@ object FileSDK {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val capturedNotifId = notifId
|
val capturedNotifId = notifId
|
||||||
|
var downloadError: Throwable? = null
|
||||||
try {
|
try {
|
||||||
FileTransfer.downloadToFile(downloadUrl, target) { progress ->
|
FileTransfer.downloadToFile(downloadUrl, target) { progress ->
|
||||||
if (capturedNotifId != null) {
|
if (capturedNotifId != null) {
|
||||||
@ -204,21 +212,38 @@ object FileSDK {
|
|||||||
}
|
}
|
||||||
onProgress(progress)
|
onProgress(progress)
|
||||||
}
|
}
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
downloadError = e
|
||||||
|
android.util.Log.e("FileSDK", "download failed: url=$downloadUrl dest=${target.absolutePath}", e)
|
||||||
|
throw e
|
||||||
} finally {
|
} finally {
|
||||||
capturedNotifId?.let { id ->
|
capturedNotifId?.let { id ->
|
||||||
val nm = NotificationManagerCompat.from(context)
|
val nm = NotificationManagerCompat.from(context)
|
||||||
nm.cancel(id)
|
nm.cancel(id)
|
||||||
if (nm.areNotificationsEnabled()) {
|
if (nm.areNotificationsEnabled()) {
|
||||||
nm.notify(
|
val err = downloadError
|
||||||
id,
|
if (err == null) {
|
||||||
NotificationCompat.Builder(context, DOWNLOAD_CHANNEL_ID)
|
nm.notify(
|
||||||
.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
id,
|
||||||
.setContentTitle(notificationTitle)
|
NotificationCompat.Builder(context, DOWNLOAD_CHANNEL_ID)
|
||||||
.setContentText("下载完成:$resolvedName")
|
.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
||||||
.setContentIntent(buildOpenFilePendingIntent(context, target))
|
.setContentTitle(notificationTitle)
|
||||||
.setAutoCancel(true)
|
.setContentText("下载完成:$resolvedName")
|
||||||
.build(),
|
.setContentIntent(buildOpenFilePendingIntent(context, target))
|
||||||
)
|
.setAutoCancel(true)
|
||||||
|
.build(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
nm.notify(
|
||||||
|
id,
|
||||||
|
NotificationCompat.Builder(context, DOWNLOAD_CHANNEL_ID)
|
||||||
|
.setSmallIcon(android.R.drawable.stat_notify_error)
|
||||||
|
.setContentTitle(notificationTitle)
|
||||||
|
.setContentText("下载失败:${err.message ?: resolvedName}")
|
||||||
|
.setAutoCancel(true)
|
||||||
|
.build(),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户