--- a/src/index/mimetype.cpp
+++ b/src/index/mimetype.cpp
@@ -1,14 +1,47 @@
#ifndef lint
-static char rcsid[] = "@(#$Id: mimetype.cpp,v 1.5 2005-02-09 13:34:08 dockes Exp $ (C) 2004 J.F.Dockes";
+static char rcsid[] = "@(#$Id: mimetype.cpp,v 1.6 2005-03-25 09:40:27 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
#include <ctype.h>
#include <string>
using std::string;
+#include <list>
+using std::list;
#include "mimetype.h"
#include "debuglog.h"
+#include "execmd.h"
+#include "conftree.h"
+
+static string mimetypefromdata(const string &fn)
+{
+ list<string> args;
+
+ args.push_back("-i");
+ args.push_back(fn);
+ ExecCmd ex;
+ string result;
+ string cmd = "file";
+ int status = ex.doexec(cmd, args, 0, &result);
+ if (status) {
+ LOGERR(("mimetypefromdata: doexec: status 0x%x\n", status));
+ return "";
+ }
+ // LOGDEB(("mimetypefromdata: %s [%s]\n", result.c_str(), fn.c_str()));
+ list<string> res;
+ ConfTree::stringToStrings(result, res);
+ if (res.size() <= 1)
+ return "";
+ list<string>::iterator it = res.begin();
+ it++;
+ string mime = *it;
+
+ if (mime.length() > 0 && !isalpha(mime[mime.length() - 1]))
+ mime.erase(mime.length() -1);
+
+ return mime;
+}
string mimetype(const string &fn, ConfTree *mtypes)
{
@@ -38,8 +71,9 @@
// If the file name has a suffix and we find it in the map, we're done
string::size_type dot = fn.find_last_of(".");
+ string suff;
if (dot != string::npos) {
- string suff = fn.substr(dot);
+ suff = fn.substr(dot);
for (unsigned int i = 0; i < suff.length(); i++)
suff[i] = tolower(suff[i]);
@@ -48,7 +82,9 @@
return mtype;
}
- // Look at file data ? One day maybe
+ // Look at file data ? Only when no suffix
+ if (suff.empty())
+ return mimetypefromdata(fn);
return "";
}