* i2psnark:

- Replace file name characters not supported in default charset
    - Add torrent file name to local details page
This commit is contained in:
zzz
2011-12-17 13:52:32 +00:00
parent 5b05d86ec6
commit 1488cd0f48
2 changed files with 46 additions and 11 deletions

View File

@ -23,11 +23,15 @@ package org.klomp.snark;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import net.i2p.crypto.SHA1; import net.i2p.crypto.SHA1;
import net.i2p.util.SecureFile; import net.i2p.util.SecureFile;
@ -67,6 +71,8 @@ public class Storage
public static final int MAX_PIECES = 10*1024; public static final int MAX_PIECES = 10*1024;
public static final long MAX_TOTAL_SIZE = MAX_PIECE_SIZE * (long) MAX_PIECES; public static final long MAX_TOTAL_SIZE = MAX_PIECE_SIZE * (long) MAX_PIECES;
private static final Map<String, String> _filterNameCache = new ConcurrentHashMap();
/** /**
* Creates a new storage based on the supplied MetaInfo. This will * Creates a new storage based on the supplied MetaInfo. This will
* try to create and/or check all needed files in the MetaInfo. * try to create and/or check all needed files in the MetaInfo.
@ -568,20 +574,48 @@ public class Storage
/** /**
* Removes 'suspicious' characters from the given file name. * Removes 'suspicious' characters from the given file name.
* http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx * http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
* Then replace chars not supported in the charset.
*
* This is called frequently and it can be pretty slow so cache the result.
*
* TODO: If multiple files in the same torrent map to the same filter name,
* the whole torrent will blow up. Check at torrent creation?
*/ */
public static String filterName(String name) public static String filterName(String name)
{ {
if (name.equals(".") || name.equals(" ")) String rv = _filterNameCache.get(name);
return "_"; if (rv != null)
String rv = name; return rv;
if (rv.startsWith(".")) if (name.equals(".") || name.equals(" ")) {
rv = '_' + rv.substring(1); rv = "_";
if (rv.endsWith(".") || rv.endsWith(" ")) } else {
rv = rv.substring(0, rv.length() - 1) + '_'; rv = name;
for (int i = 0; i < ILLEGAL.length; i++) { if (rv.startsWith("."))
if (rv.indexOf(ILLEGAL[i]) >= 0) rv = '_' + rv.substring(1);
rv = rv.replace(ILLEGAL[i], '_'); if (rv.endsWith(".") || rv.endsWith(" "))
} rv = rv.substring(0, rv.length() - 1) + '_';
for (int i = 0; i < ILLEGAL.length; i++) {
if (rv.indexOf(ILLEGAL[i]) >= 0)
rv = rv.replace(ILLEGAL[i], '_');
}
// Replace characters not supported in the charset
if (!Charset.defaultCharset().name().equals("UTF-8")) {
try {
CharsetEncoder enc = Charset.defaultCharset().newEncoder();
if (!enc.canEncode(rv)) {
String repl = rv;
for (int i = 0; i < rv.length(); i++) {
char c = rv.charAt(i);
if (!enc.canEncode(c))
repl = repl.replace(c, '_');
}
rv = repl;
}
} catch (Exception ex) {
ex.printStackTrace();
}
} }
_filterNameCache.put(name, rv);
return rv; return rv;
} }

View File

@ -1759,6 +1759,7 @@ public class I2PSnarkServlet extends Default {
// We don't have the hash of the torrent file // We don't have the hash of the torrent file
//buf.append("<br>").append(_("Maggot link")).append(": <a href=\"").append(MAGGOT).append(hex).append(':').append(hex).append("\">") //buf.append("<br>").append(_("Maggot link")).append(": <a href=\"").append(MAGGOT).append(hex).append(':').append(hex).append("\">")
// .append(MAGGOT).append(hex).append(':').append(hex).append("</a>"); // .append(MAGGOT).append(hex).append(':').append(hex).append("</a>");
buf.append("<br>").append(_("Torrent file")).append(": ").append(snark.getName());
buf.append("</div></th></tr>"); buf.append("</div></th></tr>");
} }
if (ls == null) { if (ls == null) {