70 lines
2.0 KiB
Java
70 lines
2.0 KiB
Java
package eu.univento.commons.updater;
|
|
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParser;
|
|
import eu.univento.commons.utils.Strings;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.net.URL;
|
|
|
|
/**
|
|
* @author joethei
|
|
* @version 0.1
|
|
*/
|
|
public class Updater {
|
|
|
|
private String check;
|
|
private String download;
|
|
private String name;
|
|
private String version;
|
|
|
|
public Updater(String check, String name, String version) {
|
|
this.check = check;
|
|
this.name = name;
|
|
this.version = version;
|
|
}
|
|
|
|
public boolean needsUpdate() {
|
|
JsonObject object;
|
|
try {
|
|
object = new JsonParser().parse(IOUtils.toString(new URL(check).openStream(), "UTF-8")).getAsJsonObject();
|
|
JsonArray array = object.get("versions").getAsJsonArray();
|
|
for (JsonElement element : array.getAsJsonArray()) {
|
|
JsonObject obj = element.getAsJsonObject();
|
|
if (obj.get("version").getAsString() != null) {
|
|
download = obj.get("link").getAsString();
|
|
return Strings.versionCompare(obj.get("version").getAsString(), version) > 0;
|
|
}
|
|
}
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean delete() {
|
|
try {
|
|
FileUtils.forceDelete(new File("/plugins", name + ".jar"));
|
|
return true;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public boolean download() {
|
|
try {
|
|
FileUtils.copyURLToFile(new URL(download), new File("/plugins", name + ".jar"));
|
|
return true;
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
}
|
|
} |