fix #1087 create thumbnails at indexing time

This commit is contained in:
Shinsuke Sugaya 2017-06-04 17:15:54 +09:00
parent eb36d94d27
commit c84ebd0f45
6 changed files with 54 additions and 60 deletions

View file

@ -714,12 +714,10 @@ public class FessXpathTransformer extends XpathTransformer implements FessTransf
for (int i = 0; i < imgNodeList.getLength(); i++) {
final Node imgNode = imgNodeList.item(i);
final NamedNodeMap attributes = imgNode.getAttributes();
final Node heightAttr = attributes.getNamedItem("height");
final Node widthAttr = attributes.getNamedItem("width");
if (heightAttr != null && widthAttr != null) {
final Integer height = getAttributeAsInteger(attributes, "height");
final Integer width = getAttributeAsInteger(attributes, "width");
if (height != null && width != null) {
try {
final int height = Integer.parseInt(heightAttr.getTextContent());
final int width = Integer.parseInt(widthAttr.getTextContent());
if (fessConfig.validateThumbnailSize(width, height)) {
final Node srcNode = attributes.getNamedItem("src");
if (srcNode != null) {
@ -756,6 +754,25 @@ public class FessXpathTransformer extends XpathTransformer implements FessTransf
return null;
}
protected Integer getAttributeAsInteger(final NamedNodeMap attributes, final String name) {
Node namedItem = attributes.getNamedItem(name);
if (namedItem == null) {
return null;
}
final String value = namedItem.getTextContent();
if (value == null) {
return null;
}
try {
return Integer.parseInt(value);
} catch (final NumberFormatException e) {
if (value.endsWith("%") || value.endsWith("px")) {
return null;
}
return 0;
}
}
protected URL getURL(final String currentUrl, final String url) throws MalformedURLException {
if (url != null) {
if (url.startsWith("://")) {

View file

@ -771,7 +771,7 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
/** The key of the configuration. e.g. all */
String THUMBNAIL_GENERATOR_TARGETS = "thumbnail.generator.targets";
/** The key of the configuration. e.g. false */
/** The key of the configuration. e.g. true */
String THUMBNAIL_CRAWLER_ENABLED = "thumbnail.crawler.enabled";
/** The key of the configuration. e.g. userCode */
@ -3780,14 +3780,14 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
/**
* Get the value for the key 'thumbnail.crawler.enabled'. <br>
* The value is, e.g. false <br>
* The value is, e.g. true <br>
* @return The value of found property. (NotNull: if not found, exception but basically no way)
*/
String getThumbnailCrawlerEnabled();
/**
* Is the property for the key 'thumbnail.crawler.enabled' true? <br>
* The value is, e.g. false <br>
* The value is, e.g. true <br>
* @return The determination, true or false. (if not found, exception but basically no way)
*/
boolean isThumbnailCrawlerEnabled();

View file

@ -79,18 +79,29 @@ public class HtmlTagBasedGenerator extends BaseThumbnailGenerator {
}
Curl.get(url).execute(con -> {
boolean created = false;
try (ImageInputStream input = ImageIO.createImageInputStream(con.getInputStream())) {
saveImage(input, outputFile);
if (saveImage(input, outputFile)) {
created = true;
} else {
logger.warn("Failed to create a thumbnail for " + url);
}
} catch (final Throwable t) {
logger.warn("Failed to convert " + url, t);
updateThumbnailField(thumbnailId, url, StringUtil.EMPTY);
logger.warn("Failed to create a thumbnail for " + url, t);
} finally {
if (!created) {
updateThumbnailField(thumbnailId, url, StringUtil.EMPTY);
if (outputFile.exists() && !outputFile.delete()) {
logger.warn("Failed to delete " + outputFile.getAbsolutePath());
}
}
}
});
return false;
return outputFile.exists();
}
protected void saveImage(final ImageInputStream input, final File outputFile) throws IOException {
protected boolean saveImage(final ImageInputStream input, final File outputFile) throws IOException {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
final Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (readers.hasNext()) {
@ -114,10 +125,12 @@ public class HtmlTagBasedGenerator extends BaseThumbnailGenerator {
0, thumbnailWidth, thumbnailHeight, null);
ImageIO.write(thumbnail, fessConfig.getThumbnailHtmlImageFormat(), outputFile);
image.flush();
return true;
} finally {
reader.dispose();
}
}
return false;
}
}

View file

@ -393,7 +393,7 @@ thumbnail.html.image.thumbnail.width=100
thumbnail.html.image.thumbnail.height=100
thumbnail.html.image.format=png
thumbnail.generator.targets=all
thumbnail.crawler.enabled=false
thumbnail.crawler.enabled=true
# user
user.code.request.parameter=userCode

View file

@ -6,57 +6,13 @@
<postConstruct name="add">
<arg>htmlThumbnailGenerator</arg>
</postConstruct>
<postConstruct name="add">
<arg>msofficeThumbnailGenerator</arg>
</postConstruct>
</component>
<component name="htmlThumbnailGenerator" class="org.codelibs.fess.thumbnail.impl.EmptyGenerator">
<component name="htmlThumbnailGenerator" class="org.codelibs.fess.thumbnail.impl.HtmlTagBasedGenerator">
<property name="name">"htmlThumbnailGenerator"</property>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
<arg>"text/html"</arg>
</postConstruct>
</component>
<component name="msofficeThumbnailGenerator" class="org.codelibs.fess.thumbnail.impl.EmptyGenerator">
<property name="name">"msofficeThumbnailGenerator"</property>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
<arg>"text/html"</arg>
</postConstruct>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
<arg>"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
</arg>
</postConstruct>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
<arg>"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
</arg>
</postConstruct>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
<arg>"application/vnd.openxmlformats-officedocument.presentationml.presentation"
</arg>
</postConstruct>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
<arg>"application/msword"
</arg>
</postConstruct>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
<arg>"application/vnd.ms-excel"
</arg>
</postConstruct>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
<arg>"application/vnd.ms-powerpoint"
</arg>
</postConstruct>
<postConstruct name="addCondition">
<arg>"mimetype"</arg>
<arg>"application/rtf"
</arg>
</postConstruct>
</component>
<!-- TODO remove ServletContext from CommandGenerator -->
</components>

View file

@ -706,6 +706,14 @@ public class FessXpathTransformerTest extends UnitFessTestCase {
data = "<img src=\"http://example/foo.jpg\" width=\"100\" height=\"100\">";
expected = "http://example/foo.jpg";
assertGetThumbnailUrl(data, expected);
data = "<img src=\"http://example/foo.jpg\" width=\"100%\" height=\"100%\">";
expected = "http://example/foo.jpg";
assertGetThumbnailUrl(data, expected);
data = "<img src=\"http://example/foo.jpg\" width=\"100px\" height=\"100px\">";
expected = "http://example/foo.jpg";
assertGetThumbnailUrl(data, expected);
}
private void assertGetThumbnailUrl(String data, String expected) throws Exception {