From c0a210956dfc6f43950474c32905222a9e9bdb34 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Fri, 12 Feb 2021 09:34:48 +0200 Subject: [PATCH] Refactored file deletion checks for JSONFileStorage --- .../webserver/cache/JSONFileStorage.java | 62 ++++++++++++------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONFileStorage.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONFileStorage.java index 6ea3f9816..f3807b71a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONFileStorage.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/cache/JSONFileStorage.java @@ -198,47 +198,61 @@ public class JSONFileStorage implements JSONStorage { List toDelete = new ArrayList<>(); for (File file : stored) { - try { - String fileName = file.getName(); - if (fileName.endsWith(JSON_FILE_EXTENSION) && fileName.startsWith(identifier)) { - Matcher timestampMatch = timestampRegex.matcher(fileName); - if (timestampMatch.find() && Long.parseLong(timestampMatch.group(1)) < timestamp) { - toDelete.add(file); - } - } - } catch (NumberFormatException e) { - // Ignore this file, malformed timestamp + if (shouldDeleteFile(identifier, timestamp, toDelete, file)) { + toDelete.add(file); } } deleteFiles(toDelete); } + private boolean shouldDeleteFile(String identifier, long timestamp, List toDelete, File file) { + try { + String fileName = file.getName(); + if (fileName.endsWith(JSON_FILE_EXTENSION) && fileName.startsWith(identifier)) { + Matcher timestampMatch = timestampRegex.matcher(fileName); + if (timestampMatch.find() && Long.parseLong(timestampMatch.group(1)) < timestamp) { + return true; + } + } + } catch (NumberFormatException e) { + // Ignore this file, malformed timestamp + } + return false; + } + private void invalidateOlderButIgnore(long timestamp, String... ignoredIdentifiers) { File[] stored = jsonDirectory.toFile().listFiles(); if (stored == null) return; List toDelete = new ArrayList<>(); - outer: for (File file : stored) { - try { - String fileName = file.getName(); - if (fileName.endsWith(JSON_FILE_EXTENSION)) { - Matcher timestampMatch = timestampRegex.matcher(fileName); - if (timestampMatch.find() && Long.parseLong(timestampMatch.group(1)) < timestamp) { - for (String ignoredIdentifier : ignoredIdentifiers) { - if (fileName.startsWith(ignoredIdentifier)) continue outer; - } - toDelete.add(file); - } - } - } catch (NumberFormatException e) { - // Ignore this file, malformed timestamp + if (shouldDeleteFile(timestamp, file, ignoredIdentifiers)) { + toDelete.add(file); } } deleteFiles(toDelete); } + private boolean shouldDeleteFile(long timestamp, File file, String[] ignoredIdentifiers) { + try { + String fileName = file.getName(); + if (fileName.endsWith(JSON_FILE_EXTENSION)) { + Matcher timestampMatch = timestampRegex.matcher(fileName); + boolean isOlder = timestampMatch.find() && Long.parseLong(timestampMatch.group(1)) < timestamp; + if (isOlder) { + for (String ignoredIdentifier : ignoredIdentifiers) { + if (fileName.startsWith(ignoredIdentifier)) return false; + } + return true; + } + } + } catch (NumberFormatException e) { + // Ignore this file, malformed timestamp + } + return false; + } + private void deleteFiles(List toDelete) { for (File fileToDelete : toDelete) { try {