Skip to content

Commit

Permalink
feat: Export configs to backup dir after backing up
Browse files Browse the repository at this point in the history
Change-Id: Id86629d807d1bffb7a858383ba359aa6f10266e3
  • Loading branch information
XayahSuSuSu committed Nov 17, 2024
1 parent 68429f7 commit 4592b67
Show file tree
Hide file tree
Showing 37 changed files with 240 additions and 443 deletions.
2 changes: 2 additions & 0 deletions source/app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,6 @@
<string name="apps_which_have_backups">存在备份</string>
<string name="apps_which_have_no_backups">不存在备份</string>
<string name="no_labels_here">这里没有标签。</string>
<string name="backup_configs">备份配置</string>
<string name="backup_configs_desc">将配置文件导出到备份目录</string>
</resources>
2 changes: 2 additions & 0 deletions source/app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,6 @@
<string name="apps_which_have_backups">存在備份</string>
<string name="apps_which_have_no_backups">不存在備份</string>
<string name="no_labels_here">這裏沒有標簽</string>
<string name="backup_configs">備份配置</string>
<string name="backup_configs_desc">將配置文件導出到備份目錄</string>
</resources>
2 changes: 2 additions & 0 deletions source/app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,6 @@
<string name="apps_which_have_backups">存在備份</string>
<string name="apps_which_have_no_backups">不存在備份</string>
<string name="no_labels_here">這裏沒有標簽</string>
<string name="backup_configs">備份配置</string>
<string name="backup_configs_desc">將配置文件導出到備份目錄</string>
</resources>
2 changes: 2 additions & 0 deletions source/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,6 @@
<string name="apps_which_have_backups">Apps which have backups</string>
<string name="apps_which_have_no_backups">Apps which have no backups</string>
<string name="no_labels_here">No labels here.</string>
<string name="backup_configs">Backup configs</string>
<string name="backup_configs_desc">Export configurations to backup directory</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CloudRepository @Inject constructor(
suspend fun upsert(item: CloudEntity) = cloudDao.upsert(item)
suspend fun upsert(items: List<CloudEntity>) = cloudDao.upsert(items)
suspend fun queryByName(name: String) = cloudDao.queryByName(name)
suspend fun query() = cloudDao.query()

val clouds = cloudDao.queryFlow().distinctUntilChanged()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ class LabelsRepo @Inject constructor(
@Dispatcher(Default) private val defaultDispatcher: CoroutineDispatcher,
private val labelDao: LabelDao,
) {
fun getLabels(): Flow<List<LabelEntity>> = labelDao.queryLabelsFlow().flowOn(defaultDispatcher)
fun getLabelsFlow(): Flow<List<LabelEntity>> = labelDao.queryLabelsFlow().flowOn(defaultDispatcher)
fun getAppRefsFlow(): Flow<List<LabelAppCrossRefEntity>> = labelDao.queryAppRefsFlow()
fun getFileRefsFlow(): Flow<List<LabelFileCrossRefEntity>> = labelDao.queryFileRefsFlow()
suspend fun getLabels(): List<LabelEntity> = labelDao.queryLabels()
suspend fun getAppRefs(labelIds: Set<String>): List<LabelAppCrossRefEntity> = labelDao.queryAppRefs(labelIds)
suspend fun getAppRefs(): List<LabelAppCrossRefEntity> = labelDao.queryAppRefs()
suspend fun getFileRefs(labelIds: Set<String>): List<LabelFileCrossRefEntity> = labelDao.queryFileRefs(labelIds)
suspend fun getFileRefs(): List<LabelFileCrossRefEntity> = labelDao.queryFileRefs()

/**
* Add a unique label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.xayah.core.model.database.MediaExtraInfo
import com.xayah.core.model.database.MediaIndexInfo
import com.xayah.core.model.database.MediaInfo
import com.xayah.core.rootservice.service.RemoteRootService
import com.xayah.core.util.ConfigsMediaRestoreName
import com.xayah.core.util.DateUtil
import com.xayah.core.util.LogUtil
import com.xayah.core.util.PathUtil
Expand All @@ -40,14 +39,10 @@ class MediaRepository @Inject constructor(
msg
}

private val localBackupSaveDir get() = context.localBackupSaveDir()
val backupFilesDir get() = pathUtil.getLocalBackupFilesDir()

suspend fun clearBlocked() = mediaDao.clearBlocked()
suspend fun setBlocked(id: Long, blocked: Boolean) = mediaDao.setBlocked(id, blocked)
fun queryFlow(opType: OpType, blocked: Boolean) = mediaDao.queryFlow(opType, blocked).distinctUntilChanged()
fun queryFlow(opType: OpType, cloud: String, backupDir: String) = mediaDao.queryFlow(opType, cloud, backupDir).distinctUntilChanged()
fun queryFlow(name: String, opType: OpType, preserveId: Long) = mediaDao.queryFlow(name, opType, preserveId).distinctUntilChanged()
suspend fun query(opType: OpType, blocked: Boolean) = mediaDao.query(opType, blocked)
suspend fun upsert(item: MediaEntity) = mediaDao.upsert(item)
suspend fun upsert(items: List<MediaEntity>) = mediaDao.upsert(items)
suspend fun delete(id: Long) = mediaDao.delete(id)
Expand Down Expand Up @@ -139,29 +134,6 @@ class MediaRepository @Inject constructor(
}
}

suspend fun updateLocalMediaSize(name: String, opType: OpType, preserveId: Long) {
query(opType = opType, preserveId = preserveId, name = name, cloud = "", backupDir = "").also {
if (it != null) {
it.extraInfo.existed = rootService.exists(it.path)
if (it.extraInfo.existed) {
it.mediaInfo.displayBytes = rootService.calculateSize(it.path)
}
upsert(it)
}
}
}

private suspend fun calculateLocalArchiveSize(m: MediaEntity) = rootService.calculateSize(
getArchiveDst("${backupFilesDir}/${m.archivesRelativeDir}", m.indexInfo.compressionType)
)

suspend fun updateLocalMediaArchivesSize(name: String, opType: OpType) {
query(opType, name, "", localBackupSaveDir).onEach {
it.mediaInfo.displayBytes = calculateLocalArchiveSize(it)
upsert(it)
}
}

private fun renameDuplicateMedia(name: String): String {
val nameList = name.split("_").toMutableList()
val index = nameList.first().toIntOrNull()
Expand Down Expand Up @@ -271,76 +243,4 @@ class MediaRepository @Inject constructor(
upsert(mediaEntity)
}
}

suspend fun loadMediumFromLocal() {
val path = backupFilesDir
val paths = rootService.walkFileTree(path)
paths.forEach {
val fileName = PathUtil.getFileName(it.pathString)
if (fileName == ConfigsMediaRestoreName) {
runCatching {
val stored = rootService.readJson<MediaEntity>(it.pathString).also { p ->
p?.id = 0
p?.extraInfo?.existed = true
p?.extraInfo?.activated = false
p?.indexInfo?.cloud = ""
p?.indexInfo?.backupDir = localBackupSaveDir
}
if (stored != null) {
query(
name = stored.name,
opType = stored.indexInfo.opType,
preserveId = stored.preserveId,
ct = stored.indexInfo.compressionType,
cloud = stored.indexInfo.cloud,
backupDir = localBackupSaveDir
).also { m ->
if (m == null)
mediaDao.upsert(stored)
}
}
}
}
}
}

suspend fun loadMediumFromCloud(cloud: String) = runCatching {
cloudRepository.withClient(cloud) { client, entity ->
val remote = entity.remote
val src = pathUtil.getCloudRemoteFilesDir(remote)
if (client.exists(src)) {
val paths = client.walkFileTree(src)
val tmpDir = pathUtil.getCloudTmpDir()
paths.forEach {
val fileName = PathUtil.getFileName(it.pathString)
if (fileName == ConfigsMediaRestoreName) {
runCatching {
cloudRepository.download(client = client, src = it.pathString, dstDir = tmpDir) { path ->
val stored = rootService.readJson<MediaEntity>(path).also { p ->
p?.id = 0
p?.extraInfo?.existed = true
p?.extraInfo?.activated = false
p?.indexInfo?.cloud = entity.name
p?.indexInfo?.backupDir = remote
}
if (stored != null) {
query(
name = stored.name,
opType = stored.indexInfo.opType,
preserveId = stored.preserveId,
ct = stored.indexInfo.compressionType,
cloud = entity.name,
backupDir = remote
).also { m ->
if (m == null)
mediaDao.upsert(stored)
}
}
}
}
}
}
}
}
}.onFailure(rootService.onFailure)
}
Loading

0 comments on commit 4592b67

Please sign in to comment.