[BUGFIX] Remade validateCache

Closes issue #167.
This commit is contained in:
Emma Stone
2017-02-03 17:34:28 +00:00
parent f9c612dbcc
commit 5a2e12eb54
+7 -17
View File
@@ -20,6 +20,7 @@ import java.net.URISyntaxException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Date;
/** /**
* The core of parabot * The core of parabot
@@ -206,31 +207,20 @@ public class Core {
} }
/** /**
* Validates the cache and removes the cache contents if required * Method that removes the cache contents after 3 days
*/ */
private static void validateCache() { private static void validateCache() {
File[] cache = Directories.getCachePath().listFiles(); File[] cache = Directories.getCachePath().listFiles();
Integer lowest = null;
if (cache != null) { if (cache != null) {
for (File f : cache) { for (File f : cache) {
int date = (int) (f.lastModified() / 1000); long age = new Date().getTime() - f.lastModified();
if (lowest == null || date < lowest) {
lowest = date; if (age > 3 * 24 * 60 * 60 * 1000) {
f.delete();
} }
} }
} }
try {
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(WebUtil.getContents("http://bdn.parabot.org/api/v2/bot/cache", "date=" + lowest));
if ((boolean) object.get("result")) {
Core.verbose("Making space for the latest cache files");
Directories.clearCache();
} else {
Core.verbose("Cache is up to date");
}
} catch (MalformedURLException | ParseException e) {
e.printStackTrace();
}
} }
public static void downloadNewVersion() { public static void downloadNewVersion() {