Skip to content

Commit 4c49b22

Browse files
author
Fastace
committed
feat(cloud): 新增阿里云 OSS 云存储后端支持
与既有 S3(腾讯云 COS)/AWSS3 链路同构,新增 CloudType.ALIYUNOSS 云类型,打通 「添加账户 → 目录浏览 → restic 仓库备份/恢复」全流程。目录浏览与 rustic 仓库 访问统一走 opendal 的 oss scheme;签名沿用 opendal-service-oss 默认实现 (OSS V1,Authorization: OSS <AccessKeyId>:<Signature>),未改动默认行为。 一、数据模型(core/model) - CloudType 新增 ALIYUNOSS("Aliyun OSS") - 新增 AliyunOssExtra:accessKeyId / accessKeySecret / bucket / endpoint / protocol / resticPassword,配套新增 AliyunOssProtocol(HTTP, HTTPS)。 OSS 没有独立的 region 配置,地域由 endpoint 表达 - CloudEntity 沿用既有承载方式:host=bucket、user=AccessKeyId、pass=AccessKeySecret, 完整配置以 extra 保存 二、网络层(core/network) - 新增 AliyunOssClientImpl,实现 CloudClient,scheme 为 "oss" - buildOpendalOptions() 产出 bucket/root/endpoint/access_key_id/access_key_secret 五键, 与 rustic 仓库侧使用同一套键,避免两处语义漂移 - buildOpendalEndpoint():endpoint 已带 http(s):// 时原样使用,否则按所选协议补全, 兼容自定义域名与 ECS 的 VPC/内网地址;空值返回空串交由 opendal 走默认节点 - 列目录/建目录经 RemoteRootService.opendalList / opendalCreateDir; testConnection 以成功列举根路径为连通判据 三、restic 仓库层(core/restic) - 新增 ResticRepositoryAliyunOss:backendUrl 为 opendal:oss,root 走 shared.formatOpenDALRoot(remotePath),其余四键与目录浏览一致 - CloudResticBackendModule 新增 @BINDS @Intomap @CloudTypeKey(CloudType.ALIYUNOSS) 绑定,注册方式与 AWSS3/COS 相同 - BackupServiceCloudImpl 的 restic 备份分支加入 CloudType.ALIYUNOSS 四、UI / 路由 / 文案 - Routes 新增 MainRoutes.AliyunOssSetup;MainActivity 的 NavHost 注册 PageAliyunOssSetup;云列表跳转与「添加账户」入口按 ALIYUNOSS 分发 - ModelUtil 为 CloudType.ALIYUNOSS 指定 Storage 图标 - 新增 AliyunOssSetup 引导页:AccessKey ID / AccessKey Secret / Bucket / Endpoint (含填写说明与完整访问地址预览)/ 协议分段按钮 / 仓库路径与密码 / 连接测试 / 仓库初始化, 配套新增 AliyunOssResticViewModel - IndexViewModel 新增 buildAliyunOssCloudEntity 与 updateAliyunOssEntity - feature:main:cloud 的 values 与 values-zh-rCN 同步补齐双语文案: cloud_type_aliyun_oss / aliyun_oss_setup / aliyun_oss_access_key_id / aliyun_oss_access_key_secret / aliyun_oss_endpoint / aliyun_oss_endpoint_example / aliyun_oss_url_preview 五、偏好存储(core/datastore) - 新增 aliyun_oss_restic_repo_path / aliyun_oss_restic_password / aliyun_oss_restic_initialized 三个键及其 save/read 扩展函数,与既有云类型保持一致 六、native - source/native/.../rustic/rustic/Cargo.toml 的 opendal features 追加 services-oss (目录浏览所需;与 rustic_backend 复用同一 opendal 版本以合并特性) 已知限制 - 签名使用 opendal 0.57.0 的默认 OSS V1。该版本未暴露 region / signing_version 配置, 故保持默认,不做任何 patch 或 vendor:真·阿里云 OSS 桶的首页与翻页请求均正常; 若把 endpoint 指向腾讯云 COS 的兼容端点,翻页会返回 403(COS 端不接受 V1 待签串中 的 continuation-token),该场景请使用 CloudType.S3 走原生 COS 实现 - opendal 需带上 services-oss 才会识别 oss scheme,因此此改动依赖重新构建 rustic-jni 验证 - 本机无 Android SDK / Gradle 环境,未执行 :app 编译;本次为新增文件 + 与 AWSS3/COS 同构的类型分发分支,未改动既有云类型的任何行为 - 以自写签名探针直连真实阿里云 OSS(oss-cn-beijing 桶)实测:V1 首页与携带 continuation-token 的翻页请求均返回 200
1 parent 8fde263 commit 4c49b22

19 files changed

Lines changed: 1188 additions & 2 deletions

File tree

‎source/app/src/main/kotlin/com/xayah/databackup/MainActivity.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import com.xayah.feature.main.cloud.add.PageSMBSetup
5454
import com.xayah.feature.main.cloud.add.PageWebDAVSetup
5555
import com.xayah.feature.main.cloud.add.PageS3Setup
5656
import com.xayah.feature.main.cloud.add.PageAwsS3Setup
57+
import com.xayah.feature.main.cloud.add.PageAliyunOssSetup
5758
import com.xayah.feature.main.configurations.PageConfigurations
5859
import com.xayah.feature.main.dashboard.PageDashboard
5960
import com.xayah.feature.main.details.DetailsRoute
@@ -195,6 +196,9 @@ class MainActivity : AppCompatActivity() {
195196
composable(MainRoutes.AwsS3Setup.route) {
196197
PageAwsS3Setup()
197198
}
199+
composable(MainRoutes.AliyunOssSetup.route) {
200+
PageAliyunOssSetup()
201+
}
198202
composable(
199203
route = MainRoutes.List.route,
200204
arguments = listOf(

‎source/core/datastore/src/main/kotlin/com/xayah/core/datastore/DbPreferencesDataSource.kt‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ val KeyAwsS3ResticRepoPath = stringPreferencesKey("aws_s3_restic_repo_path")
3232
val KeyAwsS3ResticPassword = stringPreferencesKey("aws_s3_restic_password")
3333
val KeyAwsS3ResticInitialized = booleanPreferencesKey("aws_s3_restic_initialized")
3434

35+
// 阿里云 OSS Restic 配置键
36+
val KeyAliyunOssResticRepoPath = stringPreferencesKey("aliyun_oss_restic_repo_path")
37+
val KeyAliyunOssResticPassword = stringPreferencesKey("aliyun_oss_restic_password")
38+
val KeyAliyunOssResticInitialized = booleanPreferencesKey("aliyun_oss_restic_initialized")
39+
3540
// FTP Restic 配置键
3641
val KeyFtpResticRepoPath = stringPreferencesKey("ftp_restic_repo_path")
3742
val KeyFtpResticPassword = stringPreferencesKey("ftp_restic_password")
@@ -149,6 +154,43 @@ suspend fun Context.readAwsS3ResticRepoPath(): String? {
149154
}.first()
150155
}
151156

157+
// 阿里云 OSS Restic 配置扩展函数
158+
suspend fun Context.saveAliyunOssResticPassword(password: String) {
159+
dataStore.edit { settings ->
160+
settings[KeyAliyunOssResticPassword] = password
161+
}
162+
}
163+
164+
suspend fun Context.readAliyunOssResticPassword(): String? {
165+
return dataStore.data.map { preferences ->
166+
preferences[KeyAliyunOssResticPassword]
167+
}.first()
168+
}
169+
170+
suspend fun Context.saveAliyunOssResticInitialized(initialized: Boolean) {
171+
dataStore.edit { settings ->
172+
settings[KeyAliyunOssResticInitialized] = initialized
173+
}
174+
}
175+
176+
suspend fun Context.readAliyunOssResticInitialized(): Boolean {
177+
return dataStore.data.map { preferences ->
178+
preferences[KeyAliyunOssResticInitialized] ?: false
179+
}.first()
180+
}
181+
182+
suspend fun Context.saveAliyunOssResticRepoPath(path: String) {
183+
dataStore.edit { settings ->
184+
settings[KeyAliyunOssResticRepoPath] = path
185+
}
186+
}
187+
188+
suspend fun Context.readAliyunOssResticRepoPath(): String? {
189+
return dataStore.data.map { preferences ->
190+
preferences[KeyAliyunOssResticRepoPath]
191+
}.first()
192+
}
193+
152194
// FTP Restic 配置扩展函数
153195
suspend fun Context.saveFtpResticPassword(password: String) {
154196
dataStore.edit { settings ->

‎source/core/model/src/main/kotlin/com/xayah/core/model/Enum.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ enum class CloudType(val title: String) {
9797
SMB("SMB"),
9898
S3("Tencent Cloud COS"),
9999
AWSS3("Amazon S3"),
100+
ALIYUNOSS("Aliyun OSS"),
100101
}
101102

102103
enum class SFTPAuthMode(val index: Int) {

‎source/core/model/src/main/kotlin/com/xayah/core/model/database/CloundEntity.kt‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ enum class AwsS3Protocol {
7474
HTTPS
7575
}
7676

77+
/**
78+
* 阿里云 OSS 账户附加配置。
79+
* 字段与 opendal 0.57 `opendal-service-oss` 的 OssConfig 一一对应:
80+
* bucket / endpoint / access_key_id / access_key_secret(OSS 无 region,地域由 endpoint 表达)。
81+
* endpoint 必须填写(如 oss-cn-hangzhou.aliyuncs.com),未带 scheme 时按 protocol 补 http(s)://。
82+
*/
83+
@Serializable
84+
data class AliyunOssExtra(
85+
val type: String = "ALIYUNOSS",
86+
val accessKeyId: String,
87+
val accessKeySecret: String,
88+
val bucket: String,
89+
val endpoint: String = "",
90+
val protocol: AliyunOssProtocol = AliyunOssProtocol.HTTPS,
91+
val resticPassword: String = "",
92+
)
93+
94+
enum class AliyunOssProtocol {
95+
HTTP,
96+
HTTPS
97+
}
98+
7799
@Entity
78100
data class CloudEntity(
79101
@PrimaryKey var name: String,
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package com.xayah.core.network.client
2+
3+
import android.content.Context
4+
import com.xayah.core.model.database.AliyunOssExtra
5+
import com.xayah.core.model.database.AliyunOssProtocol
6+
import com.xayah.core.model.database.CloudEntity
7+
import com.xayah.core.network.R
8+
import com.xayah.core.network.util.getExtraEntity
9+
import com.xayah.core.rootservice.parcelables.PathParcelable
10+
import com.xayah.core.rootservice.service.RemoteRootService
11+
import com.xayah.core.util.GsonUtil
12+
import com.xayah.core.util.LogUtil
13+
import com.xayah.core.util.withMainContext
14+
import com.xayah.libpickyou.PickYouLauncher
15+
import com.xayah.libpickyou.parcelables.DirChildrenParcelable
16+
import com.xayah.libpickyou.parcelables.FileParcelable
17+
import com.xayah.libpickyou.ui.model.PickerType
18+
import kotlinx.coroutines.runBlocking
19+
20+
class AliyunOssClientImpl(
21+
private val entity: CloudEntity,
22+
private val extra: AliyunOssExtra,
23+
private val rootService: RemoteRootService,
24+
) : CloudClient {
25+
26+
companion object {
27+
private const val SCHEME = "oss"
28+
}
29+
30+
private fun log(msg: () -> String): String = run {
31+
LogUtil.log { "AliyunOssClientImpl" to msg() }
32+
msg()
33+
}
34+
35+
private fun normalizeObjectKey(path: String): String {
36+
return path.trim('/').replace("//", "/")
37+
}
38+
39+
private fun buildOpendalOptions(): Map<String, String> {
40+
return mapOf(
41+
"bucket" to extra.bucket,
42+
"root" to "/",
43+
"endpoint" to buildOpendalEndpoint(),
44+
"access_key_id" to extra.accessKeyId,
45+
"access_key_secret" to extra.accessKeySecret,
46+
)
47+
}
48+
49+
private fun buildOpendalEndpoint(): String {
50+
val raw = extra.endpoint.trim().removeSuffix("/")
51+
if (raw.isEmpty()) return ""
52+
if (raw.startsWith("http://") || raw.startsWith("https://")) return raw
53+
val protocol = when (extra.protocol) {
54+
AliyunOssProtocol.HTTP -> "http"
55+
AliyunOssProtocol.HTTPS -> "https"
56+
}
57+
return "$protocol://$raw"
58+
}
59+
60+
override fun connect() {}
61+
62+
override fun disconnect() {}
63+
64+
override fun mkdir(dst: String) {
65+
val path = normalizeObjectKey(dst)
66+
log { "mkdir(opendal): $path" }
67+
runBlocking {
68+
rootService.opendalCreateDir(SCHEME, path, buildOpendalOptions())
69+
}
70+
}
71+
72+
override fun mkdirRecursively(dst: String) {
73+
mkdir(dst)
74+
}
75+
76+
override fun renameTo(
77+
src: String,
78+
dst: String,
79+
onProgress: ((currentPart: Int, totalParts: Int, currentFile: Int, totalFiles: Int) -> Unit)?
80+
) {
81+
log { "renameTo is deprecated for Aliyun OSS, skipping: $src to $dst" }
82+
}
83+
84+
override fun upload(
85+
src: String,
86+
dst: String,
87+
onUploading: (read: Long, total: Long) -> Unit,
88+
isCanceled: (() -> Boolean)?
89+
) {
90+
throw UnsupportedOperationException("Aliyun OSS upload is handled by rustic; direct upload is no longer supported.")
91+
}
92+
93+
override fun download(src: String, dst: String, onDownloading: (written: Long, total: Long) -> Unit) {
94+
throw UnsupportedOperationException("Aliyun OSS download is handled by rustic; direct download is no longer supported.")
95+
}
96+
97+
override fun deleteFile(src: String) {
98+
throw UnsupportedOperationException("Aliyun OSS deleteFile is handled by rustic; not supported here.")
99+
}
100+
101+
override fun removeDirectory(src: String): Boolean {
102+
throw UnsupportedOperationException("Aliyun OSS removeDirectory is handled by rustic; not supported here.")
103+
}
104+
105+
override fun deleteRecursively(src: String): Boolean {
106+
throw UnsupportedOperationException("Aliyun OSS deleteRecursively is handled by rustic; not supported here.")
107+
}
108+
109+
override fun clearEmptyDirectoriesRecursively(src: String) {
110+
}
111+
112+
override fun listFiles(src: String): DirChildrenParcelable {
113+
log { "listFiles(opendal): $src" }
114+
val files = mutableListOf<FileParcelable>()
115+
val directories = mutableListOf<FileParcelable>()
116+
117+
val path = if (src.isEmpty()) "/" else normalizeObjectKey(src)
118+
val raw = runBlocking {
119+
rootService.opendalList(SCHEME, path, buildOpendalOptions())
120+
}
121+
122+
raw.lineSequence().forEach { line ->
123+
if (line.isEmpty()) return@forEach
124+
when {
125+
line.startsWith("d:") -> {
126+
val name = line.removePrefix("d:").removeSuffix("/")
127+
if (name.isNotEmpty()) directories.add(FileParcelable(name, 0))
128+
}
129+
130+
line.startsWith("f:") -> {
131+
val rest = line.removePrefix("f:")
132+
val sep = rest.lastIndexOf(':')
133+
val name = if (sep >= 0) rest.substring(0, sep) else rest
134+
val mtime = if (sep >= 0) rest.substring(sep + 1).toLongOrNull() ?: 0L else 0L
135+
if (name.isNotEmpty()) files.add(FileParcelable(name, mtime))
136+
}
137+
}
138+
}
139+
140+
files.sortBy { it.name }
141+
directories.sortBy { it.name }
142+
return DirChildrenParcelable(files = files, directories = directories)
143+
}
144+
145+
override fun walkFileTree(path: String): List<PathParcelable> {
146+
throw UnsupportedOperationException("Aliyun OSS walkFileTree is handled by rustic; not supported here.")
147+
}
148+
149+
override fun exists(src: String): Boolean {
150+
throw UnsupportedOperationException("Aliyun OSS exists is handled by rustic; not supported here.")
151+
}
152+
153+
override fun size(src: String): Long {
154+
throw UnsupportedOperationException("Aliyun OSS size is handled by rustic; not supported here.")
155+
}
156+
157+
override suspend fun testConnection() {
158+
log { "testConnection(opendal): scheme=$SCHEME endpoint=${buildOpendalEndpoint()} bucket=${extra.bucket}" }
159+
rootService.opendalList(SCHEME, "/", buildOpendalOptions())
160+
}
161+
162+
override suspend fun setRemote(context: Context, onSet: suspend (remote: String, extra: String) -> Unit) {
163+
val currentExtra = entity.getExtraEntity<AliyunOssExtra>()!!
164+
val prefix = "${context.getString(R.string.cloud)}:"
165+
val pickYou = PickYouLauncher(
166+
checkPermission = false,
167+
traverseBackend = { listFiles(it.replaceFirst(prefix, "")) },
168+
mkdirsBackend = { parent, child ->
169+
runCatching {
170+
val path = "$parent/$child".replaceFirst(prefix, "").trim('/')
171+
mkdirRecursively(path)
172+
}.isSuccess
173+
},
174+
title = context.getString(R.string.select_target_directory),
175+
pickerType = PickerType.DIRECTORY,
176+
rootPathList = listOf(prefix),
177+
defaultPathList = listOf(prefix),
178+
)
179+
withMainContext {
180+
val pathString = pickYou.awaitLaunch(context)
181+
val remotePath = pathString.replaceFirst(prefix, "").trim('/')
182+
onSet(remotePath, GsonUtil().toJson(currentExtra))
183+
}
184+
}
185+
}

‎source/core/network/src/main/kotlin/com/xayah/core/network/client/CloudClient.kt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.xayah.core.network.util.getExtraEntity
1010
import com.xayah.core.rootservice.parcelables.PathParcelable
1111
import com.xayah.libpickyou.parcelables.DirChildrenParcelable
1212
import com.xayah.core.model.database.S3Extra
13+
import com.xayah.core.model.database.AliyunOssExtra
1314
import com.xayah.core.model.database.AwsS3Extra
1415
import com.xayah.core.model.database.SMBExtra
1516
import com.xayah.core.rootservice.service.RemoteRootService
@@ -64,6 +65,11 @@ fun CloudEntity.getCloud(rootService: RemoteRootService) = when (this.type) {
6465
AwsS3ClientImpl(this, extra, rootService)
6566
}
6667

68+
CloudType.ALIYUNOSS -> {
69+
val extra = getExtraEntity<AliyunOssExtra>()!!
70+
AliyunOssClientImpl(this, extra, rootService)
71+
}
72+
6773
CloudType.SMB -> {
6874
val extra = getExtraEntity<SMBExtra>()!!
6975
SMBClientImpl(this, extra, rootService)

0 commit comments

Comments
 (0)