1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-02 08:35:08 -04:00

Replace invalid characters in file name instead of removing them

This commit is contained in:
cketti 2012-01-27 03:21:20 +01:00
parent 68f5f009f1
commit 6c23e204ba

View File

@ -39,6 +39,18 @@ import com.fsck.k9.mail.store.LocalStore.LocalAttachmentBodyPart;
import com.fsck.k9.provider.AttachmentProvider; import com.fsck.k9.provider.AttachmentProvider;
public class AttachmentView extends FrameLayout { public class AttachmentView extends FrameLayout {
/**
* Regular expression that represents characters that aren't allowed
* to be used in file names saved using K-9
*/
private static final String INVALID_CHARACTERS = "[^\\d\\s\\w!" +
"#\\$%&'\\(\\)\\-@\\^_`\\{\\}~.,]+";
/**
* Invalid characters in a file name are replaced by this character.
*/
private static final String REPLACEMENT_CHARACTER = "_";
private Context mContext; private Context mContext;
public Button viewButton; public Button viewButton;
@ -53,13 +65,6 @@ public class AttachmentView extends FrameLayout {
public long size; public long size;
public ImageView iconView; public ImageView iconView;
/**
* Regular expression that represents characters that aren't allowed
* to be used in file names saved using K-9
*/
private static final String specialCharacters = new String("[^\\d\\s\\w!" +
"#\\$%&'\\(\\)\\-@\\^_`\\{\\}~.,]");
private AttachmentFileDownloadCallback callback; private AttachmentFileDownloadCallback callback;
public AttachmentView(Context context, AttributeSet attrs, int defStyle) { public AttachmentView(Context context, AttributeSet attrs, int defStyle) {
@ -203,7 +208,7 @@ public class AttachmentView extends FrameLayout {
*/ */
public void writeFile(File directory) { public void writeFile(File directory) {
try { try {
String filename = removeSpecialCharacters(name); String filename = sanitizeFilename(name);
File file = Utility.createUniqueFile(directory, filename); File file = Utility.createUniqueFile(directory, filename);
Uri uri = AttachmentProvider.getAttachmentUri(mAccount, part.getAttachmentId()); Uri uri = AttachmentProvider.getAttachmentUri(mAccount, part.getAttachmentId());
InputStream in = mContext.getContentResolver().openInputStream(uri); InputStream in = mContext.getContentResolver().openInputStream(uri);
@ -220,14 +225,15 @@ public class AttachmentView extends FrameLayout {
} }
/** /**
* Removes characters that aren't allowed to be used in file names saved * Replace characters we don't allow in file names with a replacement character.
* in K-9 application.
* *
* @param filename The original file name. * @param filename
* @return A file name with only legal characters. * The original file name.
*
* @return The sanitized file name containing only allowed characters.
*/ */
private String removeSpecialCharacters(String filename) { private String sanitizeFilename(String filename) {
return filename.replaceAll(specialCharacters, ""); return filename.replaceAll(INVALID_CHARACTERS, REPLACEMENT_CHARACTER);
} }
/** /**