misc cleanup + tell filters if working for preview or index

This commit is contained in:
dockes 2005-11-18 15:19:14 +00:00
parent b83513021d
commit 01780f5c42
5 changed files with 75 additions and 53 deletions

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: internfile.cpp,v 1.8 2005-11-14 09:59:17 dockes Exp $ (C) 2004 J.F.Dockes"; static char rcsid[] = "@(#$Id: internfile.cpp,v 1.9 2005-11-18 15:19:14 dockes Exp $ (C) 2004 J.F.Dockes";
#endif #endif
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
@ -72,23 +72,24 @@ static bool uncompressfile(RclConfig *conf, const string& ifn,
void FileInterner::tmpcleanup() void FileInterner::tmpcleanup()
{ {
if (tdir.empty() || tfile.empty()) if (m_tdir.empty() || m_tfile.empty())
return; return;
if (unlink(tfile.c_str()) < 0) { if (unlink(m_tfile.c_str()) < 0) {
LOGERR(("FileInterner::tmpcleanup: unlink(%s) errno %d\n", LOGERR(("FileInterner::tmpcleanup: unlink(%s) errno %d\n",
tfile.c_str(), errno)); m_tfile.c_str(), errno));
return; return;
} }
} }
// Handler==0 on return says we're in error // Handler==0 on return says we're in error, will be handled when calling
// internfile
FileInterner::FileInterner(const std::string &f, RclConfig *cnf, FileInterner::FileInterner(const std::string &f, RclConfig *cnf,
const string& td, const string *imime) const string& td, const string *imime)
: fn(f), config(cnf), tdir(td), handler(0) : m_fn(f), m_cfg(cnf), m_tdir(td), m_handler(0)
{ {
// Note that we are actually going to access the file, so that it's ok // We are actually going to access the file, so it's ok
// to check this config variable at every call even if it can only change // performancewise to check this config variable at every call
// when we change directories // even if it can only change when we change directories
string usfc; string usfc;
int usfci; int usfci;
if (!cnf->getConfParam("usesystemfilecommand", usfc)) if (!cnf->getConfParam("usesystemfilecommand", usfc))
@ -97,17 +98,19 @@ FileInterner::FileInterner(const std::string &f, RclConfig *cnf,
usfci = atoi(usfc.c_str()) ? 1 : 0; usfci = atoi(usfc.c_str()) ? 1 : 0;
LOGDEB1(("FileInterner::FileInterner: usfci now %d\n", usfci)); LOGDEB1(("FileInterner::FileInterner: usfci now %d\n", usfci));
bool forPreview = imime ? true : false;
// We need to run mime type identification in any case to check // We need to run mime type identification in any case to check
// for a compressed file. // for a compressed file.
mime = mimetype(fn, config->getMimeMap(), usfci); m_mime = mimetype(m_fn, m_cfg->getMimeMap(), usfci);
// If identification fails, try to use the input parameter. Note that this // If identification fails, try to use the input parameter. Note that this
// is normally not a compressed type (it's the mime type from the db) // is normally not a compressed type (it's the mime type from the db)
if (mime.empty() && imime) if (m_mime.empty() && imime)
mime = *imime; m_mime = *imime;
if (mime.empty()) { if (m_mime.empty()) {
// No mime type: not listed in our map, or present in stop list // No mime type: not listed in our map, or present in stop list
LOGDEB(("FileInterner::FileInterner: (no mime) [%s]\n", fn.c_str())); LOGDEB(("FileInterner::FileInterner: (no mime) [%s]\n", m_fn.c_str()));
return; return;
} }
@ -115,42 +118,45 @@ FileInterner::FileInterner(const std::string &f, RclConfig *cnf,
// uncompressed file, and rerun the mime type identification, then do the // uncompressed file, and rerun the mime type identification, then do the
// rest with the temp file. // rest with the temp file.
list<string>ucmd; list<string>ucmd;
if (getUncompressor(mime, config->getMimeConf(), ucmd)) { if (getUncompressor(m_mime, m_cfg->getMimeConf(), ucmd)) {
if (!uncompressfile(config, fn, ucmd, tdir, tfile)) { if (!uncompressfile(m_cfg, m_fn, ucmd, m_tdir, m_tfile)) {
return; return;
} }
LOGDEB(("internfile: after ucomp: tdir %s, tfile %s\n", LOGDEB(("internfile: after ucomp: m_tdir %s, tfile %s\n",
tdir.c_str(), tfile.c_str())); m_tdir.c_str(), m_tfile.c_str()));
fn = tfile; m_fn = m_tfile;
mime = mimetype(fn, config->getMimeMap(), usfci); m_mime = mimetype(m_fn, m_cfg->getMimeMap(), usfci);
if (mime.empty() && imime) if (m_mime.empty() && imime)
mime = *imime; m_mime = *imime;
if (mime.empty()) { if (m_mime.empty()) {
// No mime type ?? pass on. // No mime type ?? pass on.
LOGDEB(("internfile: (no mime) [%s]\n", fn.c_str())); LOGDEB(("internfile: (no mime) [%s]\n", m_fn.c_str()));
return; return;
} }
} }
// Look for appropriate handler // Look for appropriate handler
handler = getMimeHandler(mime, config->getMimeConf()); m_handler = getMimeHandler(m_mime, m_cfg->getMimeConf());
if (!handler) { if (!m_handler) {
// No handler for this type, for now :( // No handler for this type, for now :(
LOGDEB(("FileInterner::FileInterner: %s: no handler\n", mime.c_str())); LOGDEB(("FileInterner::FileInterner: %s: no handler\n",
m_mime.c_str()));
return; return;
} }
m_handler->setForPreview(forPreview);
LOGDEB(("FileInterner::FileInterner: %s [%s]\n",mime.c_str(), fn.c_str())); LOGDEB(("FileInterner::FileInterner: %s [%s]\n", m_mime.c_str(),
m_fn.c_str()));
} }
FileInterner::Status FileInterner::internfile(Rcl::Doc& doc, string& ipath) FileInterner::Status FileInterner::internfile(Rcl::Doc& doc, string& ipath)
{ {
if (!handler) if (!m_handler)
return FIError; return FIError;
// Turn file into a document. The document has fields for title, body // Turn file into a document. The document has fields for title, body
// etc., all text converted to utf8 // etc., all text converted to utf8
MimeHandler::Status mhs = handler->mkDoc(config, fn, mime, doc, ipath); MimeHandler::Status mhs =
m_handler->mkDoc(m_cfg, m_fn, m_mime, doc, ipath);
FileInterner::Status ret = FIError; FileInterner::Status ret = FIError;
switch (mhs) { switch (mhs) {
case MimeHandler::MHError: break; case MimeHandler::MHError: break;
@ -158,13 +164,13 @@ FileInterner::Status FileInterner::internfile(Rcl::Doc& doc, string& ipath)
case MimeHandler::MHAgain: ret = FIAgain;break; case MimeHandler::MHAgain: ret = FIAgain;break;
} }
doc.mimetype = mime; doc.mimetype = m_mime;
return ret; return ret;
} }
FileInterner::~FileInterner() FileInterner::~FileInterner()
{ {
delete handler; delete m_handler;
handler = 0; m_handler = 0;
tmpcleanup(); tmpcleanup();
} }

View File

@ -1,6 +1,6 @@
#ifndef _INTERNFILE_H_INCLUDED_ #ifndef _INTERNFILE_H_INCLUDED_
#define _INTERNFILE_H_INCLUDED_ #define _INTERNFILE_H_INCLUDED_
/* @(#$Id: internfile.h,v 1.4 2005-11-14 09:59:17 dockes Exp $ (C) 2004 J.F.Dockes */ /* @(#$Id: internfile.h,v 1.5 2005-11-18 15:19:14 dockes Exp $ (C) 2004 J.F.Dockes */
#include <string> #include <string>
@ -12,24 +12,20 @@ class MimeHandler;
/// Turn external file into internal representation, according to mime /// Turn external file into internal representation, according to mime
/// type etc /// type etc
class FileInterner { class FileInterner {
string fn;
RclConfig *config;
const string &tdir;
MimeHandler *handler;
string tfile;
string mime;
void tmpcleanup();
public: public:
/** /**
* Identify and possibly decompress file, create adequate handler * Identify and possibly decompress file, create adequate
* handler. The mtype parameter is only set when the object is
* created for previewing a file. Filter output may be
* different for previewing and indexing.
*
* @param fn file name * @param fn file name
* @param cnf Recoll configuration * @param cnf Recoll configuration
* @param td temporary directory to use as working space for possible * @param td temporary directory to use as working space if
* decompression * decompression needed.
* @param mimetype mime type if known. For a compressed file this is the * @param mtype mime type if known. For a compressed file this is the
* mime type for the uncompressed version. * mime type for the uncompressed version. This currently doubles up
* to indicate that this object is for previewing (not indexing).
*/ */
FileInterner(const std::string &fn, RclConfig *cnf, const string& td, FileInterner(const std::string &fn, RclConfig *cnf, const string& td,
const std::string *mtype = 0); const std::string *mtype = 0);
@ -53,6 +49,17 @@ class FileInterner {
* should be called again to get the following one(s). * should be called again to get the following one(s).
*/ */
Status internfile(Rcl::Doc& doc, string &ipath); Status internfile(Rcl::Doc& doc, string &ipath);
private:
string m_fn;
RclConfig *m_cfg;
const string &m_tdir;
MimeHandler *m_handler;
string m_tfile;
string m_mime;
void tmpcleanup();
}; };
#endif /* _INTERNFILE_H_INCLUDED_ */ #endif /* _INTERNFILE_H_INCLUDED_ */

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: mh_exec.cpp,v 1.1 2005-11-18 13:23:46 dockes Exp $ (C) 2005 J.F.Dockes"; static char rcsid[] = "@(#$Id: mh_exec.cpp,v 1.2 2005-11-18 15:19:14 dockes Exp $ (C) 2005 J.F.Dockes";
#endif #endif
#include "execmd.h" #include "execmd.h"
@ -32,6 +32,8 @@ MimeHandlerExec::mkDoc(RclConfig *conf, const string &fn,
// Execute command and store the result text, which is supposedly html // Execute command and store the result text, which is supposedly html
string html; string html;
ExecCmd exec; ExecCmd exec;
exec.putenv(m_forPreview ? "RECOLL_FILTER_FORPREVIEW=yes" :
"RECOLL_FILTER_FORPREVIEW=no");
int status = exec.doexec(cmd, myparams, 0, &html); int status = exec.doexec(cmd, myparams, 0, &html);
if (status) { if (status) {
LOGERR(("MimeHandlerExec: command status 0x%x: %s\n", LOGERR(("MimeHandlerExec: command status 0x%x: %s\n",

View File

@ -1,6 +1,6 @@
#ifndef _MIMEHANDLER_H_INCLUDED_ #ifndef _MIMEHANDLER_H_INCLUDED_
#define _MIMEHANDLER_H_INCLUDED_ #define _MIMEHANDLER_H_INCLUDED_
/* @(#$Id: mimehandler.h,v 1.8 2005-11-16 15:07:20 dockes Exp $ (C) 2004 J.F.Dockes */ /* @(#$Id: mimehandler.h,v 1.9 2005-11-18 15:19:14 dockes Exp $ (C) 2004 J.F.Dockes */
#include <string> #include <string>
#include <list> #include <list>
@ -14,6 +14,7 @@
*/ */
class MimeHandler { class MimeHandler {
public: public:
MimeHandler() : m_forPreview(false) {}
virtual ~MimeHandler() {} virtual ~MimeHandler() {}
/// Status from mkDoc method. /// Status from mkDoc method.
@ -41,6 +42,11 @@ class MimeHandler {
const std::string &mimetype, const std::string &mimetype,
Rcl::Doc& outdoc, Rcl::Doc& outdoc,
string& ipath) = 0; string& ipath) = 0;
virtual void setForPreview(bool onoff) {m_forPreview = onoff;};
protected:
bool m_forPreview;
}; };
/** /**

View File

@ -1,6 +1,6 @@
#ifndef _EXECMD_H_INCLUDED_ #ifndef _EXECMD_H_INCLUDED_
#define _EXECMD_H_INCLUDED_ #define _EXECMD_H_INCLUDED_
/* @(#$Id: execmd.h,v 1.3 2005-11-18 13:52:48 dockes Exp $ (C) 2004 J.F.Dockes */ /* @(#$Id: execmd.h,v 1.4 2005-11-18 15:19:14 dockes Exp $ (C) 2004 J.F.Dockes */
#include <string> #include <string>
#include <list> #include <list>
@ -28,7 +28,8 @@ class ExecCmd {
std::string *output = 0); std::string *output = 0);
/** /**
* Add/replace environment variable before executing command. This should * Add/replace environment variable before executing command. This should
* be called before doexec of course. * be called before doexec of course (possibly multiple times for several
* variables).
* @param envassign an environment assignment string (name=value) * @param envassign an environment assignment string (name=value)
*/ */
void putenv(const std::string &envassign); void putenv(const std::string &envassign);