Changeset 1488cd0


Ignore:
Timestamp:
Dec 17, 2011 1:52:32 PM (8 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
bf45e31
Parents:
5b05d86
Message:
  • i2psnark:
    • Replace file name characters not supported in default charset
    • Add torrent file name to local details page
Location:
apps/i2psnark/java/src/org/klomp/snark
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/Storage.java

    r5b05d86 r1488cd0  
    2424import java.io.IOException;
    2525import java.io.RandomAccessFile;
     26import java.nio.charset.Charset;
     27import java.nio.charset.CharsetEncoder;
    2628import java.security.MessageDigest;
    2729import java.util.ArrayList;
    2830import java.util.Iterator;
    2931import java.util.List;
     32import java.util.Map;
    3033import java.util.StringTokenizer;
     34import java.util.concurrent.ConcurrentHashMap;
    3135
    3236import net.i2p.crypto.SHA1;
     
    6771  public static final int MAX_PIECES = 10*1024;
    6872  public static final long MAX_TOTAL_SIZE = MAX_PIECE_SIZE * (long) MAX_PIECES;
     73
     74  private static final Map<String, String> _filterNameCache = new ConcurrentHashMap();
    6975
    7076  /**
     
    569575   * Removes 'suspicious' characters from the given file name.
    570576   * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
     577   * Then replace chars not supported in the charset.
     578   *
     579   * This is called frequently and it can be pretty slow so cache the result.
     580   *
     581   * TODO: If multiple files in the same torrent map to the same filter name,
     582   * the whole torrent will blow up. Check at torrent creation?
    571583   */
    572584  public static String filterName(String name)
    573585  {
    574     if (name.equals(".") || name.equals(" "))
    575         return "_";
    576     String rv = name;
    577     if (rv.startsWith("."))
    578         rv = '_' + rv.substring(1);
    579     if (rv.endsWith(".") || rv.endsWith(" "))
    580         rv = rv.substring(0, rv.length() - 1) + '_';
    581     for (int i = 0; i < ILLEGAL.length; i++) {
    582         if (rv.indexOf(ILLEGAL[i]) >= 0)
    583             rv = rv.replace(ILLEGAL[i], '_');
    584     }
     586    String rv = _filterNameCache.get(name);
     587    if (rv != null)
     588        return rv;
     589    if (name.equals(".") || name.equals(" ")) {
     590        rv = "_";
     591    } else {
     592        rv = name;
     593        if (rv.startsWith("."))
     594            rv = '_' + rv.substring(1);
     595        if (rv.endsWith(".") || rv.endsWith(" "))
     596            rv = rv.substring(0, rv.length() - 1) + '_';
     597        for (int i = 0; i < ILLEGAL.length; i++) {
     598            if (rv.indexOf(ILLEGAL[i]) >= 0)
     599                rv = rv.replace(ILLEGAL[i], '_');
     600        }
     601        // Replace characters not supported in the charset
     602        if (!Charset.defaultCharset().name().equals("UTF-8")) {
     603            try {
     604                CharsetEncoder enc = Charset.defaultCharset().newEncoder();
     605                if (!enc.canEncode(rv)) {
     606                    String repl = rv;
     607                    for (int i = 0; i < rv.length(); i++) {
     608                        char c = rv.charAt(i);
     609                        if (!enc.canEncode(c))
     610                            repl = repl.replace(c, '_');
     611                    }
     612                    rv = repl;
     613                }
     614            } catch (Exception ex) {
     615                ex.printStackTrace();
     616            }
     617    }   }
     618    _filterNameCache.put(name, rv);
    585619    return rv;
    586620  }
  • apps/i2psnark/java/src/org/klomp/snark/web/I2PSnarkServlet.java

    r5b05d86 r1488cd0  
    17601760            //buf.append("<br>").append(_("Maggot link")).append(": <a href=\"").append(MAGGOT).append(hex).append(':').append(hex).append("\">")
    17611761            //   .append(MAGGOT).append(hex).append(':').append(hex).append("</a>");
     1762            buf.append("<br>").append(_("Torrent file")).append(": ").append(snark.getName());
    17621763            buf.append("</div></th></tr>");
    17631764        }
Note: See TracChangeset for help on using the changeset viewer.