Skip to content

Commit d84596d

Browse files
committed
Todo: files seems strange while downloading
1 parent e368bf6 commit d84596d

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

src/main/java/org/maxgamer/quickshop/util/language/text/TextManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void load() {
6161
Map<String, JsonConfiguration> fileLocaleMapping = locale2ContentMapping.computeIfAbsent(languageFileCrowdin, k -> new HashMap<>());
6262
JsonConfiguration configuration = new JsonConfiguration();
6363
fileLocaleMapping.put(availableLanguage, configuration);
64+
plugin.getLogger().info(localeFileContent);
6465
configuration.loadFromString(localeFileContent);
6566
// load override text
6667
JsonConfiguration override = new JsonConfiguration();

src/main/java/org/maxgamer/quickshop/util/language/text/distributions/crowdin/CrowdinOTA.java

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import com.google.common.cache.Cache;
44
import com.google.common.cache.CacheBuilder;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonParser;
7+
import com.google.gson.JsonPrimitive;
58
import org.apache.commons.codec.digest.DigestUtils;
9+
import org.apache.commons.lang.StringUtils;
610
import org.bukkit.configuration.file.YamlConfiguration;
711
import org.jetbrains.annotations.NotNull;
812
import org.jetbrains.annotations.Nullable;
@@ -16,13 +20,13 @@
1620
import java.io.File;
1721
import java.io.IOException;
1822
import java.net.URL;
19-
import java.util.Collections;
20-
import java.util.List;
23+
import java.nio.charset.StandardCharsets;
24+
import java.util.*;
2125
import java.util.concurrent.TimeUnit;
2226

2327
public class CrowdinOTA implements Distribution {
2428
protected static final String CROWDIN_OTA_HOST = "https://distributions.crowdin.net/daf1a8db40f132ce157c457xrm4/";
25-
protected final Cache<String, String> requestCachePool = CacheBuilder.newBuilder()
29+
protected final Cache<String, byte[]> requestCachePool = CacheBuilder.newBuilder()
2630
.expireAfterWrite(7, TimeUnit.DAYS)
2731
.recordStats()
2832
.build();
@@ -33,40 +37,73 @@ public CrowdinOTA(QuickShop plugin) {
3337
Util.getCacheFolder().mkdirs();
3438
}
3539

36-
private @Nullable String requestWithCache(String url) {
37-
String data = requestCachePool.getIfPresent(url);
40+
private byte[] requestWithCache(@NotNull String url, @Nullable File saveTo) {
41+
byte[] data = requestCachePool.getIfPresent(url);
3842
if (data == null) {
3943
try {
40-
data = HttpRequest.get(new URL(url))
44+
HttpRequest.BufferedResponse response = HttpRequest.get(new URL(url))
4145
.execute()
4246
.expectResponseCode(200)
43-
.returnContent()
44-
.asString("UTF-8");
47+
.returnContent();
48+
if (saveTo != null)
49+
response.saveContent(saveTo);
50+
return response.asBytes();
4551
} catch (IOException e) {
4652
e.printStackTrace();
4753
return null;
54+
} catch (InterruptedException e) {
55+
e.printStackTrace();
4856
}
49-
if (data != null)
50-
requestCachePool.put(url, data);
57+
return null;
5158
}
5259
return data;
5360
}
5461

5562
@Nullable
5663
public Manifest getManifest() {
5764
String url = CROWDIN_OTA_HOST + "manifest.json";
58-
String data = requestWithCache(url);
59-
if (data == null)
65+
String data = new String(requestWithCache(url,null), StandardCharsets.UTF_8);
66+
if (StringUtils.isEmpty(data))
6067
return null;
6168
return JsonUtil.getGson().fromJson(data, Manifest.class);
6269
}
6370

71+
@Nullable
72+
public String getManifestJson() {
73+
String url = CROWDIN_OTA_HOST + "manifest.json";
74+
String data = new String(requestWithCache(url,null), StandardCharsets.UTF_8);
75+
if (StringUtils.isEmpty(data))
76+
return null;
77+
return data;
78+
}
79+
80+
public Map<String, String> genLanguageMapping() {
81+
if (getManifestJson() == null)
82+
return new HashMap<>();
83+
Map<String, String> mapping = new HashMap<>();
84+
JsonElement parser = new JsonParser().parse(getManifestJson());
85+
for (Map.Entry<String, JsonElement> set : parser.getAsJsonObject().getAsJsonObject("language_mapping").entrySet()) {
86+
if (!set.getValue().isJsonObject())
87+
continue;
88+
JsonPrimitive object = set.getValue().getAsJsonObject().getAsJsonPrimitive("locale");
89+
if (object == null)
90+
continue;
91+
mapping.put(set.getKey(), object.getAsString());
92+
}
93+
return mapping;
94+
}
95+
6496
@NotNull
6597
public List<String> getAvailableLanguages() {
6698
Manifest manifest = getManifest();
6799
if (manifest == null)
68100
return Collections.emptyList();
69-
return manifest.getLanguages();
101+
List<String> languages = new ArrayList<>();
102+
Map<String, String> mapping = genLanguageMapping();
103+
for (String language : manifest.getLanguages()) {
104+
languages.add(mapping.getOrDefault(language, language));
105+
}
106+
return languages;
70107
}
71108

72109
@NotNull
@@ -79,11 +116,11 @@ public List<String> getAvailableFiles() {
79116

80117
@Override
81118
public @NotNull String getFile(String fileCrowdinPath, String crowdinLocale) throws Exception {
82-
return getFile(fileCrowdinPath,crowdinLocale,false);
119+
return getFile(fileCrowdinPath, crowdinLocale, false);
83120
}
84121

85122
@NotNull
86-
public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forceFlush)throws Exception {
123+
public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forceFlush) throws Exception {
87124
Manifest manifest = getManifest();
88125
if (manifest == null)
89126
throw new IllegalStateException("Failed to get project manifest");
@@ -98,18 +135,20 @@ public String getFile(String fileCrowdinPath, String crowdinLocale, boolean forc
98135
long localeTimestamp = cacheMetadata.getLong(pathHash + ".timestamp");
99136
File cachedDataFile = new File(Util.getCacheFolder(), pathHash);
100137
String data = null;
101-
if(cachedDataFile.exists()){
138+
if (cachedDataFile.exists()) {
102139
data = Util.readToString(cachedDataFile);
103140
}
104141
// invalidate cache, flush it
105-
if (forceFlush || data == null ||localeTimestamp != manifest.getTimestamp()) {
106-
String url = CROWDIN_OTA_HOST + "content/" + fileCrowdinPath.replace("%locale%", postProcessingPath);
107-
data = requestWithCache(url);
108-
if (data == null)
109-
throw new IOException("Couldn't download translation from remote server");
142+
if (forceFlush || data == null || localeTimestamp != manifest.getTimestamp()) {
143+
String url = CROWDIN_OTA_HOST + "content" + fileCrowdinPath.replace("%locale%", crowdinLocale);
144+
byte[] bin = requestWithCache(url,cachedDataFile);
145+
if (bin == null)
146+
throw new IOException("Couldn't download translation from remote server. If you see any error like \"404 Not Found\", please report it to QuickShop.");
110147
// update cache index
148+
data = Util.readToString(cachedDataFile);
111149
cacheMetadata.set(pathHash + ".timestamp", manifest.getTimestamp());
112150
cacheMetadata.save(metadataFile);
151+
return new String(bin, StandardCharsets.UTF_8);
113152
}
114153
// if (data == null) {
115154
// cacheMetadata.set(pathHash, null);

src/main/java/org/maxgamer/quickshop/util/language/text/distributions/crowdin/bean/Manifest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.List;
88

9+
910
@NoArgsConstructor
1011
@Data
1112
public class Manifest {
@@ -14,10 +15,9 @@ public class Manifest {
1415
private List<String> files;
1516
@JsonProperty("languages")
1617
private List<String> languages;
17-
@JsonProperty("language_mapping")
18-
private List<?> languageMapping;
1918
@JsonProperty("custom_languages")
2019
private List<?> customLanguages;
2120
@JsonProperty("timestamp")
2221
private Integer timestamp;
22+
2323
}

0 commit comments

Comments
 (0)