[geary/wip/attachment-filename-714549: 2/2] Use regex to do replacement



commit d90505cbd7236b42a79d7b1aa5533386a1894424
Author: Charles Lindsay <chaz yorba org>
Date:   Wed Mar 5 15:03:11 2014 -0800

    Use regex to do replacement

 src/engine/rfc822/rfc822-utils.vala |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
---
diff --git a/src/engine/rfc822/rfc822-utils.vala b/src/engine/rfc822/rfc822-utils.vala
index ad96433..7b2ed4f 100644
--- a/src/engine/rfc822/rfc822-utils.vala
+++ b/src/engine/rfc822/rfc822-utils.vala
@@ -11,6 +11,9 @@ namespace Geary.RFC822.Utils {
 // in UTF-8, and is unmolested by GMime.FilterHTML.
 public const char QUOTE_MARKER = '\x7f';
 
+private Regex? invalid_filename_character_re = null;
+
+
 public GMime.FilterCharset create_utf8_filter_charset(string from_charset) {
     GMime.FilterCharset? filter_charset = new GMime.FilterCharset(from_charset, "UTF-8");
     if (filter_charset == null) {
@@ -326,7 +329,20 @@ public string get_clean_attachment_filename(GMime.Part part) {
         /// Placeholder filename for attachments with no filename.
         filename = _("none");
     }
-    filename = filename.replace("/", "_").replace("\0", "_");
+    if (invalid_filename_character_re == null) {
+        try {
+            invalid_filename_character_re = new Regex("[/\\0]");
+        } catch (RegexError e) {
+            assert_not_reached();
+        }
+    }
+    
+    try {
+        filename = invalid_filename_character_re.replace_literal(filename, filename.length, 0, "_");
+    } catch (RegexError e) {
+        debug("Error sanitizing attachment filename: %s", e.message);
+    }
+    
     return filename;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]