-
Notifications
You must be signed in to change notification settings - Fork 34
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
Showing
11 changed files
with
256 additions
and
91 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
51 changes: 51 additions & 0 deletions
51
buildSrc/src/main/java/customskinloader/gradle/storage/CosStorage.java
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,51 @@ | ||
package customskinloader.gradle.storage; | ||
|
||
import com.qcloud.cos.COSClient; | ||
import com.qcloud.cos.ClientConfig; | ||
import com.qcloud.cos.auth.BasicCOSCredentials; | ||
import com.qcloud.cos.auth.COSCredentials; | ||
import com.qcloud.cos.model.PutObjectRequest; | ||
import com.qcloud.cos.region.Region; | ||
import customskinloader.gradle.util.CdnUtil; | ||
|
||
import java.nio.file.Path; | ||
|
||
/** | ||
* Tencent Cloud COS | ||
*/ | ||
public class CosStorage implements Storage { | ||
private static final String BUCKET_NAME = System.getenv("COS_BUCKET"); | ||
private COSClient cosClient = null; | ||
|
||
private COSClient getCosClient() { | ||
if (cosClient != null) { | ||
return cosClient; | ||
} | ||
|
||
synchronized (this) { | ||
if (cosClient != null) { | ||
return cosClient; | ||
} | ||
|
||
COSCredentials cred = new BasicCOSCredentials( | ||
System.getenv("COS_SECRET_ID"), | ||
System.getenv("COS_SECRET_KEY") | ||
); | ||
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai")); | ||
cosClient = new COSClient(cred, clientConfig); | ||
|
||
return cosClient; | ||
} | ||
} | ||
|
||
@Override | ||
public void put(String key, Path file) { | ||
PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, key, file.toFile()); | ||
getCosClient().putObject(putObjectRequest); | ||
} | ||
|
||
@Override | ||
public String getPublicBaseUrl() { | ||
return CdnUtil.TENCENT_CDN_ROOT; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
buildSrc/src/main/java/customskinloader/gradle/storage/R2Storage.java
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,44 @@ | ||
package customskinloader.gradle.storage; | ||
|
||
import customskinloader.gradle.util.CdnUtil; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.net.URI; | ||
|
||
/** | ||
* CloudFlare R2 | ||
*/ | ||
public class R2Storage extends S3Storage { | ||
@Nonnull | ||
@Override | ||
protected S3Client initClient() { | ||
try { | ||
return S3Client.builder() | ||
.endpointOverride(new URI(System.getenv("R2_BASE_URL"))) | ||
.credentialsProvider( | ||
StaticCredentialsProvider.create( | ||
AwsBasicCredentials.create( | ||
System.getenv("R2_SECRET_ID"), | ||
System.getenv("R2_SECRET_KEY")))) | ||
.region(Region.of("auto")) | ||
.build(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
protected String getBucket() { | ||
return System.getenv("R2_BUCKET"); | ||
} | ||
|
||
@Override | ||
public String getPublicBaseUrl() { | ||
return CdnUtil.CLOUDFLARE_CDN_ROOT; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
buildSrc/src/main/java/customskinloader/gradle/storage/S3Storage.java
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,50 @@ | ||
package customskinloader.gradle.storage; | ||
|
||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Amazon Web Service Simple Storage Service | ||
* There is also plenty of S3 compatible API. | ||
*/ | ||
public abstract class S3Storage implements Storage { | ||
private S3Client client; | ||
|
||
/** | ||
* The S3 client will be initialized only once. | ||
* @return S3 client | ||
*/ | ||
protected abstract @Nonnull S3Client initClient(); | ||
|
||
private @Nonnull S3Client getClient() { | ||
if (client != null) { | ||
return client; | ||
} | ||
synchronized (this) { | ||
if (client != null) { | ||
return client; | ||
} | ||
client = initClient(); | ||
return client; | ||
} | ||
} | ||
|
||
/** | ||
* Get bucket for object. | ||
* @return bucket | ||
*/ | ||
protected abstract @Nonnull String getBucket(); | ||
|
||
@Override | ||
public void put(String key, Path file) { | ||
getClient().putObject( | ||
PutObjectRequest.builder() | ||
.bucket(getBucket()) | ||
.key(key).build(), | ||
file | ||
); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
buildSrc/src/main/java/customskinloader/gradle/storage/Storage.java
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,14 @@ | ||
package customskinloader.gradle.storage; | ||
|
||
import java.nio.file.Path; | ||
|
||
public interface Storage { | ||
/** | ||
* Put file to object storage service. | ||
* @param key key | ||
* @param file file | ||
*/ | ||
void put(String key, Path file); | ||
|
||
String getPublicBaseUrl(); | ||
} |
50 changes: 50 additions & 0 deletions
50
buildSrc/src/main/java/customskinloader/gradle/storage/StorageService.java
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,50 @@ | ||
package customskinloader.gradle.storage; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class StorageService { | ||
private static final List<Storage> storages = new ArrayList<>(); | ||
|
||
static { | ||
storages.add(new CosStorage()); | ||
storages.add(new R2Storage()); | ||
} | ||
|
||
/** | ||
* Put file to all storage. | ||
* | ||
* @param key key | ||
* @param file file | ||
*/ | ||
public static void put(String key, Path file) { | ||
storages.forEach(it -> it.put(key, file)); | ||
} | ||
|
||
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); | ||
|
||
/** | ||
* Put object to all storage. | ||
* | ||
* @param key key | ||
* @param obj obj | ||
*/ | ||
public static void put(String key, Object obj) throws IOException { | ||
for (Storage it : storages) { | ||
Path file = Paths.get("build/libs/", key); | ||
Files.write(file, | ||
gson.toJson(obj).replace(BASE_URL, it.getPublicBaseUrl()).getBytes(StandardCharsets.UTF_8)); | ||
it.put(key, file); | ||
} | ||
} | ||
|
||
public static final String BASE_URL = "{BASE_URL}"; | ||
} |
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
Oops, something went wrong.