mh_mail now uses mimetype() to try and better identify application/octet-stream

This commit is contained in:
dockes 2008-07-01 10:29:45 +00:00
parent 4b898a39e7
commit 0460f1016c
3 changed files with 42 additions and 23 deletions

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: mimetype.cpp,v 1.20 2006-12-20 09:54:18 dockes Exp $ (C) 2004 J.F.Dockes"; static char rcsid[] = "@(#$Id: mimetype.cpp,v 1.21 2008-07-01 10:29:45 dockes Exp $ (C) 2004 J.F.Dockes";
#endif #endif
/* /*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -57,13 +57,11 @@ using namespace std;
static string mimetypefromdata(const string &fn, bool usfc) static string mimetypefromdata(const string &fn, bool usfc)
{ {
string mime;
// In any case first try the internal identifier // In any case first try the internal identifier
mime = idFile(fn.c_str()); string mime = idFile(fn.c_str());
#ifdef USE_SYSTEM_FILE_COMMAND #ifdef USE_SYSTEM_FILE_COMMAND
if (usfc && mime == "") { if (usfc && mime.empty()) {
// Last resort: use "file -i" // Last resort: use "file -i"
list<string> args; list<string> args;
@ -75,7 +73,7 @@ static string mimetypefromdata(const string &fn, bool usfc)
int status = ex.doexec(cmd, args, 0, &result); int status = ex.doexec(cmd, args, 0, &result);
if (status) { if (status) {
LOGERR(("mimetypefromdata: doexec: status 0x%x\n", status)); LOGERR(("mimetypefromdata: doexec: status 0x%x\n", status));
return ""; return string();
} }
// LOGDEB(("mimetypefromdata: %s [%s]\n", result.c_str(), fn.c_str())); // LOGDEB(("mimetypefromdata: %s [%s]\n", result.c_str(), fn.c_str()));
@ -88,7 +86,7 @@ static string mimetypefromdata(const string &fn, bool usfc)
list<string> res; list<string> res;
stringToStrings(result, res); stringToStrings(result, res);
if (res.size() <= 1) if (res.size() <= 1)
return ""; return string();
list<string>::iterator it = res.begin(); list<string>::iterator it = res.begin();
mime = *++it; mime = *++it;
// Remove possible punctuation at the end // Remove possible punctuation at the end
@ -96,7 +94,7 @@ static string mimetypefromdata(const string &fn, bool usfc)
mime.erase(mime.length() -1); mime.erase(mime.length() -1);
// File -i will sometimes return strange stuff (ie: "very small file") // File -i will sometimes return strange stuff (ie: "very small file")
if(mime.find("/") == string::npos) if(mime.find("/") == string::npos)
mime = ""; mime.clear();
} }
#endif #endif
@ -109,17 +107,18 @@ static string mimetypefromdata(const string &fn, bool usfc)
string mimetype(const string &fn, const struct stat *stp, string mimetype(const string &fn, const struct stat *stp,
RclConfig *cfg, bool usfc) RclConfig *cfg, bool usfc)
{ {
if (S_ISDIR(stp->st_mode)) if (stp) {
return "application/x-fsdirectory"; if (S_ISDIR(stp->st_mode))
if (!S_ISREG(stp->st_mode)) return "application/x-fsdirectory";
return "application/x-fsspecial"; if (!S_ISREG(stp->st_mode))
return "application/x-fsspecial";
}
if (cfg == 0) if (cfg == 0)
return ""; return string();
if (cfg->inStopSuffixes(fn)) { if (cfg->inStopSuffixes(fn)) {
LOGDEB(("mimetype: fn [%s] in stopsuffixes\n", fn.c_str())); LOGDEB(("mimetype: fn [%s] in stopsuffixes\n", fn.c_str()));
return ""; return string();
} }
// First look for suffix in mimetype map // First look for suffix in mimetype map
@ -135,7 +134,9 @@ string mimetype(const string &fn, const struct stat *stp,
return mtype; return mtype;
} }
// Then examine data // Finally examine data
if (!stp)
return string();
return mimetypefromdata(fn, usfc); return mimetypefromdata(fn, usfc);
} }

View File

@ -16,16 +16,22 @@
*/ */
#ifndef _MIMETYPE_H_INCLUDED_ #ifndef _MIMETYPE_H_INCLUDED_
#define _MIMETYPE_H_INCLUDED_ #define _MIMETYPE_H_INCLUDED_
/* @(#$Id: mimetype.h,v 1.6 2006-12-19 08:40:50 dockes Exp $ (C) 2004 J.F.Dockes */ /* @(#$Id: mimetype.h,v 1.7 2008-07-01 10:29:45 dockes Exp $ (C) 2004 J.F.Dockes */
#include <string> #include <string>
class RclConfig; class RclConfig;
struct stat; struct stat;
/** /**
* Try to determine a mime type for filename. * Try to determine a mime type for file.
* This may imply more than matching the suffix, the name must be usable *
* to actually access file data. * If stp is not null, this may imply more than matching the suffix,
* the name must be usable to actually access file data.
* @param filename file/path name to use
* @param stp if not null use st_mode bits for directories etc.
* @param cfg recoll config
* @param usfc Use system's 'file' command as last resort (or not)
*/ */
string mimetype(const std::string &filename, const struct stat *stp, string mimetype(const std::string &filename, const struct stat *stp,
RclConfig *cfg, bool usfc); RclConfig *cfg, bool usfc);

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: mh_mail.cpp,v 1.31 2007-12-13 06:58:21 dockes Exp $ (C) 2005 J.F.Dockes"; static char rcsid[] = "@(#$Id: mh_mail.cpp,v 1.32 2008-07-01 10:29:45 dockes Exp $ (C) 2005 J.F.Dockes";
#endif #endif
/* /*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -36,6 +36,8 @@ static char rcsid[] = "@(#$Id: mh_mail.cpp,v 1.31 2007-12-13 06:58:21 dockes Exp
#include "debuglog.h" #include "debuglog.h"
#include "smallut.h" #include "smallut.h"
#include "mh_html.h" #include "mh_html.h"
#include "rclconfig.h"
#include "mimetype.h"
// binc imap mime definitions // binc imap mime definitions
#include "mime.h" #include "mime.h"
@ -103,7 +105,7 @@ bool MimeHandlerMail::skip_to_document(const string& ipath)
LOGDEB(("MimeHandlerMail::skip_to_document(%s)\n", ipath.c_str())); LOGDEB(("MimeHandlerMail::skip_to_document(%s)\n", ipath.c_str()));
if (m_idx == -1) { if (m_idx == -1) {
// No decoding done yet. If ipath is null need do nothing // No decoding done yet. If ipath is null need do nothing
if (ipath == "" || ipath == "-1") if (ipath.empty() || ipath == "-1")
return true; return true;
// ipath points to attachment: need to decode message // ipath points to attachment: need to decode message
if (!next_document()) { if (!next_document()) {
@ -189,7 +191,7 @@ bool MimeHandlerMail::processAttach()
att->m_charset.c_str(), att->m_charset.c_str(),
att->m_filename.c_str())); att->m_filename.c_str()));
m_metaData["content"] = ""; m_metaData["content"] = string();
string& body = m_metaData["content"]; string& body = m_metaData["content"];
att->m_part->getBody(body, 0, att->m_part->bodylength); att->m_part->getBody(body, 0, att->m_part->bodylength);
string decoded; string decoded;
@ -215,6 +217,16 @@ bool MimeHandlerMail::processAttach()
} }
} }
// Special case for application/octet-stream: try to better
// identify content, using file name if set
if (m_metaData["mimetype"] == "application/octet-stream" &&
!m_metaData["filename"].empty()) {
string mt = mimetype(m_metaData["filename"], 0,
RclConfig::getMainConfig(), false);
if (!mt.empty())
m_metaData["mimetype"] = mt;
}
// Ipath // Ipath
char nbuf[10]; char nbuf[10];
sprintf(nbuf, "%d", m_idx); sprintf(nbuf, "%d", m_idx);