X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fdemoscenemusic%2Fcore%2FDataDirectory.java;h=faa169e83782d67020e6e3853fba1d4c6d632105;hb=565ad78c862c9fefcba5fb35fd07e6d2a25746a1;hp=63c2933fe616d793d78ed901ff7be560eb88a702;hpb=581ddc54bb9b3ed089e9c53741c6274803525026;p=demoscenemusic.git diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java b/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java index 63c2933..faa169e 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java @@ -47,7 +47,7 @@ public class DataDirectory { * @return The absolute path of the file */ public String getPath(String id) { - return new File(dataDirectory, id.toLowerCase().replace('-', '/')).getAbsolutePath(); + return new File(new File(dataDirectory, getDirectoryName(id)), getFileName(id)).getAbsolutePath(); } /** @@ -59,9 +59,58 @@ public class DataDirectory { * @return The file */ public File getFile(String id) { - File file = new File(dataDirectory, id.toLowerCase().replace('-', '/')); + File file = new File(dataDirectory, getDirectoryName(id)); file.mkdirs(); - return file; + return new File(file, getFileName(id)); + } + + /** + * Removes the file and all its directories, up to the + * {@link #dataDirectory}. + * + * @param id + * The ID of the file to remove + */ + public void removeFile(String id) { + File file = getFile(id); + if (file.delete()) { + File parentDirectory = file.getParentFile(); + String lastDirectory = new File(dataDirectory).getAbsolutePath(); + while (!lastDirectory.equals(parentDirectory.getAbsolutePath()) && (parentDirectory.listFiles().length == 0)) { + parentDirectory.delete(); + parentDirectory = parentDirectory.getParentFile(); + } + } + } + + // + // PRIVATE METHODS + // + + /** + * Returns the relative name of the directory that the file for the given ID + * will be stored in. + * + * @param id + * The ID of the file to store + * @return The name of the directory to store the file in + */ + private String getDirectoryName(String id) { + String realId = id.replaceAll("-", "").toLowerCase(); + return realId.substring(0, 2) + "/" + realId.substring(2, 4) + "/" + realId.substring(4, 6) + "/" + realId.substring(6, 8); + } + + /** + * Returns the name of the file for the given ID, relative to its + * {@link #getDirectoryName(String) directory}. + * + * @param id + * The ID of the file to store + * @return The name of the file to store the file in + */ + private String getFileName(String id) { + String realId = id.replaceAll("-", "").toLowerCase(); + return realId.substring(8); } }