fix #607 add ${path}

This commit is contained in:
Shinsuke Sugaya 2016-08-08 06:41:33 +09:00
parent 418dabd1c7
commit c1a6aa1ea1
4 changed files with 73 additions and 18 deletions

View file

@ -15,7 +15,9 @@
*/
package org.codelibs.fess.thumbnail.impl;
import static org.codelibs.core.stream.StreamUtil.stream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -34,7 +36,9 @@ public abstract class BaseThumbnailGenerator implements ThumbnailGenerator {
protected int directoryNameLength = 5;
public List<String> generatorList;
protected List<String> generatorList;
protected Map<String, String> filePathMap = new HashMap<>();
public void addCondition(final String key, final String regex) {
conditionMap.put(key, regex);
@ -54,7 +58,31 @@ public abstract class BaseThumbnailGenerator implements ThumbnailGenerator {
@Override
public boolean isAvailable() {
if (generatorList != null && !generatorList.isEmpty()) {
return generatorList.stream().allMatch(s -> new File(s).isFile());
String path = System.getenv("PATH");
if (path == null) {
path = System.getenv("Path");
}
if (path == null) {
path = System.getenv("path");
}
final List<String> pathList = new ArrayList<>();
pathList.add("/usr/share/fess/bin");
if (path != null) {
stream(path.split(File.pathSeparator)).of(stream -> stream.map(s -> s.trim()).forEach(s -> pathList.add(s)));
}
return generatorList.stream().map(s -> {
if (s.startsWith("${path}")) {
for (String p : pathList) {
final File f = new File(s.replace("${path}", p));
if (f.exists()) {
final String filePath = f.getAbsolutePath();
filePathMap.put(s, filePath);
return filePath;
}
}
}
return s;
}).allMatch(s -> new File(s).isFile());
}
return true;
}
@ -63,4 +91,15 @@ public abstract class BaseThumbnailGenerator implements ThumbnailGenerator {
this.directoryNameLength = directoryNameLength;
}
protected String expandPath(String value) {
if (value != null && filePathMap.containsKey(value)) {
return filePathMap.get(value);
}
return value;
}
public void setGeneratorList(List<String> generatorList) {
this.generatorList = generatorList;
}
}

View file

@ -81,7 +81,7 @@ public class CommandGenerator extends BaseThumbnailGenerator {
final String outputPath = outputFile.getAbsolutePath();
final List<String> cmdList = new ArrayList<>();
for (final String value : commandList) {
cmdList.add(value.replace("${url}", url).replace("${outputFile}", outputPath));
cmdList.add(expandPath(value.replace("${url}", url).replace("${outputFile}", outputPath)));
}
ProcessBuilder pb = null;

View file

@ -31,6 +31,7 @@ import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -52,16 +53,28 @@ public class WebDriverGenerator extends BaseThumbnailGenerator {
@PostConstruct
public void init() {
try {
if (webDriver == null) {
webDriver = webDriverCapabilities == null ? new PhantomJSDriver() : new PhantomJSDriver(webDriverCapabilities);
}
webDriver.manage().window().setSize(new Dimension(windowWidth, windowHeight));
} catch (final Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("WebDriver is not available for generating thumbnails.", e);
} else {
logger.info("WebDriver is not available for generating thumbnails.");
if (super.isAvailable()) {
try {
if (webDriver == null) {
if (webDriverCapabilities == null) {
webDriver = new PhantomJSDriver();
} else {
if (webDriverCapabilities instanceof DesiredCapabilities) {
DesiredCapabilities capabilities = (DesiredCapabilities) webDriverCapabilities;
webDriverCapabilities.asMap().entrySet().stream()
.filter(e -> e.getValue() instanceof String && filePathMap.containsKey(e.getValue().toString()))
.forEach(e -> capabilities.setCapability(e.getKey(), filePathMap.get(e.getValue().toString())));
}
webDriver = new PhantomJSDriver(webDriverCapabilities);
}
}
webDriver.manage().window().setSize(new Dimension(windowWidth, windowHeight));
} catch (final Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("WebDriver is not available for generating thumbnails.", e);
} else {
logger.info("WebDriver is not available for generating thumbnails.");
}
}
}
}

View file

@ -11,11 +11,14 @@
</postConstruct>
</component>
<component name="htmlThumbnailGenerator" class="org.codelibs.fess.thumbnail.impl.WebDriverGenerator">
<property name="generatorList">
["${path}/phantomjs"]
</property>
<property name="webDriverCapabilities">
<component class="org.openqa.selenium.remote.DesiredCapabilities">
<postConstruct name="setCapability">
<arg>"phantomjs.binary.path"</arg>
<arg>"/usr/bin/phantomjs"</arg>
<arg>"${path}/phantomjs"</arg>
</postConstruct>
</component>
</property>
@ -27,13 +30,13 @@
<!--
<component name="htmlThumbnailGenerator" class="org.codelibs.fess.thumbnail.impl.CommandGenerator">
<property name="commandList">
["/usr/share/fess/bin/generate-thumbnail",
["${path}/generate-thumbnail",
"html",
"${url}",
"${outputFile}"]
</property>
<property name="generatorList">
["/usr/share/fess/bin/generate-thumbnail"]
["${path}/generate-thumbnail"]
</property>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
@ -43,13 +46,13 @@
-->
<component name="msofficeThumbnailGenerator" class="org.codelibs.fess.thumbnail.impl.CommandGenerator">
<property name="commandList">
["/usr/share/fess/bin/generate-thumbnail",
["${path}/generate-thumbnail",
"msoffice",
"${url}",
"${outputFile}"]
</property>
<property name="generatorList">
["/usr/share/fess/bin/generate-thumbnail"]
["${path}/generate-thumbnail"]
</property>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>