Skip to content

Commit

Permalink
TIKA-4216 (#1673)
Browse files Browse the repository at this point in the history
* TIKA-4216 -- Avoid checking for imagemagick if image processing is disabled

(cherry picked from commit 237e73f)
  • Loading branch information
tballison committed Mar 21, 2024
1 parent bf00061 commit fad4b8c
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public class TesseractOCRParser extends AbstractExternalProcessParser implements

})));
private static volatile boolean HAS_WARNED = false;
private static volatile boolean HAS_CHECKED_FOR_IMAGE_MAGICK = false;

//if a user specifies a custom tess path or tessdata path
//load the available languages at initialization time
private final Set<String> langs = new HashSet<>();
Expand Down Expand Up @@ -190,7 +192,10 @@ public boolean hasTesseract() throws TikaConfigException {
return hasTesseract;
}

boolean hasImageMagick() throws TikaConfigException {
synchronized boolean hasImageMagick() throws TikaConfigException {
if (HAS_CHECKED_FOR_IMAGE_MAGICK) {
return hasImageMagick;
}
// Fetch where the config says to find ImageMagick Program
String fullImageMagickPath = imageMagickPath + getImageMagickProg();

Expand All @@ -208,7 +213,7 @@ boolean hasImageMagick() throws TikaConfigException {
LOG.debug("ImageMagick does not appear to be installed " + "(commandline: " +
fullImageMagickPath + ")");
}

HAS_CHECKED_FOR_IMAGE_MAGICK = true;
return hasImageMagick;

}
Expand Down Expand Up @@ -245,6 +250,11 @@ public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
return;
}

//if you haven't checked yet, and a per file config requests imagemagick
//and if the default is not to use image processing
if (! HAS_CHECKED_FOR_IMAGE_MAGICK && config.isEnableImagePreprocessing()) {
hasImageMagick = hasImageMagick();
}

try (TemporaryResources tmp = new TemporaryResources()) {
TikaInputStream tikaStream = TikaInputStream.get(stream, tmp, metadata);
Expand Down Expand Up @@ -528,7 +538,11 @@ private Thread logStream(final InputStream stream, final StringBuilder out) {
@Override
public void initialize(Map<String, Param> params) throws TikaConfigException {
hasTesseract = hasTesseract();
hasImageMagick = hasImageMagick();
if (isEnableImagePreprocessing()) {
hasImageMagick = hasImageMagick();
} else {
hasImageMagick = false;
}
if (preloadLangs) {
preloadLangs();
if (!StringUtils.isBlank(defaultConfig.getLanguage())) {
Expand Down

0 comments on commit fad4b8c

Please sign in to comment.