Changeset 13731 in josm
- Timestamp:
- 2018-05-12T14:16:32+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/CachedFile.java
r13728 r13731 10 10 import java.io.IOException; 11 11 import java.io.InputStream; 12 import java.math.BigInteger; 12 13 import java.net.HttpURLConnection; 13 14 import java.net.MalformedURLException; … … 16 17 import java.nio.file.Files; 17 18 import java.nio.file.StandardCopyOption; 19 import java.security.MessageDigest; 20 import java.security.NoSuchAlgorithmException; 18 21 import java.util.ArrayList; 19 22 import java.util.Arrays; … … 439 442 urlStr = urlStr.replaceAll("%<(.*)>", ""); 440 443 long age = 0L; 441 long maxAgeMillis = maxAge;444 long maxAgeMillis = TimeUnit.SECONDS.toMillis(maxAge); 442 445 Long ifModifiedSince = null; 443 446 File localFile = null; … … 497 500 String a = urlStr.replaceAll("[^A-Za-z0-9_.-]", "_"); 498 501 String localPath = "mirror_" + a; 502 localPath = truncatePath(destDir, localPath); 499 503 destDirFile = new File(destDir, localPath + ".tmp"); 500 504 try { … … 546 550 } 547 551 552 private static String truncatePath(String directory, String fileName) { 553 String ret = fileName; 554 if (directory.length() + fileName.length() > 255) { 555 // Windows doesn't support paths longer than 260, leave 5 chars as safe buffer, 4 will be used by ".tmp" 556 // TODO: what about filename size on other systems? 255? 557 if (directory.length() > 191 && Main.isPlatformWindows()) { 558 // digest length + name prefix == 64 559 // 255 - 64 = 191 560 // TODO: use this check only on Windows? 561 throw new IllegalArgumentException("Path " + directory + " too long to cached files"); 562 } 563 564 MessageDigest md; 565 try { 566 md = MessageDigest.getInstance("SHA-256"); 567 md.update(fileName.getBytes(StandardCharsets.UTF_8)); 568 String digest = String.format("%064x", new BigInteger(1, md.digest())); 569 return fileName.substring(0, Math.min(fileName.length(), 32)) + digest.substring(0, 32); 570 } catch (NoSuchAlgorithmException e) { 571 Logging.error(e); 572 // TODO: what better can we do here? 573 throw new IllegalArgumentException("Missing digest algorithm SHA-256", e); 574 } 575 } 576 return fileName; 577 } 578 548 579 /** 549 580 * Attempts to disconnect an URL connection.
Note:
See TracChangeset
for help on using the changeset viewer.