|
| 1 | +package cn.wildfirechat.app; |
| 2 | + |
| 3 | +import cn.wildfirechat.app.pojo.VersionCheckResponse; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.scheduling.annotation.Scheduled; |
| 8 | +import org.springframework.stereotype.Service; |
| 9 | + |
| 10 | +import javax.annotation.PostConstruct; |
| 11 | +import java.io.File; |
| 12 | +import java.io.FileInputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStreamReader; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.util.Properties; |
| 17 | +import java.util.concurrent.ConcurrentHashMap; |
| 18 | + |
| 19 | +@Service |
| 20 | +public class VersionConfigService { |
| 21 | + private static final Logger LOG = LoggerFactory.getLogger(VersionConfigService.class); |
| 22 | + |
| 23 | + @Value("${version.config.path:config/version.properties}") |
| 24 | + private String configPath; |
| 25 | + |
| 26 | + private final ConcurrentHashMap<String, VersionCheckResponse> versionMap = new ConcurrentHashMap<>(); |
| 27 | + private long lastModified = 0; |
| 28 | + |
| 29 | + @PostConstruct |
| 30 | + public void init() { |
| 31 | + loadConfig(); |
| 32 | + } |
| 33 | + |
| 34 | + @Scheduled(fixedRate = 30 * 1000) |
| 35 | + public void reloadIfModified() { |
| 36 | + File file = new File(configPath); |
| 37 | + if (!file.exists()) { |
| 38 | + LOG.warn("Version config file not found: {}", configPath); |
| 39 | + return; |
| 40 | + } |
| 41 | + if (file.lastModified() > lastModified) { |
| 42 | + LOG.info("Version config file modified, reloading..."); |
| 43 | + loadConfig(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private synchronized void loadConfig() { |
| 48 | + File file = new File(configPath); |
| 49 | + if (!file.exists()) { |
| 50 | + LOG.warn("Version config file not found: {}", configPath); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + Properties props = new Properties(); |
| 55 | + try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) { |
| 56 | + props.load(reader); |
| 57 | + lastModified = file.lastModified(); |
| 58 | + } catch (IOException e) { |
| 59 | + LOG.error("Failed to load version config", e); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // Android |
| 64 | + versionMap.put("android", buildVersionInfo(props, "android")); |
| 65 | + // iOS |
| 66 | + versionMap.put("ios", buildVersionInfo(props, "ios")); |
| 67 | + // Harmony |
| 68 | + versionMap.put("harmony", buildVersionInfo(props, "harmony")); |
| 69 | + |
| 70 | + LOG.info("Version config loaded successfully"); |
| 71 | + } |
| 72 | + |
| 73 | + private VersionCheckResponse buildVersionInfo(Properties props, String platform) { |
| 74 | + VersionCheckResponse info = new VersionCheckResponse(); |
| 75 | + info.setLatestVersion(getProp(props, "version." + platform + ".latestVersion", "")); |
| 76 | + info.setBuildNumber(parseInt(getProp(props, "version." + platform + ".buildNumber", "0"))); |
| 77 | + info.setMinVersion(getProp(props, "version." + platform + ".minVersion", "")); |
| 78 | + info.setForceUpdate(Boolean.parseBoolean(getProp(props, "version." + platform + ".forceUpdate", "false"))); |
| 79 | + info.setTitle(getProp(props, "version." + platform + ".title", "")); |
| 80 | + info.setMessage(getProp(props, "version." + platform + ".message", "")); |
| 81 | + info.setDownloadUrl(getProp(props, "version." + platform + ".downloadUrl", "")); |
| 82 | + info.setRedirectUrl(getProp(props, "version." + platform + ".redirectUrl", "")); |
| 83 | + info.setHarmonyUrl(getProp(props, "version." + platform + ".harmonyUrl", "")); |
| 84 | + return info; |
| 85 | + } |
| 86 | + |
| 87 | + private String getProp(Properties props, String key, String defaultValue) { |
| 88 | + String value = props.getProperty(key); |
| 89 | + return value != null ? value : defaultValue; |
| 90 | + } |
| 91 | + |
| 92 | + private int parseInt(String value) { |
| 93 | + try { |
| 94 | + return Integer.parseInt(value); |
| 95 | + } catch (NumberFormatException e) { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public VersionCheckResponse getVersionInfo(String platform) { |
| 101 | + return versionMap.get(platform); |
| 102 | + } |
| 103 | +} |
0 commit comments