mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-12-24 14:05:34 +00:00
Added version checking code. Complete with unit tests. Oh yeah. :D
This commit is contained in:
parent
3e64b336a8
commit
5291b35f63
@ -21,6 +21,9 @@ dependencies {
|
||||
compile 'com.github.spullara.mustache.java:compiler:0.8.18'
|
||||
compile 'org.slf4j:slf4j-api:1.7.21'
|
||||
compile 'ch.qos.logback:logback-classic:1.1.7'
|
||||
compile 'com.google.code.gson:gson:2.5'
|
||||
|
||||
testCompile 'junit:junit:4.12'
|
||||
}
|
||||
|
||||
run {
|
||||
|
@ -17,6 +17,8 @@
|
||||
package de.fabianonline.telegram_backup;
|
||||
|
||||
import de.fabianonline.telegram_backup.CommandLineController;
|
||||
import de.fabianonline.telegram_backup.Utils;
|
||||
import de.fabianonline.telegram_backup.Version;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
@ -55,6 +57,14 @@ public class CommandLineRunner {
|
||||
((Logger)LoggerFactory.getLogger("com.github.badoualy")).setLevel(Level.TRACE);
|
||||
}
|
||||
|
||||
Version v = Utils.getNewestVersion();
|
||||
if (v!=null && v.isNewer) {
|
||||
System.out.println("A newer version is vailable!");
|
||||
System.out.println("You are using: " + Config.APP_APPVER);
|
||||
System.out.println("Available: " + v.version);
|
||||
System.out.println("Get it here: " + v.url);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
if (true || CommandLineOptions.cmd_console) {
|
||||
// Always use the console for now.
|
||||
|
@ -19,8 +19,20 @@ import com.github.badoualy.telegram.tl.exception.RpcErrorException;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
import com.google.gson.*;
|
||||
import java.net.URL;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import de.fabianonline.telegram_backup.Version;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Utils {
|
||||
public static final int VERSIONS_EQUAL = 0;
|
||||
public static final int VERSION_1_NEWER = 1;
|
||||
public static final int VERSION_2_NEWER = 2;
|
||||
|
||||
private static final Logger logger = (Logger)LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
static Vector<String> getAccounts() {
|
||||
Vector<String> accounts = new Vector<String>();
|
||||
File folder = new File(Config.FILE_BASE);
|
||||
@ -53,4 +65,83 @@ public class Utils {
|
||||
try { Thread.sleep(wait * 60 * 1000); } catch(InterruptedException e2) {}
|
||||
System.out.println("");
|
||||
}
|
||||
|
||||
static Version getNewestVersion() {
|
||||
try {
|
||||
String data_url = "https://api.github.com/repos/fabianonline/telegram_backup/releases";
|
||||
logger.debug("Requesting current release info from {}", data_url);
|
||||
String json = IOUtils.toString(new URL(data_url));
|
||||
JsonParser parser = new JsonParser();
|
||||
JsonElement root_elm = parser.parse(json);
|
||||
if (root_elm.isJsonArray()) {
|
||||
JsonArray root = root_elm.getAsJsonArray();
|
||||
JsonObject newest_version = null;
|
||||
for (JsonElement e : root) if (e.isJsonObject()) {
|
||||
JsonObject version = e.getAsJsonObject();
|
||||
if (version.getAsJsonPrimitive("prerelease").getAsBoolean() == false) {
|
||||
newest_version = version;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (newest_version == null) return null;
|
||||
String new_v = newest_version.getAsJsonPrimitive("tag_name").getAsString();
|
||||
logger.debug("Found current release version {}", new_v);
|
||||
String cur_v = Config.APP_APPVER;
|
||||
|
||||
int result = compareVersions(cur_v, new_v);
|
||||
|
||||
return new Version(new_v, newest_version.getAsJsonPrimitive("html_url").getAsString(), result == VERSION_2_NEWER);
|
||||
}
|
||||
return null;
|
||||
} catch(Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static int compareVersions(String v1, String v2) {
|
||||
logger.debug("Comparing versions {} and {}.", v1, v2);
|
||||
if (v1.equals(v2)) return VERSIONS_EQUAL;
|
||||
|
||||
String[] v1_p = v1.split("-");
|
||||
String[] v2_p = v2.split("-");
|
||||
|
||||
logger.trace("Parts to compare without suffixes: {} and {}.", v1_p[0], v2_p[0]);
|
||||
|
||||
String[] v1_p2 = v1_p[0].split("\\.");
|
||||
String[] v2_p2 = v2_p[0].split("\\.");
|
||||
|
||||
logger.trace("Length of the parts without suffixes: {} and {}.", v1_p2.length, v2_p2.length);
|
||||
|
||||
int i;
|
||||
for (i=0; i<v1_p2.length && i<v2_p2.length; i++) {
|
||||
int i_1 = Integer.parseInt(v1_p2[i]);
|
||||
int i_2 = Integer.parseInt(v2_p2[i]);
|
||||
logger.trace("Comparing parts: {} and {}.", i_1, i_2);
|
||||
if (i_1 > i_2) {
|
||||
logger.debug("v1 is newer");
|
||||
return VERSION_1_NEWER;
|
||||
} else if (i_2 > i_1) {
|
||||
logger.debug("v2 is newer");
|
||||
return VERSION_2_NEWER;
|
||||
}
|
||||
}
|
||||
logger.trace("At least one of the versions has run out of parts.");
|
||||
if (v1_p2.length > v2_p2.length) {
|
||||
logger.debug("v1 is longer, so it is newer");
|
||||
return VERSION_1_NEWER;
|
||||
} else if (v2_p2.length > v1_p2.length) {
|
||||
logger.debug("v2 is longer, so it is newer");
|
||||
return VERSION_2_NEWER;
|
||||
}
|
||||
|
||||
if (v1_p.length>1 && v2_p.length==1) {
|
||||
logger.debug("v1 has a suffix, v2 not - so v2 is newer.");
|
||||
return VERSION_2_NEWER;
|
||||
} else if (v1_p.length==1 && v2_p.length>1) {
|
||||
logger.debug("v1 has no suffix, but v2 has - so v1 is newer.");
|
||||
return VERSION_1_NEWER;
|
||||
}
|
||||
logger.debug("We couldn't find a real difference, so we're assuming the versions are equal-ish.");
|
||||
return VERSIONS_EQUAL;
|
||||
}
|
||||
}
|
||||
|
29
src/main/java/de/fabianonline/telegram_backup/Version.java
Normal file
29
src/main/java/de/fabianonline/telegram_backup/Version.java
Normal file
@ -0,0 +1,29 @@
|
||||
/* Telegram_Backup
|
||||
* Copyright (C) 2016 Fabian Schlenz
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
package de.fabianonline.telegram_backup;
|
||||
|
||||
public class Version {
|
||||
public final String version;
|
||||
public final String url;
|
||||
public final boolean isNewer;
|
||||
|
||||
public Version(String v, String u, boolean n) {
|
||||
this.version = v;
|
||||
this.url = u;
|
||||
this.isNewer = n;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
import de.fabianonline.telegram_backup.Utils;
|
||||
|
||||
public class CompareVersionsTest {
|
||||
@Test
|
||||
public void tests() {
|
||||
assertEquals(Utils.compareVersions("1.0.3", "1.0.4"), Utils.VERSION_2_NEWER);
|
||||
assertEquals(Utils.compareVersions("1.0.4", "1.0.3"), Utils.VERSION_1_NEWER);
|
||||
assertEquals(Utils.compareVersions("1.0.4", "1.0.4.1"), Utils.VERSION_2_NEWER);
|
||||
assertEquals(Utils.compareVersions("1.0.4", "1.0.4-pre.1"), Utils.VERSION_1_NEWER);
|
||||
assertEquals(Utils.compareVersions("1.0.4", "1.0.4"), Utils.VERSIONS_EQUAL);
|
||||
assertEquals(Utils.compareVersions("1.0.4-pre.2", "1.0.4-pre.1"), Utils.VERSIONS_EQUAL);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user