Skip to content

Commit c98794d

Browse files
committed
use libmagic for filetype determination
1 parent 3daeaee commit c98794d

File tree

5 files changed

+132
-128
lines changed

5 files changed

+132
-128
lines changed

doc/astyle.options

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unchanged* ./ProcessorFactory.cpp

src/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <QTextStream>
3737

3838

39-
#include <magic.h>
4039

4140
//#include "rtengine.h"
4241

@@ -116,7 +115,7 @@ void customMessageHandler(QtMsgType type, const char *msg)
116115
int main ( int argc, char **argv )
117116
{
118117
// some logging initialization
119-
qInstallMsgHandler(customMessageHandler);
118+
// qInstallMsgHandler(customMessageHandler);
120119

121120
// rtengine::Settings mySettings;
122121
// rtengine::init (&s, ".");

src/processors/ImageProcessor.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,35 @@
4343
namespace openPablo
4444
{
4545

46-
/*
47-
* @class ImageProcessor
48-
*
49-
* @brief Abstract class to interface the capabilities of a processor
50-
*
51-
* Abstract class..
52-
*
53-
*/
46+
/*
47+
* @class ImageProcessor
48+
*
49+
* @brief Abstract class to interface the capabilities of a processor
50+
*
51+
* Abstract class..
52+
*
53+
*/
5454

5555

56-
ImageProcessor::ImageProcessor()
57-
{
58-
//
59-
}
56+
ImageProcessor::ImageProcessor()
57+
{
58+
//
59+
}
6060

6161

6262

63-
ImageProcessor::~ImageProcessor()
64-
{
65-
//
63+
ImageProcessor::~ImageProcessor()
64+
{
65+
//
6666

67-
}
67+
}
6868

6969

7070

71-
void ImageProcessor::start ()
72-
{
73-
//
74-
}
71+
void ImageProcessor::start ()
72+
{
73+
//
74+
}
75+
7576

76-
7777
}

src/processors/ProcessorFactory.cpp

Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* ProcessorFactory.cpp
3-
*
4-
*
3+
*
4+
*
55
* This file is part of openPalo.
66
*
77
* Copyright (c) 2012- Aydin Demircioglu ([email protected])
@@ -10,7 +10,7 @@
1010
* it under the terms of the GNU General Public License as published by
1111
* the Free Software Foundation, either version 3 of the License, or
1212
* (at your option) any later version.
13-
*
13+
*
1414
* openPablo is distributed in the hope that it will be useful,
1515
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1616
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -23,25 +23,28 @@
2323

2424
/*
2525
* @mainpage ProcessorFactory
26-
*
26+
*
2727
* Description in html
2828
* @author Aydin Demircioglu
29-
*/
29+
*/
3030

3131

32-
/*
32+
/*
3333
* @file ProcessorFactory.hpp
34-
*
34+
*
3535
* @brief description in brief.
36-
*
37-
*/
36+
*
37+
*/
3838

3939

4040
#include <QString>
4141
#include <QFile>
4242
#include <QDataStream>
4343
#include <QDebug>
4444

45+
#include <magic.h>
46+
47+
4548
#include "ProcessorFactory.hpp"
4649

4750
#include "ImageProcessor.hpp"
@@ -50,74 +53,75 @@
5053
namespace openPablo
5154
{
5255

53-
/*
54-
* @class ProcessorFactory
55-
*
56-
* @brief Factory class to create the right processor for a given image
57-
*
58-
* This will give back the correct processor when provided with an image
59-
* in form of filename.
60-
*
61-
*/
62-
63-
Processor* ProcessorFactory::createInstance (QString imageFileName)
64-
{
65-
ImageProcessor *imageProcessor = new ImageProcessor();
66-
67-
qDebug() << "Analysing file type of file ";
68-
69-
QFile file(imageFileName);
70-
if (!file.open(QIODevice::ReadOnly) )
71-
{
72-
qDebug() << ("failed to load ");// << iccPath << "/" << outputICC << "\n";
73-
return imageProcessor;
74-
}
75-
76-
// Read and check the header
77-
QDataStream in(&file);
78-
quint32 magic;
79-
in >> magic;
80-
file.close();
81-
82-
// -----
83-
84-
// check if its a pdf file
85-
if ( magic == 2064261152)
86-
{
87-
// this is a pdf file
88-
// std::cout << " Found PDF File.\n";
89-
}
90-
91-
92-
93-
// determine type of image
94-
95-
// for now just PDF file
96-
// const char *magic_full;
97-
// magic_t magic_cookie;
98-
// /*MAGIC_MIME tells magic to return a mime of the file, but you can specify different things*/
99-
// magic_cookie = magic_open(MAGIC_MIME);
100-
//
101-
// if (magic_cookie == NULL) {
102-
// printf("unable to initialize magic library\n");
103-
// return 1;
104-
// }
105-
// printf("Loading default magic database\n");
106-
// if (magic_load(magic_cookie, NULL) != 0) {
107-
// printf("cannot load magic database - %s\n", magic_error(magic_cookie));
108-
// magic_close(magic_cookie);
109-
// return 1;
110-
// }
111-
// magic_full = magic_file(magic_cookie, inputFile.c_str());
112-
// std::cout << "Filetype: " << magic_full;
113-
// magic_close(magic_cookie);
114-
//
115-
116-
// create processor
117-
118-
// assign correct filename to processor
119-
120-
return imageProcessor;
121-
}
122-
56+
/*
57+
* @class ProcessorFactory
58+
*
59+
* @brief Factory class to create the right processor for a given image
60+
*
61+
* This will give back the correct processor when provided with an image
62+
* in form of filename.
63+
*
64+
*/
65+
66+
Processor* ProcessorFactory::createInstance (QString imageFileName)
67+
{
68+
ImageProcessor *imageProcessor = new ImageProcessor();
69+
70+
qDebug() << "Analysing file type of file " << imageFileName;
71+
72+
73+
// determine type of image
74+
// apply magic for it
75+
const char *magic_full;
76+
magic_t magic_cookie;
77+
78+
magic_cookie = magic_open(MAGIC_MIME);
79+
80+
if (magic_cookie == NULL)
81+
{
82+
qDebug() << "unable to initialize magic library";
83+
return imageProcessor;
84+
}
85+
printf("Loading default magic database\n");
86+
if (magic_load(magic_cookie, NULL) != 0)
87+
{
88+
qDebug() << "cannot load magic database" << magic_error(magic_cookie);
89+
magic_close(magic_cookie);
90+
return imageProcessor;
91+
}
92+
magic_full = magic_file(magic_cookie, imageFileName.toStdString().c_str());
93+
94+
// convert to qstring
95+
QString mimeType(magic_full);
96+
qDebug() << "Filetype: " << mimeType;
97+
magic_close(magic_cookie);
98+
99+
// create processor
100+
if (mimeType.contains("application/pdf", Qt::CaseInsensitive))
101+
{
102+
qDebug() << " Determined file type PDF.";
103+
104+
// create PDF Processor
105+
ImageProcessor *pdfProcessor = new ImageProcessor();
106+
return pdfProcessor;
107+
}
108+
109+
if (mimeType.contains("image/jpeg", Qt::CaseInsensitive))
110+
{
111+
qDebug() << " Determined file type JPG.";
112+
113+
// create PDF Processor
114+
ImageProcessor *imageProcessor = new ImageProcessor();
115+
return imageProcessor;
116+
}
117+
118+
// assign correct filename to processor
119+
120+
// unable to do anything
121+
qDebug() << " Unknown file type.";
122+
123+
124+
return imageProcessor;
125+
}
126+
123127
}

src/processors/ProcessorFactory.hpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* ProcessorFactory.hpp
3-
*
4-
*
3+
*
4+
*
55
* This file is part of openPalo.
66
*
77
* Copyright (c) 2012- Aydin Demircioglu ([email protected])
@@ -10,7 +10,7 @@
1010
* it under the terms of the GNU General Public License as published by
1111
* the Free Software Foundation, either version 3 of the License, or
1212
* (at your option) any later version.
13-
*
13+
*
1414
* openPablo is distributed in the hope that it will be useful,
1515
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1616
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -27,18 +27,18 @@
2727

2828
/*
2929
* @mainpage ProcessorFactory
30-
*
30+
*
3131
* Description in html
3232
* @author Aydin Demircioglu
33-
*/
33+
*/
3434

3535

36-
/*
36+
/*
3737
* @file ProcessorFactory.hpp
38-
*
38+
*
3939
* @brief description in brief.
40-
*
41-
*/
40+
*
41+
*/
4242

4343

4444
#include <QString>
@@ -53,26 +53,26 @@ using namespace openPablo;
5353
namespace openPablo
5454
{
5555

56-
/*
57-
* @class ProcessorFactory
58-
*
59-
* @brief Factory class to create the right processor for a given image
60-
*
61-
* This will give back the correct processor when provided with an image
62-
* in form of filename.
63-
*
64-
*/
65-
class ProcessorFactory
66-
{
67-
public:
6856
/*
69-
*
57+
* @class ProcessorFactory
58+
*
59+
* @brief Factory class to create the right processor for a given image
60+
*
61+
* This will give back the correct processor when provided with an image
62+
* in form of filename.
63+
*
7064
*/
71-
static Processor* createInstance (QString imageFileName);
65+
class ProcessorFactory
66+
{
67+
public:
68+
/*
69+
*
70+
*/
71+
static Processor* createInstance (QString imageFileName);
72+
73+
};
7274

73-
};
74-
75-
}
75+
}
7676

7777

7878
#endif // OPENPABLO_PROCESSORFACTORY_H_

0 commit comments

Comments
 (0)