1
0
Fork 0
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:
B. Petersen 2018-04-24 16:12:04 +02:00
parent 61e3aca8f3
commit ae8603936f
2 changed files with 34 additions and 12 deletions

View file

@ -380,12 +380,11 @@ char* mrmsg_get_filemime(const mrmsg_t* msg)
ret = mrparam_get(msg->m_param, MRP_MIMETYPE, NULL);
if( ret == NULL ) {
int dummy_msgtype = 0;
file = mrparam_get(msg->m_param, MRP_FILE, NULL);
if( file == NULL ) {
goto cleanup;
}
mrmsg_guess_msgtype_from_suffix(file, &dummy_msgtype, &ret);
mrmsg_guess_msgtype_from_suffix(file, NULL, &ret);
if( ret == NULL ) {
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[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.
*
@ -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)
{
if( pathNfilename == NULL || ret_msgtype == NULL || ret_mime == NULL) {
return;
char* suffix = NULL;
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_mime = NULL;
char* s = mr_get_filesuffix_lc(pathNfilename);
if( s == NULL ) {
suffix = mr_get_filesuffix_lc(pathNfilename);
if( suffix == NULL ) {
goto cleanup;
}
if( strcmp(s, "mp3")==0 ) {
if( strcmp(suffix, "mp3")==0 ) {
*ret_msgtype = MR_MSG_AUDIO;
*ret_mime = safe_strdup("audio/mpeg");
}
else if( strcmp(s, "mp4")==0 ) {
else if( strcmp(suffix, "mp4")==0 ) {
*ret_msgtype = MR_MSG_VIDEO;
*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_mime = safe_strdup("image/jpeg");
}
else if( strcmp(s, "png")==0 ) {
else if( strcmp(suffix, "png")==0 ) {
*ret_msgtype = MR_MSG_IMAGE;
*ret_mime = safe_strdup("image/png");
}
else if( strcmp(s, "gif")==0 ) {
else if( strcmp(suffix, "gif")==0 ) {
*ret_msgtype = MR_MSG_GIF;
*ret_mime = safe_strdup("image/gif");
}
cleanup:
free(s);
free(suffix);
free(dummy_buf);
}