Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #14 from monich/empty-content-type
[mms_task_encode] Treat empty content type as no content type
  • Loading branch information
Slava Monich committed Apr 1, 2014
2 parents 3f6071d + 95d0e83 commit a34950e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 37 deletions.
73 changes: 43 additions & 30 deletions mms-lib/src/mms_attachment.c
Expand Up @@ -107,6 +107,7 @@ mms_attachment_get_path(
#endif
}

static
gboolean
mms_attachment_write_smil(
FILE* f,
Expand Down Expand Up @@ -164,6 +165,44 @@ mms_attachment_write_smil(
return FALSE;
}

char*
mms_attachment_guess_content_type(
const char* path)
{
char* content_type = NULL;
const char* detected_type = NULL;

#ifdef HAVE_MAGIC
/* Use magic to determine mime type */
magic_t magic = magic_open(MAGIC_MIME_TYPE);
if (magic) {
if (magic_load(magic, NULL) == 0) {
detected_type = magic_file(magic, path);
}
}
#endif

/* Magic detects SMIL as text/html */
if ((!detected_type ||
g_str_has_prefix(detected_type, "text/")) &&
mms_file_is_smil(path)) {
detected_type = SMIL_CONTENT_TYPE;
}

if (!detected_type) {
MMS_WARN("No mime type for %s", path);
detected_type = MMS_ATTACHMENT_DEFAULT_TYPE;
}

content_type = g_strdup(detected_type);

#ifdef HAVE_MAGIC
if (magic) magic_close(magic);
#endif

return content_type;
}

MMSAttachment*
mms_attachment_new_smil(
const MMSConfig* config,
Expand Down Expand Up @@ -214,7 +253,7 @@ mms_attachment_new(
GType type;
MMSAttachment* at;

if (info->content_type) {
if (info->content_type && info->content_type[0]) {
char** ct = mms_parse_http_content_type(info->content_type);
if (ct) {
content_type = mms_unparse_http_content_type(ct);
Expand All @@ -226,33 +265,11 @@ mms_attachment_new(
}

if (!content_type) {
/* Use magic to determine mime type */
char* detected_type = mms_attachment_guess_content_type(path);
const char* default_charset = "utf-8";
const char* detected_type = NULL;
const char* charset = NULL;
const char* ct[4];
int n;

#ifdef HAVE_MAGIC
magic_t magic = magic_open(MAGIC_MIME_TYPE);
if (magic) {
if (magic_load(magic, NULL) == 0) {
detected_type = magic_file(magic, path);
}
}
#endif

/* Magic detects SMIL as text/html */
if ((!detected_type ||
g_str_has_prefix(detected_type, "text/")) &&
mms_file_is_smil(path)) {
detected_type = SMIL_CONTENT_TYPE;
}

if (!detected_type) {
MMS_WARN("No mime type for %s", path);
detected_type = MMS_ATTACHMENT_DEFAULT_TYPE;
}
int n = 0;

if (!strcmp(detected_type, SMIL_CONTENT_TYPE)) {
flags |= MMS_ATTACHMENT_SMIL;
Expand All @@ -261,18 +278,14 @@ mms_attachment_new(
charset = default_charset;
}

n = 0;
ct[n++] = detected_type;
if (charset) {
ct[n++] = "charset";
ct[n++] = charset;
}
ct[n++] = NULL;
content_type = mms_unparse_http_content_type((char**)ct);

#ifdef HAVE_MAGIC
if (magic) magic_close(magic);
#endif
g_free(detected_type);
}

MMS_DEBUG("%s: %s", path, content_type);
Expand Down
4 changes: 4 additions & 0 deletions mms-lib/src/mms_attachment.h
Expand Up @@ -74,6 +74,10 @@ void
mms_attachment_reset(
MMSAttachment* attachment);

char*
mms_attachment_guess_content_type(
const char* path);

gboolean
mms_attachment_resize(
MMSAttachment* attachment);
Expand Down
21 changes: 16 additions & 5 deletions mms-lib/src/mms_task_encode.c
Expand Up @@ -461,12 +461,22 @@ mms_task_encode_prepare_attachments(
for (i=0; i<nparts; i++) {
MMSAttachment* attachment = NULL;
MMSAttachmentInfo info = parts[i];
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
char* path = mms_task_encode_generate_path(dir,
char* detected_type = NULL;
char* path;
GFile* src;
GFile* dest;

if (!info.content_type || !info.content_type[0]) {
detected_type = mms_attachment_guess_content_type(info.file_name);
info.content_type = detected_type;
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
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);
G_GNUC_END_IGNORE_DEPRECATIONS;

src = g_file_new_for_path(info.file_name);
dest = g_file_new_for_path(path);
if (g_file_copy(src, dest, 0, NULL, NULL, NULL, error)) {
info.file_name = path;
attachment = mms_attachment_new(config, &info, error);
Expand All @@ -482,6 +492,7 @@ mms_task_encode_prepare_attachments(
}
g_object_unref(src);
g_object_unref(dest);
g_free(detected_type);
g_free(path);
if (!attachment) break;
}
Expand Down
4 changes: 2 additions & 2 deletions mms-lib/test/send/test_send.c
Expand Up @@ -80,8 +80,8 @@ static const MMSAttachmentInfo test_files_accept [] = {

static const MMSAttachmentInfo test_files_accept_no_ext [] = {
{ "smil", NULL, NULL },
{ "0001", "image/jpeg", "image1" },
{ "0001", "image/jpeg", "image2" },
{ "0001", NULL, "image1" },
{ "0001", "", "image2" },
{ "test.text", "text/plain;charset=utf-8", "text" }
};

Expand Down

0 comments on commit a34950e

Please sign in to comment.