forked from CFPAOrg/I18nUpdateMod3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetUtil.java
More file actions
127 lines (111 loc) · 4.64 KB
/
AssetUtil.java
File metadata and controls
127 lines (111 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package i18nupdatemod.util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
public class AssetUtil {
private static final String CFPA_ASSET_ROOT = "http://downloader1.meitangdehulu.com:22943/";
private static final List<String> MIRRORS;
static {
// 镜像地址可以改成服务器下发
MIRRORS = new ArrayList<>();
MIRRORS.add("https://raw.githubusercontent.com/");
// 此镜像源维护者:502y
MIRRORS.add("http://8.137.167.65:64684/");
}
public static void download(String url, Path localFile) throws IOException, URISyntaxException {
Log.info("Downloading: %s -> %s", url, localFile);
FileUtils.copyURLToFile(new URI(url).toURL(), localFile.toFile(),
(int) TimeUnit.SECONDS.toMillis(3), (int) TimeUnit.SECONDS.toMillis(33));
Log.debug("Downloaded: %s -> %s", url, localFile);
}
public static String getString(String url) throws IOException, URISyntaxException {
return IOUtils.toString(new URI(url).toURL(), StandardCharsets.UTF_8);
}
public static String getFastestUrl() {
List<String> urls = new ArrayList<>(MIRRORS);
urls.add(CFPA_ASSET_ROOT);
ExecutorService executor = Executors.newFixedThreadPool(Math.max(urls.size(), 10));
try {
List<CompletableFuture<String>> futures = new ArrayList<>();
for (String url : urls) {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
return testUrlConnection(url);
} catch (IOException e) {
return null; // 表示失败
}
}, executor);
futures.add(future);
}
// 阻塞等待最快完成且成功的任务
String fastest = null;
while (!futures.isEmpty()) {
CompletableFuture<Object> first = CompletableFuture.anyOf(futures.toArray(new CompletableFuture[0]));
fastest = (String) first.join();
// 移除已完成的 future
futures.removeIf(CompletableFuture::isDone);
if (fastest != null) {
// 成功,取消其他任务
for (CompletableFuture<String> f : futures) {
f.cancel(true);
}
Log.info("Using fastest url: %s", fastest);
return fastest;
}
}
// 全部失败,返回默认 URL
Log.info("All urls are unreachable, using CFPA_ASSET_ROOT");
return CFPA_ASSET_ROOT;
} finally {
executor.shutdownNow();
}
}
private static String testUrlConnection(String url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("HEAD");
conn.setConnectTimeout(3000);
conn.setReadTimeout(5000);
conn.connect();
int code = conn.getResponseCode();
if (code >= 200 && code < 300) {
return url;
}
Log.debug("URL unreachable: %s, code: %d", url, code);
throw new IOException("URL unreachable: " + url);
}
@NotNull
public static Map<String, String> getGitIndex() {
try {
URL index_url = new URL("https://raw.githubusercontent.com/CFPAOrg/Minecraft-Mod-Language-Package/refs/heads/index/version-index.json");
HttpURLConnection httpConn = (HttpURLConnection) index_url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setConnectTimeout(5000);
httpConn.setReadTimeout(5000);
try (InputStreamReader reader = new InputStreamReader(httpConn.getInputStream())) {
Type mapType = new TypeToken<Map<String, String>>() {
}.getType();
return new Gson().fromJson(reader, mapType);
} finally {
httpConn.disconnect();
}
} catch (Exception ignore) {
return new HashMap<>();
}
}
}