|
a/src/index/mimetype.cpp |
|
b/src/index/mimetype.cpp |
1 |
#ifndef lint
|
1 |
#ifndef lint
|
2 |
static char rcsid[] = "@(#$Id: mimetype.cpp,v 1.5 2005-02-09 13:34:08 dockes Exp $ (C) 2004 J.F.Dockes";
|
2 |
static char rcsid[] = "@(#$Id: mimetype.cpp,v 1.6 2005-03-25 09:40:27 dockes Exp $ (C) 2004 J.F.Dockes";
|
3 |
#endif
|
3 |
#endif
|
4 |
|
4 |
|
5 |
#include <ctype.h>
|
5 |
#include <ctype.h>
|
6 |
|
6 |
|
7 |
#include <string>
|
7 |
#include <string>
|
8 |
using std::string;
|
8 |
using std::string;
|
|
|
9 |
#include <list>
|
|
|
10 |
using std::list;
|
9 |
|
11 |
|
10 |
#include "mimetype.h"
|
12 |
#include "mimetype.h"
|
11 |
#include "debuglog.h"
|
13 |
#include "debuglog.h"
|
|
|
14 |
#include "execmd.h"
|
|
|
15 |
#include "conftree.h"
|
|
|
16 |
|
|
|
17 |
static string mimetypefromdata(const string &fn)
|
|
|
18 |
{
|
|
|
19 |
list<string> args;
|
|
|
20 |
|
|
|
21 |
args.push_back("-i");
|
|
|
22 |
args.push_back(fn);
|
|
|
23 |
ExecCmd ex;
|
|
|
24 |
string result;
|
|
|
25 |
string cmd = "file";
|
|
|
26 |
int status = ex.doexec(cmd, args, 0, &result);
|
|
|
27 |
if (status) {
|
|
|
28 |
LOGERR(("mimetypefromdata: doexec: status 0x%x\n", status));
|
|
|
29 |
return "";
|
|
|
30 |
}
|
|
|
31 |
// LOGDEB(("mimetypefromdata: %s [%s]\n", result.c_str(), fn.c_str()));
|
|
|
32 |
list<string> res;
|
|
|
33 |
ConfTree::stringToStrings(result, res);
|
|
|
34 |
if (res.size() <= 1)
|
|
|
35 |
return "";
|
|
|
36 |
list<string>::iterator it = res.begin();
|
|
|
37 |
it++;
|
|
|
38 |
string mime = *it;
|
|
|
39 |
|
|
|
40 |
if (mime.length() > 0 && !isalpha(mime[mime.length() - 1]))
|
|
|
41 |
mime.erase(mime.length() -1);
|
|
|
42 |
|
|
|
43 |
return mime;
|
|
|
44 |
}
|
12 |
|
45 |
|
13 |
string mimetype(const string &fn, ConfTree *mtypes)
|
46 |
string mimetype(const string &fn, ConfTree *mtypes)
|
14 |
{
|
47 |
{
|
15 |
if (mtypes == 0)
|
48 |
if (mtypes == 0)
|
16 |
return "";
|
49 |
return "";
|
|
... |
|
... |
36 |
}
|
69 |
}
|
37 |
}
|
70 |
}
|
38 |
|
71 |
|
39 |
// If the file name has a suffix and we find it in the map, we're done
|
72 |
// If the file name has a suffix and we find it in the map, we're done
|
40 |
string::size_type dot = fn.find_last_of(".");
|
73 |
string::size_type dot = fn.find_last_of(".");
|
|
|
74 |
string suff;
|
41 |
if (dot != string::npos) {
|
75 |
if (dot != string::npos) {
|
42 |
string suff = fn.substr(dot);
|
76 |
suff = fn.substr(dot);
|
43 |
for (unsigned int i = 0; i < suff.length(); i++)
|
77 |
for (unsigned int i = 0; i < suff.length(); i++)
|
44 |
suff[i] = tolower(suff[i]);
|
78 |
suff[i] = tolower(suff[i]);
|
45 |
|
79 |
|
46 |
string mtype;
|
80 |
string mtype;
|
47 |
if (mtypes->get(suff, mtype, ""))
|
81 |
if (mtypes->get(suff, mtype, ""))
|
48 |
return mtype;
|
82 |
return mtype;
|
49 |
}
|
83 |
}
|
50 |
|
84 |
|
51 |
// Look at file data ? One day maybe
|
85 |
// Look at file data ? Only when no suffix
|
|
|
86 |
if (suff.empty())
|
|
|
87 |
return mimetypefromdata(fn);
|
52 |
return "";
|
88 |
return "";
|
53 |
}
|
89 |
}
|
54 |
|
90 |
|
55 |
|
91 |
|
56 |
|
92 |
|