-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 修改构建脚本 - 更改插件加载方式,从 JarClassLoader 到 ClassLoaderUtils,现在直接读到和主线程一样的 ClassLoader,不会在造成无法访问到类的情况 - 添加资源上传方法 - 添加阿里云OSS资源插件(支持资源上传) - 添加客户端请求工具(未完成) - 添加以前没上传的文档资源
- Loading branch information
Showing
14 changed files
with
456 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Booster Plugins | ||
|
||
Plugin projects running on Booster loader. | ||
|
||
## Plugins | ||
|
||
- [Aliyun OSS Resource Provider](aliyun-oss-resource) |
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,17 @@ | ||
plugins { | ||
kotlin("jvm") | ||
id("com.github.johnrengelman.shadow") version "7.1.2" | ||
} | ||
|
||
group = "explode" | ||
version = "1.0.0" | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compileOnly(project(":booster-resource")) | ||
|
||
implementation("com.aliyun.oss:aliyun-sdk-oss:3.16.0") | ||
} |
150 changes: 150 additions & 0 deletions
150
...esource/src/main/kotlin/explode2/booster/resource/oss/aliyun/AliyunOSSResourceProvider.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,150 @@ | ||
package explode2.booster.resource.oss.aliyun | ||
|
||
import com.aliyun.oss.OSS | ||
import com.aliyun.oss.OSSClientBuilder | ||
import com.github.taskeren.config.Configuration | ||
import explode2.booster.resource.RedirectResourceProvider | ||
import explode2.booster.resource.ResourceReceiver | ||
import explode2.labyrinth.LabyrinthPlugin.Companion.labyrinth | ||
import explode2.logging.Colors | ||
import org.slf4j.LoggerFactory | ||
import java.io.ByteArrayInputStream | ||
import java.io.File | ||
import java.util.* | ||
|
||
private const val MUSIC_TYPE = "music" | ||
private const val PREVIEW_MUSIC_TYPE = "preview-music" | ||
private const val COVER_PICTURE_TYPE = "cover-picture" | ||
private const val STORE_COVER_PICTURE_TYPE = "store-cover" | ||
private const val CHART_MAP_TYPE = "chart-map" | ||
private const val USER_AVATAR_TYPE = "user-avatar" | ||
|
||
class AliyunOSSResourceProvider : RedirectResourceProvider, ResourceReceiver { | ||
|
||
companion object { | ||
|
||
private val logger = LoggerFactory.getLogger(AliyunOSSResourceProvider::class.java) | ||
|
||
private val config = Configuration(File("aliyun-oss.resource.cfg")) | ||
|
||
private val endpoint: String = | ||
config.getString("endpoint", "oss", "http://oss-cn-hangzhou.aliyuncs.com", "地域节点地址") | ||
|
||
private val accessKeyId: String = | ||
config.getString("access-key-id", "user-access", "", "用户 AccessKeyId") | ||
private val accessKeySecret: String = | ||
config.getString("access-key-secret", "user-access", "", "用户 AccessKeySecret") | ||
|
||
private val bucketName: String = | ||
config.getString("bucket-name", "bucket", "explode-oss-resource", "Bucket 名称") | ||
private val resourceKeyPattern: String = | ||
config.getString( | ||
"resource-key-pattern", | ||
"bucket", | ||
"{type}-{id}", | ||
"资源 Key 模板({type} 为资源类型,{id} 为资源ID)" | ||
) | ||
|
||
init { | ||
config.save() | ||
} | ||
|
||
private val oss: OSS = OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret) | ||
|
||
init { | ||
if(oss.doesBucketExist(bucketName)) { | ||
logger.info("${Colors.Light.Green}Aliyun OSS bucket $bucketName is available") | ||
} else { | ||
logger.error("${Colors.Light.Red}Aliyun OSS bucket $bucketName was not ready!") | ||
throw IllegalStateException("Aliyun OSS bucket $bucketName was not ready!") | ||
} | ||
} | ||
} | ||
|
||
private fun getResourceKey(type: String, id: String): String { | ||
return resourceKeyPattern.replace("{type}", type).replace("{id}", id) | ||
} | ||
|
||
private fun getResourceUrl(type: String, id: String): String { | ||
val resourceKey = getResourceKey(type, id) | ||
|
||
if(oss.doesObjectExist(bucketName, resourceKey)) { | ||
// TODO: 调整过期时间 | ||
val expiration = Date(Date().time + 3600 * 1000) | ||
return oss.generatePresignedUrl(bucketName, resourceKey, expiration).toString() | ||
} else { | ||
throw IllegalStateException("requested resource $resourceKey does not exist in bucket $bucketName") | ||
} | ||
} | ||
|
||
/** | ||
* 检查 Token 不为空,切用户存在 | ||
*/ | ||
private fun validateToken(token: String?) { | ||
labyrinth.gameUserFactory.getGameUserById(token ?: error("soudayo is undefined")) ?: error("invalid token") | ||
} | ||
|
||
private fun validateTokenUpload(token: String?) { | ||
labyrinth.gameUserFactory.getGameUserById(token ?: error("soudayo is undefined")) ?: error("invalid token") | ||
} | ||
|
||
override fun getMusic(id: String, token: String?): String { | ||
validateToken(token) | ||
return getResourceUrl(MUSIC_TYPE, id) | ||
} | ||
|
||
override fun getPreviewMusic(id: String, token: String?): String { | ||
validateToken(token) | ||
return getResourceUrl(PREVIEW_MUSIC_TYPE, id) | ||
} | ||
|
||
override fun getCoverPicture(id: String, token: String?): String { | ||
validateToken(token) | ||
return getResourceUrl(COVER_PICTURE_TYPE, id) | ||
} | ||
|
||
override fun getStoreCoverPicture(id: String, token: String?): String { | ||
validateToken(token) | ||
return getResourceUrl(STORE_COVER_PICTURE_TYPE, id) | ||
} | ||
|
||
override fun getChartXML(cid: String, sid: String, token: String?): String { | ||
validateToken(token) | ||
return getResourceUrl(CHART_MAP_TYPE, cid) | ||
} | ||
|
||
override fun getUserAvatar(id: String, token: String?): String { | ||
validateToken(token) | ||
return getResourceUrl(USER_AVATAR_TYPE, id) | ||
} | ||
|
||
override fun uploadMusic(id: String, token: String?, content: ByteArray) { | ||
validateTokenUpload(token) | ||
oss.putObject(bucketName, getResourceKey(MUSIC_TYPE, id), ByteArrayInputStream(content)) | ||
} | ||
|
||
override fun uploadPreviewMusic(id: String, token: String?, content: ByteArray) { | ||
validateTokenUpload(token) | ||
oss.putObject(bucketName, getResourceKey(PREVIEW_MUSIC_TYPE, id), ByteArrayInputStream(content)) | ||
} | ||
|
||
override fun uploadCoverPicture(id: String, token: String?, content: ByteArray) { | ||
validateTokenUpload(token) | ||
oss.putObject(bucketName, getResourceKey(COVER_PICTURE_TYPE, id), ByteArrayInputStream(content)) | ||
} | ||
|
||
override fun uploadStoreCoverPicture(id: String, token: String?, content: ByteArray) { | ||
validateTokenUpload(token) | ||
oss.putObject(bucketName, getResourceKey(STORE_COVER_PICTURE_TYPE, id), ByteArrayInputStream(content)) | ||
} | ||
|
||
override fun uploadChartXML(cid: String, sid: String, token: String?, content: ByteArray) { | ||
validateTokenUpload(token) | ||
oss.putObject(bucketName, getResourceKey(CHART_MAP_TYPE, cid), ByteArrayInputStream(content)) | ||
} | ||
|
||
override fun uploadUserAvatar(id: String, token: String?, content: ByteArray) { | ||
validateTokenUpload(token) | ||
oss.putObject(bucketName, getResourceKey(USER_AVATAR_TYPE, id), ByteArrayInputStream(content)) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...-resource/src/main/resources/META-INF/services/explode2.booster.resource.ResourceProvider
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 @@ | ||
explode2.booster.resource.oss.aliyun.AliyunOSSResourceProvider |
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
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,22 @@ | ||
plugins { | ||
kotlin("jvm") version "1.7.20" | ||
} | ||
|
||
group = "explode" | ||
version = "1.0-SNAPSHOT" | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
val ktorVer = "2.2.1" | ||
fun ktor(moduleName: String) = implementation("io.ktor:${moduleName}:${ktorVer}") | ||
ktor("ktor-client-core") | ||
ktor("ktor-client-okhttp") | ||
ktor("ktor-client-content-negotiation") | ||
ktor("ktor-serialization-gson") | ||
implementation("com.google.code.gson:gson:2.10") | ||
|
||
testImplementation(kotlin("test")) | ||
} |
Oops, something went wrong.