|
| 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 | +} |
0 commit comments