Skip to content

Commit

Permalink
Add SHA256 digest to cached file name
Browse files Browse the repository at this point in the history
Fixes caching failure when URL is too long (e.g. discord hosted ones).
Fixes illegal characters on Windows (namely '\') that can show up in base64.
  • Loading branch information
zFERDQFREZrzfq committed Apr 19, 2024
1 parent da657cb commit a999f37
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ allprojects {
compileAndTest 'org.apache.logging.log4j:log4j-core:2.20.0'
compileAndTest 'org.apache.commons:commons-lang3:3.12.0'
compileAndTest 'commons-io:commons-io:2.7' // NOTE: ask Forge to bump this on 1.16.5
compileAndTest 'commons-codec:commons-codec:1.16.1'
}

// Process target resources with mod info
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/me/srrapero720/watermedia/api/cache/CacheAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import java.io.*;
import java.nio.file.Files;
import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -134,6 +137,13 @@ private static boolean updateIndex() {
}

static File entry$getFile(String url) {
return new File(dir, Base64.getEncoder().encodeToString(url.getBytes()));
try
{
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return new File(dir, org.apache.commons.codec.binary.Hex.encodeHexString(digest.digest(url.getBytes(StandardCharsets.UTF_8))));
} catch (NoSuchAlgorithmException e) { LOGGER.error(IT, "Failed to initalize digest", e); }

// Fallback to old naming
return new File(dir, Base64.getEncoder().encodeToString(url.getBytes()));
}
}
}

0 comments on commit a999f37

Please sign in to comment.