diff --git a/mms-lib/src/mms_task_encode.c b/mms-lib/src/mms_task_encode.c index 058b856..448bb52 100644 --- a/mms-lib/src/mms_task_encode.c +++ b/mms-lib/src/mms_task_encode.c @@ -387,15 +387,52 @@ mms_task_encode_finalize( static char* -mms_task_encode_generate_unique_path( +mms_task_encode_generate_path( const char* dir, - const char* file) + const char* file, + const char* content_type) { + int i; + char* path; + char* tmpfile = NULL; + + /* For interoperability reasons, it's better if the file extension + * matches the content type. */ + if (content_type) { + char** parsed = mms_parse_http_content_type(content_type); + if (parsed) { + static const struct extension_map { + const char* type; + const char* ext; + } known_extensions [] = { + { "image/jpeg", ".jpg" }, + { "image/png", ".png" }, + { "image/bmp", ".bmp" }, + { "image/gif", ".gif" }, + { "text/plain", ".txt" }, + { "text/html", ".html" }, + }; + const char* type = parsed[0]; + const char* known_ext = NULL; + for (i=0; i<(int)G_N_ELEMENTS(known_extensions); i++) { + if (!strcmp(type, known_extensions[i].type)) { + known_ext = known_extensions[i].ext; + break; + } + } + if (known_ext) { + const char* ext = strrchr(file, '.'); + if (!ext || strcasecmp(ext, known_ext)) { + file = tmpfile = g_strconcat(file, known_ext, NULL); + } + } + g_strfreev(parsed); + } + } + /* Most likely the very first check would succeed */ - char* path = g_strconcat(dir, "/", file, NULL); + path = g_strconcat(dir, "/", file, NULL); if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) { - int i; - char* tmpfile = NULL; for (i=0; i<100; i++) { char* newfile = g_strconcat("_", file, NULL); g_free(tmpfile); @@ -404,8 +441,8 @@ mms_task_encode_generate_unique_path( file = tmpfile = newfile; if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) break; } - g_free(tmpfile); } + g_free(tmpfile); return path; } @@ -425,8 +462,8 @@ mms_task_encode_prepare_attachments( MMSAttachment* attachment = NULL; MMSAttachmentInfo info = parts[i]; G_GNUC_BEGIN_IGNORE_DEPRECATIONS - char* path = mms_task_encode_generate_unique_path(dir, - g_basename(info.file_name)); + char* path = mms_task_encode_generate_path(dir, + g_basename(info.file_name), info.content_type); G_GNUC_END_IGNORE_DEPRECATIONS GFile* src = g_file_new_for_path(info.file_name); GFile* dest = g_file_new_for_path(path); @@ -452,7 +489,7 @@ mms_task_encode_prepare_attachments( if (i == nparts) { /* Generate SMIL if necessary */ if (smil_index < 0) { - char* path = mms_task_encode_generate_unique_path(dir, "smil"); + char* path = mms_task_encode_generate_path(dir, "smil", NULL); MMSAttachment* smil = mms_attachment_new_smil(config, path, (MMSAttachment**)array->pdata, array->len, error); g_free(path); diff --git a/mms-lib/test/send/data/AcceptNoExt/0001 b/mms-lib/test/send/data/AcceptNoExt/0001 new file mode 100644 index 0000000..ac86411 Binary files /dev/null and b/mms-lib/test/send/data/AcceptNoExt/0001 differ diff --git a/mms-lib/test/send/data/AcceptNoExt/m-send.conf b/mms-lib/test/send/data/AcceptNoExt/m-send.conf new file mode 100644 index 0000000..596ddaa Binary files /dev/null and b/mms-lib/test/send/data/AcceptNoExt/m-send.conf differ diff --git a/mms-lib/test/send/data/AcceptNoExt/smil b/mms-lib/test/send/data/AcceptNoExt/smil new file mode 100644 index 0000000..8531a43 --- /dev/null +++ b/mms-lib/test/send/data/AcceptNoExt/smil @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/mms-lib/test/send/data/AcceptNoExt/test.text b/mms-lib/test/send/data/AcceptNoExt/test.text new file mode 100644 index 0000000..67ea871 --- /dev/null +++ b/mms-lib/test/send/data/AcceptNoExt/test.text @@ -0,0 +1 @@ +Тестовое сообщение diff --git a/mms-lib/test/send/test_send.c b/mms-lib/test/send/test_send.c index 09166e6..08c951d 100644 --- a/mms-lib/test/send/test_send.c +++ b/mms-lib/test/send/test_send.c @@ -78,16 +78,24 @@ static const MMSAttachmentInfo test_files_accept [] = { { "test.txt", "text/plain;charset=utf-8", "text" } }; +static const MMSAttachmentInfo test_files_accept_no_ext [] = { + { "smil", NULL, NULL }, + { "0001", "image/jpeg", "image1" }, + { "0001", "image/jpeg", "image2" }, + { "test.text", "text/plain;charset=utf-8", "text" } +}; + static const MMSAttachmentInfo test_files_reject [] = { { "0001.png", "image/png", "image" }, { "test.txt", "text/plain", "text" } }; +#define ATTACHMENTS(a) a, G_N_ELEMENTS(a) + static const TestDesc send_tests[] = { { "Accept", - test_files_accept, - G_N_ELEMENTS(test_files_accept), + ATTACHMENTS(test_files_accept), "Test of successful delivery", "+1234567890", "+2345678901,+3456789012", @@ -99,10 +107,23 @@ static const TestDesc send_tests[] = { SOUP_STATUS_OK, MMS_SEND_STATE_SENDING, "TestMessageId" + },{ + "AcceptNoExt", + ATTACHMENTS(test_files_accept_no_ext), + "Test of successful delivery (no extensions)", + "+1234567890", + "+2345678901,+3456789012", + "+4567890123", + "IMSI", + 0, + "m-send.conf", + MMS_CONTENT_TYPE, + SOUP_STATUS_OK, + MMS_SEND_STATE_SENDING, + "TestMessageId" },{ "Reject", - test_files_reject, - G_N_ELEMENTS(test_files_reject), + ATTACHMENTS(test_files_reject), "Rejection test", "+1234567890", NULL,