-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5427655
commit 2d63f71
Showing
13 changed files
with
209 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
plugins { | ||
id 'com.android.library' | ||
id 'kotlin-android' | ||
id 'kotlin-parcelize' | ||
id 'kotlin-kapt' | ||
id 'com.google.devtools.ksp' | ||
id 'maven-publish' | ||
} | ||
|
||
android { | ||
namespace 'com.xiaojinzi.module.common.ali.oss' | ||
} | ||
|
||
apply from: project.getRootProjectPath() + "/common_module.gradle" | ||
|
||
ksp { | ||
arg("ModuleName", "module_common_ali_oss") | ||
} | ||
|
||
dependencies { | ||
// 依赖 ali oss | ||
implementation 'com.aliyun.dpa:oss-android-sdk:2.9.19' | ||
} | ||
|
||
apply from: "../common_publish.gradle" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> |
21 changes: 21 additions & 0 deletions
21
module-ali-oss/src/main/java/com/xiaojinzi/module/common/ali/oss/AliOssModuleApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.xiaojinzi.module.common.ali.oss | ||
|
||
import android.app.Application | ||
import com.xiaojinzi.component.anno.ModuleAppAnno | ||
import com.xiaojinzi.component.application.IApplicationLifecycle | ||
import com.xiaojinzi.component.application.IModuleNotifyChanged | ||
|
||
|
||
@ModuleAppAnno | ||
class AliOssModuleApplication : IApplicationLifecycle, IModuleNotifyChanged { | ||
|
||
override fun onModuleChanged(app: Application) { | ||
} | ||
|
||
override fun onCreate(app: Application) { | ||
} | ||
|
||
override fun onDestroy() { | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
...-oss/src/main/java/com/xiaojinzi/module/common/ali/oss/spi/AliOssFileUploadServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.xiaojinzi.module.common.ali.oss.spi | ||
|
||
import com.alibaba.sdk.android.oss.ClientException | ||
import com.alibaba.sdk.android.oss.OSSClient | ||
import com.alibaba.sdk.android.oss.ServiceException | ||
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback | ||
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider | ||
import com.alibaba.sdk.android.oss.common.auth.OSSFederationCredentialProvider | ||
import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken | ||
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask | ||
import com.alibaba.sdk.android.oss.model.PutObjectRequest | ||
import com.alibaba.sdk.android.oss.model.PutObjectResult | ||
import com.xiaojinzi.component.anno.ServiceAnno | ||
import com.xiaojinzi.module.common.base.spi.AliOssSpi | ||
import com.xiaojinzi.module.common.base.spi.FileUploadServiceBaseImpl | ||
import com.xiaojinzi.module.common.base.spi.FileUploadTaskDto | ||
import com.xiaojinzi.support.ktx.app | ||
import com.xiaojinzi.support.ktx.extension | ||
import com.xiaojinzi.support.ktx.newUUid | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
@ServiceAnno( | ||
value = [AliOssSpi::class] | ||
) | ||
class AliOssFileUploadServiceImpl : FileUploadServiceBaseImpl(), AliOssSpi { | ||
|
||
private val taskMap: ConcurrentHashMap<String, OSSAsyncTask<PutObjectResult>> = | ||
ConcurrentHashMap() | ||
|
||
private var endpoint: String = "" | ||
private var bucket: String = "" | ||
|
||
override suspend fun init(endpoint: String, bucket: String) { | ||
this.endpoint = endpoint | ||
this.bucket = bucket | ||
} | ||
|
||
override fun doUpload(task: FileUploadTaskDto) { | ||
|
||
val fileKey = "${ | ||
if (task.extendPrefix.isEmpty()) { | ||
"" | ||
} else { | ||
"${task.extendPrefix}_" | ||
} | ||
}${newUUid()}.${task.targetFile.name.extension}" | ||
|
||
val request = PutObjectRequest( | ||
bucket, | ||
fileKey, | ||
task.targetFile.path, | ||
).apply { | ||
this.setProgressCallback { _, currentSize, totalSize -> | ||
postProgress( | ||
uid = task.uuid, | ||
progress = if (totalSize <= 0) { | ||
0f | ||
} else { | ||
(currentSize.toFloat() / totalSize) | ||
}, | ||
) | ||
} | ||
} | ||
|
||
val credentialProvider: OSSCredentialProvider = object : OSSFederationCredentialProvider() { | ||
override fun getFederationToken(): OSSFederationToken { | ||
// 从您的服务器中获取Token。 | ||
return OSSFederationToken( | ||
"STS.NTGjwL7wHcGsy82MMq2Mb6wfL", | ||
"CtAnW5airamRuSZErz4aa96ByXZTcE7Jf4YupSARF4Sk", | ||
"CAISvgJ1q6Ft5B2yfSjIr5fyIc342qhp1IWYex6DqU0kPsJO2bLNrjz2IHhMeXdsAOkbsvwylWBZ6P8TlqBiStpGSAnNddMoPX+/WID4MeT7oMWQweEuqv/MQBq+aXPS2MvVfJ+KLrf0ceusbFbpjzJ6xaCAGxypQ12iN+/i6/clFKN1ODO1dj1bHtxbCxJ/ocsBTxvrOO2qLwThjxi7biMqmHIl2TovtP3mn5bNskWA1QWr8IJP+dSteKrDRtJ3IZJyX+2y2OFLbafb2EZSkUMSrPoo0PUdpGuf44DNUgcIsg/nIuuR8dAqJRdhdk2qynT/eQ2Xf5RazQ+z/+yfo34lVWrlhiB+0j9O6n9lCTHY18e4SMdq9k63pvSlHLebnf1KL/z1Gi1kFXQPhLEOJ35IaHXuToXDnvSiUkZM3vbtuMkagAEwIZysmx9gozZHC6l9JzyCN3vvRAXoQh0g5jSjRoFM3bdlv25v/f4Hh0YIEbQK946M3ruNKTTiuJnW+SdfnroLQgT19gzkX1yBDGyLGYzajBUisF3oizw4QmvTcljW14Pk8Fvzs6UuGYDs7bU8y8iidYCJjdv2nIhlSKBlQ8xJ/yAA", | ||
"2024-04-07T10:34:49Z" | ||
) | ||
} | ||
} | ||
val ossClient = OSSClient(app, endpoint, credentialProvider) | ||
|
||
ossClient.asyncPutObject( | ||
request, object : OSSCompletedCallback<PutObjectRequest, PutObjectResult> { | ||
override fun onSuccess(request: PutObjectRequest, result: PutObjectResult) { | ||
request.progressCallback = null | ||
} | ||
|
||
override fun onFailure( | ||
request: PutObjectRequest, | ||
clientException: ClientException?, | ||
serviceException: ServiceException? | ||
) { | ||
request.progressCallback = null | ||
} | ||
} | ||
).apply { | ||
taskMap[task.uuid] = this | ||
} | ||
|
||
} | ||
|
||
override suspend fun cancelTasks(uidList: List<String>) { | ||
uidList.forEach { uid -> | ||
postCancel(uid = uid) | ||
taskMap[uid]?.cancel() | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
module-base/src/main/java/com/xiaojinzi/module/common/base/spi/AliOssSpi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.xiaojinzi.module.common.base.spi | ||
|
||
|
||
interface AliOssSpi : FileUploadSpi { | ||
|
||
companion object { | ||
|
||
const val TAG = "AliOssSpi" | ||
|
||
} | ||
|
||
/** | ||
* 初始化 | ||
*/ | ||
suspend fun init( | ||
endpoint: String, | ||
bucket: String, | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters