19 package org.sleuthkit.autopsy.textextractors;
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.io.CharSource;
23 import com.google.common.util.concurrent.ThreadFactoryBuilder;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.PushbackReader;
30 import java.io.Reader;
31 import java.nio.file.Paths;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Objects;
36 import java.util.concurrent.Callable;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.Executors;
39 import java.util.concurrent.Future;
40 import java.util.concurrent.ThreadFactory;
41 import java.util.concurrent.TimeUnit;
42 import java.util.concurrent.TimeoutException;
43 import java.util.logging.Level;
44 import java.util.stream.Collectors;
45 import org.apache.tika.Tika;
46 import org.apache.tika.exception.TikaException;
47 import org.apache.tika.metadata.Metadata;
48 import org.apache.tika.parser.AutoDetectParser;
49 import org.apache.tika.parser.EmptyParser;
50 import org.apache.tika.parser.ParseContext;
51 import org.apache.tika.parser.Parser;
52 import org.apache.tika.parser.ParsingReader;
53 import org.apache.tika.parser.microsoft.OfficeParserConfig;
54 import org.apache.tika.parser.ocr.TesseractOCRConfig;
55 import org.apache.tika.parser.pdf.PDFParserConfig;
56 import org.openide.util.NbBundle;
57 import org.openide.modules.InstalledFileLocator;
58 import org.openide.util.Lookup;
71 import org.xml.sax.ContentHandler;
72 import org.xml.sax.SAXException;
73 import org.xml.sax.helpers.DefaultHandler;
74 import com.google.common.collect.ImmutableMap;
80 final class TikaTextExtractor
implements TextExtractor {
84 private static final List<String> BINARY_MIME_TYPES
87 "application/octet-stream",
88 "application/x-msdownload");
94 private static final List<String> ARCHIVE_MIME_TYPES
97 "application/x-7z-compressed",
98 "application/x-ace-compressed",
99 "application/x-alz-compressed",
101 "application/vnd.ms-cab-compressed",
102 "application/x-cfs-compressed",
103 "application/x-dgc-compressed",
104 "application/x-apple-diskimage",
105 "application/x-gca-compressed",
109 "application/x-rar-compressed",
110 "application/x-stuffit",
111 "application/x-stuffitx",
112 "application/x-gtar",
113 "application/x-archive",
114 "application/x-executable",
115 "application/x-gzip",
118 "application/x-cpio",
119 "application/x-shar",
121 "application/x-bzip",
122 "application/x-bzip2",
123 "application/x-lzip",
124 "application/x-lzma",
125 "application/x-lzop",
127 "application/x-compress");
130 private static final List<String> EMBEDDED_FILE_MIME_TYPES
131 = ImmutableList.of(
"application/msword",
132 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
133 "application/vnd.ms-powerpoint",
134 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
135 "application/vnd.ms-excel",
136 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
139 private static final java.util.logging.Logger TIKA_LOGGER = java.util.logging.Logger.getLogger(
"Tika");
140 private static final Logger AUTOPSY_LOGGER = Logger.getLogger(TikaTextExtractor.class.getName());
142 private final ThreadFactory tikaThreadFactory
143 =
new ThreadFactoryBuilder().setNameFormat(
"tika-reader-%d").build();
144 private final ExecutorService executorService = Executors.newSingleThreadExecutor(tikaThreadFactory);
145 private static final String SQLITE_MIMETYPE =
"application/x-sqlite3";
147 private final AutoDetectParser parser =
new AutoDetectParser();
148 private final Content content;
150 private boolean tesseractOCREnabled;
151 private static final String TESSERACT_DIR_NAME =
"Tesseract-OCR";
152 private static final String TESSERACT_EXECUTABLE =
"tesseract.exe";
153 private static final File TESSERACT_PATH = locateTesseractExecutable();
154 private String languagePacks = formatLanguagePacks(PlatformUtil.getOcrLanguagePacks());
155 private static final String TESSERACT_OUTPUT_FILE_NAME =
"tess_output";
156 private Map<String, String> metadataMap;
158 private ProcessTerminator processTerminator;
160 private static final List<String> TIKA_SUPPORTED_TYPES
161 =
new Tika().getParser().getSupportedTypes(
new ParseContext())
163 .map(mt -> mt.getType() +
"/" + mt.getSubtype())
164 .collect(Collectors.toList());
166 public TikaTextExtractor(Content content) {
167 this.content = content;
177 private boolean ocrEnabled() {
178 return TESSERACT_PATH != null && tesseractOCREnabled
179 && PlatformUtil.isWindowsOS() ==
true && PlatformUtil.is64BitOS();
194 public Reader getReader() throws InitReaderException {
195 InputStream stream = null;
197 ParseContext parseContext =
new ParseContext();
201 if(content instanceof AbstractFile && EMBEDDED_FILE_MIME_TYPES.contains(((AbstractFile)content).getMIMEType())) {
202 parseContext.set(Parser.class,
new EmptyParser());
204 parseContext.set(Parser.class, parser);
207 if (ocrEnabled() && content instanceof AbstractFile) {
208 AbstractFile file = ((AbstractFile) content);
210 if (file.getMIMEType().toLowerCase().startsWith(
"image/")) {
211 stream = performOCR(file);
215 PDFParserConfig pdfConfig =
new PDFParserConfig();
220 pdfConfig.setExtractInlineImages(
true);
222 pdfConfig.setExtractUniqueInlineImagesOnly(
true);
223 parseContext.set(PDFParserConfig.class, pdfConfig);
226 TesseractOCRConfig ocrConfig =
new TesseractOCRConfig();
227 String tesseractFolder = TESSERACT_PATH.getParent();
228 ocrConfig.setTesseractPath(tesseractFolder);
230 ocrConfig.setLanguage(languagePacks);
231 ocrConfig.setTessdataPath(PlatformUtil.getOcrLanguagePacksPath());
232 parseContext.set(TesseractOCRConfig.class, ocrConfig);
234 stream =
new ReadContentInputStream(content);
237 stream =
new ReadContentInputStream(content);
240 Metadata metadata =
new Metadata();
243 OfficeParserConfig officeParserConfig =
new OfficeParserConfig();
244 officeParserConfig.setUseSAXPptxExtractor(
true);
245 officeParserConfig.setUseSAXDocxExtractor(
true);
246 parseContext.set(OfficeParserConfig.class, officeParserConfig);
249 Future<Reader> future = executorService.submit(
250 new GetTikaReader(parser, stream, metadata, parseContext));
252 final Reader tikaReader = future.get(getTimeout(content.getSize()), TimeUnit.SECONDS);
254 PushbackReader pushbackReader =
new PushbackReader(tikaReader);
255 int read = pushbackReader.read();
257 throw new InitReaderException(
"Unable to extract text: "
258 +
"Tika returned empty reader for " + content);
260 pushbackReader.unread(read);
263 if (metadataMap == null) {
264 metadataMap =
new HashMap<>();
265 for (String mtdtKey : metadata.names()) {
266 metadataMap.put(mtdtKey, metadata.get(mtdtKey));
270 return new ReaderCharSource(pushbackReader).openStream();
271 }
catch (TimeoutException te) {
272 final String msg = NbBundle.getMessage(this.getClass(),
273 "AbstractFileTikaTextExtract.index.tikaParseTimeout.text",
274 content.getId(), content.getName());
275 throw new InitReaderException(msg, te);
276 }
catch (InitReaderException ex) {
278 }
catch (Exception ex) {
279 AUTOPSY_LOGGER.log(Level.WARNING, String.format(
"Error with file [id=%d] %s, see Tika log for details...",
280 content.getId(), content.getName()));
281 TIKA_LOGGER.log(Level.WARNING,
"Exception: Unable to Tika parse the "
282 +
"content" + content.getId() +
": " + content.getName(),
284 final String msg = NbBundle.getMessage(this.getClass(),
285 "AbstractFileTikaTextExtract.index.exception.tikaParse.msg",
286 content.getId(), content.getName());
287 throw new InitReaderException(msg, ex);
303 private InputStream performOCR(AbstractFile file)
throws InitReaderException {
304 File inputFile = null;
305 File outputFile = null;
307 String tempDirectory = Case.getCurrentCaseThrows().getTempDirectory();
310 String tempFileName = FileUtil.escapeFileName(file.getId() + file.getName());
311 inputFile = Paths.get(tempDirectory, tempFileName).toFile();
312 ContentUtils.writeToFile(content, inputFile);
314 String tempOutputName = FileUtil.escapeFileName(file.getId() + TESSERACT_OUTPUT_FILE_NAME);
315 String outputFilePath = Paths.get(tempDirectory, tempOutputName).toString();
316 String executeablePath = TESSERACT_PATH.toString();
319 ProcessBuilder process =
new ProcessBuilder();
320 process.command(executeablePath,
321 String.format(
"\"%s\"", inputFile.getAbsolutePath()),
322 String.format(
"\"%s\"", outputFilePath),
323 "--tessdata-dir", PlatformUtil.getOcrLanguagePacksPath(),
325 "-l", languagePacks);
329 if (processTerminator != null) {
330 ExecUtil.execute(process, 1, TimeUnit.SECONDS, processTerminator);
332 ExecUtil.execute(process);
335 outputFile =
new File(outputFilePath +
".txt");
337 return new CleanUpStream(outputFile);
338 }
catch (NoCurrentCaseException | IOException ex) {
339 if (outputFile != null) {
342 throw new InitReaderException(
"Could not successfully run Tesseract", ex);
344 if (inputFile != null) {
362 Metadata metadata, ParseContext parseContext) {
370 public Reader
call() throws Exception {
371 return new ParsingReader(parser, stream, metadata, parseContext);
402 public void close() throws IOException {
419 private static File locateTesseractExecutable() {
424 String executableToFindName = Paths.get(TESSERACT_DIR_NAME, TESSERACT_EXECUTABLE).toString();
425 File exeFile = InstalledFileLocator.getDefault().locate(executableToFindName, TikaTextExtractor.class.getPackage().getName(),
false);
426 if (null == exeFile) {
430 if (!exeFile.canExecute()) {
443 public Map<String, String> getMetadata() {
444 if (metadataMap != null) {
445 return ImmutableMap.copyOf(metadataMap);
449 metadataMap =
new HashMap<>();
450 InputStream stream =
new ReadContentInputStream(content);
451 ContentHandler doNothingContentHandler =
new DefaultHandler();
452 Metadata mtdt =
new Metadata();
453 parser.parse(stream, doNothingContentHandler, mtdt);
454 for (String mtdtKey : mtdt.names()) {
455 metadataMap.put(mtdtKey, mtdt.get(mtdtKey));
457 }
catch (IOException | SAXException | TikaException ex) {
458 AUTOPSY_LOGGER.log(Level.WARNING, String.format(
"Error getting metadata for file [id=%d] %s, see Tika log for details...",
459 content.getId(), content.getName()));
460 TIKA_LOGGER.log(Level.WARNING,
"Exception: Unable to get metadata for "
461 +
"content" + content.getId() +
": " + content.getName(), ex);
473 public boolean isSupported() {
474 if (!(content instanceof AbstractFile)) {
478 String detectedType = ((AbstractFile) content).getMIMEType();
479 if (detectedType == null
480 || BINARY_MIME_TYPES.contains(detectedType)
481 || ARCHIVE_MIME_TYPES.contains(detectedType)
482 || (detectedType.startsWith(
"video/") && !detectedType.equals(
"video/x-flv"))
483 || detectedType.equals(SQLITE_MIMETYPE)
488 return TIKA_SUPPORTED_TYPES.contains(detectedType);
496 private static String formatLanguagePacks(List<String> languagePacks) {
497 return String.join(
"+", languagePacks);
507 private static int getTimeout(
long size) {
508 if (size < 1024 * 1024L)
511 }
else if (size < 10 * 1024 * 1024L)
514 }
else if (size < 100 * 1024 * 1024L)
533 public void setExtractionSettings(Lookup context) {
534 if (context != null) {
535 ImageConfig configInstance = context.lookup(ImageConfig.class);
536 if (configInstance != null) {
537 if (Objects.nonNull(configInstance.getOCREnabled())) {
538 this.tesseractOCREnabled = configInstance.getOCREnabled();
541 if (Objects.nonNull(configInstance.getOCRLanguages())) {
542 this.languagePacks = formatLanguagePacks(configInstance.getOCRLanguages());
546 ProcessTerminator terminatorInstance = context.lookup(ProcessTerminator.class);
547 if (terminatorInstance != null) {
548 this.processTerminator = terminatorInstance;