Parent: [3fc073] (diff)

Child: [50b927] (diff)

Download this file

mimetype.cpp    116 lines (98 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef lint
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)
{
if (mtypes == 0)
return "";
static list<string> stoplist;
if (stoplist.empty()) {
string stp;
if (mtypes->get(string("recoll_noindex"), stp, "")) {
ConfTree::stringToStrings(stp, stoplist);
}
}
if (!stoplist.empty()) {
for (list<string>::const_iterator it = stoplist.begin();
it != stoplist.end(); it++) {
if (it->length() > fn.length())
continue;
if (!fn.compare(fn.length() - it->length(), string::npos, *it)) {
LOGDEB1(("mimetype: fn %s in stoplist (%s)\n", fn.c_str(),
it->c_str()));
return "";
}
}
}
// 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) {
suff = fn.substr(dot);
for (unsigned int i = 0; i < suff.length(); i++)
suff[i] = tolower(suff[i]);
string mtype;
if (mtypes->get(suff, mtype, ""))
return mtype;
}
// Look at file data ? Only when no suffix
if (suff.empty())
return mimetypefromdata(fn);
return "";
}
#ifdef _TEST_MIMETYPE_
#include <iostream>
const char *tvec[] = {
"/toto/tutu",
"/",
"toto.txt",
"toto.TXT",
"toto.C.txt",
"toto.C1",
"",
};
const int n = sizeof(tvec) / sizeof(char*);
using namespace std;
int main(int argc, const char **argv)
{
map<string, string>mtypes;
mtypes[".txt"] = "text/plain";
for (int i = 0; i < n; i++) {
cout << tvec[i] << " -> " << mimetype(string(tvec[i]), mtypes) << endl;
}
}
#endif