mirror of
https://github.com/deltachat/deltachat-core.git
synced 2025-10-06 03:50:08 +02:00
simplify msg-type-guessing
This commit is contained in:
parent
61e3aca8f3
commit
ae8603936f
2 changed files with 34 additions and 12 deletions
|
@ -276,6 +276,20 @@ void stress_functions(mrmailbox_t* mailbox)
|
||||||
mrmimeparser_unref(mimeparser);
|
mrmimeparser_unref(mimeparser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* test message helpers
|
||||||
|
**************************************************************************/
|
||||||
|
|
||||||
|
{
|
||||||
|
int type;
|
||||||
|
char* mime;
|
||||||
|
mrmsg_guess_msgtype_from_suffix("foo/bar-sth.mp3", NULL, NULL);
|
||||||
|
mrmsg_guess_msgtype_from_suffix("foo/bar-sth.mp3", NULL, &mime);
|
||||||
|
assert( strcmp(mime, "audio/mpeg")==0 );
|
||||||
|
mrmsg_guess_msgtype_from_suffix("foo/bar-sth.mp3", &type, NULL);
|
||||||
|
assert( type == MR_MSG_AUDIO );
|
||||||
|
free(mime);
|
||||||
|
}
|
||||||
|
|
||||||
/* test some string functions
|
/* test some string functions
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
|
|
32
src/mrmsg.c
32
src/mrmsg.c
|
@ -380,12 +380,11 @@ char* mrmsg_get_filemime(const mrmsg_t* msg)
|
||||||
|
|
||||||
ret = mrparam_get(msg->m_param, MRP_MIMETYPE, NULL);
|
ret = mrparam_get(msg->m_param, MRP_MIMETYPE, NULL);
|
||||||
if( ret == NULL ) {
|
if( ret == NULL ) {
|
||||||
int dummy_msgtype = 0;
|
|
||||||
file = mrparam_get(msg->m_param, MRP_FILE, NULL);
|
file = mrparam_get(msg->m_param, MRP_FILE, NULL);
|
||||||
if( file == NULL ) {
|
if( file == NULL ) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
mrmsg_guess_msgtype_from_suffix(file, &dummy_msgtype, &ret);
|
mrmsg_guess_msgtype_from_suffix(file, NULL, &ret);
|
||||||
|
|
||||||
if( ret == NULL ) {
|
if( ret == NULL ) {
|
||||||
ret = safe_strdup("application/octet-stream");
|
ret = safe_strdup("application/octet-stream");
|
||||||
|
@ -950,6 +949,7 @@ int mrmsg_load_from_db__(mrmsg_t* ths, mrmailbox_t* mailbox, uint32_t id)
|
||||||
* @param pathNfilename Path and filename of the file to guess the type for.
|
* @param pathNfilename Path and filename of the file to guess the type for.
|
||||||
*
|
*
|
||||||
* @param[out] ret_msgtype Guessed message type is copied here as one of the MR_MSG_* constants.
|
* @param[out] ret_msgtype Guessed message type is copied here as one of the MR_MSG_* constants.
|
||||||
|
* May be NULL if you're not interested in this value.
|
||||||
*
|
*
|
||||||
* @param[out] ret_mime The pointer to a string buffer is set to the guessed MIME-type. May be NULL. Must be free()'d by the caller.
|
* @param[out] ret_mime The pointer to a string buffer is set to the guessed MIME-type. May be NULL. Must be free()'d by the caller.
|
||||||
*
|
*
|
||||||
|
@ -957,41 +957,49 @@ int mrmsg_load_from_db__(mrmsg_t* ths, mrmailbox_t* mailbox, uint32_t id)
|
||||||
*/
|
*/
|
||||||
void mrmsg_guess_msgtype_from_suffix(const char* pathNfilename, int* ret_msgtype, char** ret_mime)
|
void mrmsg_guess_msgtype_from_suffix(const char* pathNfilename, int* ret_msgtype, char** ret_mime)
|
||||||
{
|
{
|
||||||
if( pathNfilename == NULL || ret_msgtype == NULL || ret_mime == NULL) {
|
char* suffix = NULL;
|
||||||
return;
|
int dummy_msgtype = 0;
|
||||||
|
char* dummy_buf = NULL;
|
||||||
|
|
||||||
|
if( pathNfilename == NULL ) {
|
||||||
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( ret_msgtype == NULL ) { ret_msgtype = &dummy_msgtype; }
|
||||||
|
if( ret_mime == NULL ) { ret_mime = &dummy_buf; }
|
||||||
|
|
||||||
*ret_msgtype = MR_MSG_UNDEFINED;
|
*ret_msgtype = MR_MSG_UNDEFINED;
|
||||||
*ret_mime = NULL;
|
*ret_mime = NULL;
|
||||||
|
|
||||||
char* s = mr_get_filesuffix_lc(pathNfilename);
|
suffix = mr_get_filesuffix_lc(pathNfilename);
|
||||||
if( s == NULL ) {
|
if( suffix == NULL ) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( strcmp(s, "mp3")==0 ) {
|
if( strcmp(suffix, "mp3")==0 ) {
|
||||||
*ret_msgtype = MR_MSG_AUDIO;
|
*ret_msgtype = MR_MSG_AUDIO;
|
||||||
*ret_mime = safe_strdup("audio/mpeg");
|
*ret_mime = safe_strdup("audio/mpeg");
|
||||||
}
|
}
|
||||||
else if( strcmp(s, "mp4")==0 ) {
|
else if( strcmp(suffix, "mp4")==0 ) {
|
||||||
*ret_msgtype = MR_MSG_VIDEO;
|
*ret_msgtype = MR_MSG_VIDEO;
|
||||||
*ret_mime = safe_strdup("video/mp4");
|
*ret_mime = safe_strdup("video/mp4");
|
||||||
}
|
}
|
||||||
else if( strcmp(s, "jpg")==0 || strcmp(s, "jpeg")==0 ) {
|
else if( strcmp(suffix, "jpg")==0 || strcmp(suffix, "jpeg")==0 ) {
|
||||||
*ret_msgtype = MR_MSG_IMAGE;
|
*ret_msgtype = MR_MSG_IMAGE;
|
||||||
*ret_mime = safe_strdup("image/jpeg");
|
*ret_mime = safe_strdup("image/jpeg");
|
||||||
}
|
}
|
||||||
else if( strcmp(s, "png")==0 ) {
|
else if( strcmp(suffix, "png")==0 ) {
|
||||||
*ret_msgtype = MR_MSG_IMAGE;
|
*ret_msgtype = MR_MSG_IMAGE;
|
||||||
*ret_mime = safe_strdup("image/png");
|
*ret_mime = safe_strdup("image/png");
|
||||||
}
|
}
|
||||||
else if( strcmp(s, "gif")==0 ) {
|
else if( strcmp(suffix, "gif")==0 ) {
|
||||||
*ret_msgtype = MR_MSG_GIF;
|
*ret_msgtype = MR_MSG_GIF;
|
||||||
*ret_mime = safe_strdup("image/gif");
|
*ret_mime = safe_strdup("image/gif");
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
free(s);
|
free(suffix);
|
||||||
|
free(dummy_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue