Ticket #2225: Shorten Filenames.patch

File Shorten Filenames.patch, 1.6 KB (added by xeen, 17 years ago)

Use MD5 instead and drastically shorten paths as fallback

  • wmsplugin/src/wmsplugin/Cache.java

     
    33import java.awt.image.BufferedImage;
    44import java.io.File;
    55import java.io.FileFilter;
     6import java.math.BigInteger;
     7import java.security.MessageDigest;
    68import java.util.Date;
    79import java.util.Iterator;
    810import java.util.Set;
    911import java.util.TreeMap;
    1012import javax.imageio.*;
    1113
     14
    1215import org.openstreetmap.josm.Main;
    1316
    1417public class Cache {
     
    126129        );
    127130    }
    128131
    129     private String clean(String ident) {
    130         return ident.replaceAll("[^a-zA-Z0-9]", "");
     132    private static String getUniqueFilename(String ident) {
     133        try {
     134            MessageDigest md = MessageDigest.getInstance("MD5");
     135            BigInteger number = new BigInteger(1, md.digest(ident.getBytes()));
     136            return number.toString(16);
     137        } catch(Exception e) {
     138            // Fall back. Remove unsuitable characters and some random ones to shrink down path length.
     139            // Limit it to 70 characters, that leaves about 190 for the path on Windows/NTFS
     140            ident = ident.replaceAll("[^a-zA-Z0-9]", "");
     141            ident = ident.replaceAll("[acegikmoqsuwy]", "");
     142            return ident.substring(ident.length() - 70);
     143        }
    131144    }
    132145
    133146    private File getPath(String ident) {
    134         return new File(dir, clean(ident) + ".png");
     147        return new File(dir, getUniqueFilename(ident) + ".png");
    135148    }
    136149}
     150 No newline at end of file