Skip to content

Commit 49d4dff

Browse files
author
Fastace
committed
feat: 为S3云端恢复添加文件大小显示功能
修复了S3云端恢复场景中APK/USER/OBB等备份文件大小显示为0的问题。 主要改动: - 在 AppsRepo.kt 中添加 calculateCloudAppArchiveSize() 函数,通过 S3 HeadObject 接口获取云端应用备份文件大小 - 在 FilesRepo.kt 中添加 calculateCloudFileArchiveSize() 函数,支持云端文件备份大小计算 - 修改 DetailsViewModel.kt 的 onResume() 函数,根据 cloud 字段判断调用云端或本地大小计算函数 - 利用 S3ClientImpl.size() 方法通过 HeadObject API 获取对象元数据中的 contentLength - 云端大小计算支持所有6种数据类型(APK/USER/USER_DE/DATA/OBB/MEDIA) - 使用 runCatching 进行错误处理,网络异常时返回0 - 所有云端操作在后台线程执行,不阻塞UI - 本地和云端逻辑保持一致,便于维护
1 parent 532e613 commit 49d4dff

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

source/core/data/src/main/kotlin/com/xayah/core/data/repository/AppsRepo.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,31 @@ class AppsRepo @Inject constructor(
621621
appsDao.upsert(app)
622622
}
623623

624+
// 添加云端单个数据类型大小计算
625+
private suspend fun calculateCloudAppArchiveSize(cloudName: String, p: PackageEntity, dataType: DataType): Long = runCatching {
626+
var size = 0L
627+
cloudRepo.withClient(cloudName) { client, entity ->
628+
val remote = entity.remote
629+
val remoteAppsDir = pathUtil.getCloudRemoteAppsDir(remote)
630+
val archivePath = getArchiveSrc("${remoteAppsDir}/${p.archivesRelativeDir}", dataType, p.indexInfo.compressionType)
631+
if (client.exists(archivePath)) {
632+
size = client.size(archivePath)
633+
}
634+
}
635+
size
636+
}.getOrDefault(0L)
637+
638+
// 添加云端应用完整大小计算
639+
suspend fun calculateCloudAppArchiveSize(cloudName: String, app: PackageEntity) {
640+
app.displayStats.apkBytes = calculateCloudAppArchiveSize(cloudName, app, DataType.PACKAGE_APK)
641+
app.displayStats.userBytes = calculateCloudAppArchiveSize(cloudName, app, DataType.PACKAGE_USER)
642+
app.displayStats.userDeBytes = calculateCloudAppArchiveSize(cloudName, app, DataType.PACKAGE_USER_DE)
643+
app.displayStats.dataBytes = calculateCloudAppArchiveSize(cloudName, app, DataType.PACKAGE_DATA)
644+
app.displayStats.obbBytes = calculateCloudAppArchiveSize(cloudName, app, DataType.PACKAGE_OBB)
645+
app.displayStats.mediaBytes = calculateCloudAppArchiveSize(cloudName, app, DataType.PACKAGE_MEDIA)
646+
appsDao.upsert(app)
647+
}
648+
624649
suspend fun protectApp(cloudName: String?, app: PackageEntity) {
625650
if (cloudName.isNullOrEmpty().not()) {
626651
cloudName?.apply {

source/core/data/src/main/kotlin/com/xayah/core/data/repository/FilesRepo.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,21 @@ class FilesRepo @Inject constructor(
286286
filesDao.upsert(file)
287287
}
288288

289+
// 添加云端文件大小计算
290+
suspend fun calculateCloudFileArchiveSize(cloudName: String, file: MediaEntity) = runCatching {
291+
var size = 0L
292+
cloudRepo.withClient(cloudName) { client, entity ->
293+
val remote = entity.remote
294+
val remoteFilesDir = pathUtil.getCloudRemoteFilesDir(remote)
295+
val archivePath = getArchiveSrc("${remoteFilesDir}/${file.archivesRelativeDir}", file.indexInfo.compressionType)
296+
if (client.exists(archivePath)) {
297+
size = client.size(archivePath)
298+
}
299+
}
300+
file.mediaInfo.displayBytes = size
301+
filesDao.upsert(file)
302+
}.withLog()
303+
289304
private fun getArchiveSrc(dstDir: String, ct: CompressionType) = "${dstDir}/${DataType.MEDIA_MEDIA.type}.${ct.suffix}"
290305

291306
suspend fun calculateLocalFileArchiveSize(file: MediaEntity) {

source/feature/main/details/src/main/kotlin/com/xayah/feature/main/details/DetailsViewModel.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ class DetailsViewModel @Inject constructor(
9494

9595
OpType.RESTORE -> {
9696
isRefreshing.emit(true)
97-
appsRepo.calculateLocalAppArchiveSize(state.app)
97+
if (state.app.indexInfo.cloud.isNotEmpty()) {
98+
appsRepo.calculateCloudAppArchiveSize(state.app.indexInfo.cloud, state.app)
99+
} else {
100+
appsRepo.calculateLocalAppArchiveSize(state.app)
101+
}
98102
isRefreshing.emit(false)
99103
}
100104
}
@@ -111,7 +115,12 @@ class DetailsViewModel @Inject constructor(
111115

112116
OpType.RESTORE -> {
113117
isRefreshing.emit(true)
114-
filesRepo.calculateLocalFileArchiveSize(state.file)
118+
// 添加云端判断
119+
if (state.file.indexInfo.cloud.isNotEmpty()) {
120+
filesRepo.calculateCloudFileArchiveSize(state.file.indexInfo.cloud, state.file)
121+
} else {
122+
filesRepo.calculateLocalFileArchiveSize(state.file)
123+
}
115124
isRefreshing.emit(false)
116125
}
117126
}

0 commit comments

Comments
 (0)