Use Unicode characters when cleaning up attachment filename
Improve the function ``cleanup_filename()`` of ``lib/mu-msg-part.c`` to use Unicode characters when replacing the control characters, slashes and colons with ``-``. Originally, this function just use plain C characters (i.e., assuming ASCII string) when checking each character is or not a control character, slash or colon. However, when the attachment filename contains non-ASCII (e.g., Chinese characters), all the non-ASCII characters are replaced with ``-``. For example: * Before: ``` > mu view test_chinese_attachment_filename.eml From: Tester <tester@example.com> To: Example <example@example.com> Subject: Test email with attachment of Chinese filename Date: Mon 23 May 2016 05:22:09 PM CST Attachments: 'attachment-test.txt', '------------.txt', '-------test.txt' Hello, This is a simple test email with three attachments: 1. `attachment:test.txt`: filename is all English; 2. `测试附件.txt`: filename is all Chinese (exclude the extension); 3. `附件-test.txt`: filename mixes Chinese and English. ``` * After: ``` > ./build/mu/mu/mu view test_chinese_attachment_filename.eml From: Tester <tester@example.com> To: Example <example@example.com> Subject: Test email with attachment of Chinese filename Date: Mon 23 May 2016 05:22:09 PM CST Attachments: 'attachment-test.txt', '测试附件.txt', '附件-test.txt' Hello, This is a simple test email with three attachments: 1. `attachment:test.txt`: filename is all English; 2. `测试附件.txt`: filename is all Chinese (exclude the extension); 3. `附件-test.txt`: filename mixes Chinese and English. ```
This commit is contained in:
@ -239,15 +239,26 @@ get_part_size (GMimePart *part)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
static char*
|
||||||
cleanup_filename (char *fname)
|
cleanup_filename (char *fname)
|
||||||
{
|
{
|
||||||
|
GString *gstr;
|
||||||
gchar *cur;
|
gchar *cur;
|
||||||
|
gunichar uc;
|
||||||
|
|
||||||
/* replace control characters, slashes,colons by a '-' */
|
gstr = g_string_sized_new (strlen (fname));
|
||||||
for (cur = fname; *cur; ++cur)
|
|
||||||
if (*cur < ' ' || *cur == '/' || *cur == ':')
|
/* replace control characters, slashes, and colons by '-' */
|
||||||
*cur = '-';
|
for (cur = fname; cur && *cur; cur = g_utf8_next_char (cur)) {
|
||||||
|
uc = g_utf8_get_char (cur);
|
||||||
|
if (g_unichar_iscntrl (uc) || uc == '/' || uc == ':')
|
||||||
|
g_string_append_unichar (gstr, '-');
|
||||||
|
else
|
||||||
|
g_string_append_unichar (gstr, uc);
|
||||||
|
}
|
||||||
|
|
||||||
|
g_free (fname);
|
||||||
|
return g_string_free (gstr, FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -304,9 +315,9 @@ mime_part_get_filename (GMimeObject *mobj, unsigned index,
|
|||||||
|
|
||||||
if (!fname)
|
if (!fname)
|
||||||
fname = guess_file_name (mobj, index);
|
fname = guess_file_name (mobj, index);
|
||||||
|
|
||||||
/* remove slashes, spaces, colons... */
|
/* replace control characters, slashes, and colons */
|
||||||
cleanup_filename (fname);
|
fname = cleanup_filename (fname);
|
||||||
|
|
||||||
return fname;
|
return fname;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user