From 06787f727a401ed8cc4c3ecd23936bf40a8b7531 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 28 Jul 2012 11:48:59 +0200 Subject: [PATCH] Improve directory name generation. --- .../demoscenemusic/core/DataDirectory.java | 36 ++++++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java b/src/main/java/net/pterodactylus/demoscenemusic/core/DataDirectory.java index 63c2933..1574f73 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,39 @@ 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)); + } + + // + // 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); } } -- 2.7.4