Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #12 from monich/content-type
[mms_task_encode] Append file extensions for known media types
  • Loading branch information
Slava Monich committed Mar 31, 2014
2 parents 07d474d + d8dbfb6 commit 63f3eb9
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
55 changes: 46 additions & 9 deletions mms-lib/src/mms_task_encode.c
Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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);
Expand Down
Binary file added mms-lib/test/send/data/AcceptNoExt/0001
Binary file not shown.
Binary file added mms-lib/test/send/data/AcceptNoExt/m-send.conf
Binary file not shown.
16 changes: 16 additions & 0 deletions mms-lib/test/send/data/AcceptNoExt/smil
@@ -0,0 +1,16 @@
<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 1.0//EN" "http://www.w3.org/TR/REC-smil/SMIL10.dtd">
<smil>
<head>
<layout>
<root-layout height="160" width="120"/>
<region fit="scroll" height="100%" left="0" top="0" width="100%" id="Text"/>
<region fit="meet" height="100%" left="0" top="0" width="100%" id="Media"/>
</layout>
</head>
<body>
<par dur="5000ms">
<text src="test.txt" region="Text"/>
<img src="0001.jpg" region="Media"/>
</par>
</body>
</smil>
1 change: 1 addition & 0 deletions mms-lib/test/send/data/AcceptNoExt/test.text
@@ -0,0 +1 @@
Тестовое сообщение
29 changes: 25 additions & 4 deletions mms-lib/test/send/test_send.c
Expand Up @@ -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",
Expand All @@ -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,
Expand Down

0 comments on commit 63f3eb9

Please sign in to comment.