瀏覽代碼

fix #1087 create thumbnails at indexing time

Shinsuke Sugaya 8 年之前
父節點
當前提交
c84ebd0f45

+ 22 - 5
src/main/java/org/codelibs/fess/crawler/transformer/FessXpathTransformer.java

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

+ 3 - 3
src/main/java/org/codelibs/fess/mylasta/direction/FessConfig.java

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

+ 18 - 5
src/main/java/org/codelibs/fess/thumbnail/impl/HtmlTagBasedGenerator.java

@@ -79,18 +79,29 @@ public class HtmlTagBasedGenerator extends BaseThumbnailGenerator {
         }
         }
 
 
         Curl.get(url).execute(con -> {
         Curl.get(url).execute(con -> {
+            boolean created = false;
             try (ImageInputStream input = ImageIO.createImageInputStream(con.getInputStream())) {
             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) {
             } 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 FessConfig fessConfig = ComponentUtil.getFessConfig();
         final Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
         final Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
         if (readers.hasNext()) {
         if (readers.hasNext()) {
@@ -114,10 +125,12 @@ public class HtmlTagBasedGenerator extends BaseThumbnailGenerator {
                         0, thumbnailWidth, thumbnailHeight, null);
                         0, thumbnailWidth, thumbnailHeight, null);
                 ImageIO.write(thumbnail, fessConfig.getThumbnailHtmlImageFormat(), outputFile);
                 ImageIO.write(thumbnail, fessConfig.getThumbnailHtmlImageFormat(), outputFile);
                 image.flush();
                 image.flush();
+                return true;
             } finally {
             } finally {
                 reader.dispose();
                 reader.dispose();
             }
             }
         }
         }
+        return false;
     }
     }
 
 
 }
 }

+ 1 - 1
src/main/resources/fess_config.properties

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

+ 2 - 46
src/main/webapp/WEB-INF/crawler/resources/crawler_thumbnail.xml

@@ -6,57 +6,13 @@
 		<postConstruct name="add">
 		<postConstruct name="add">
 			<arg>htmlThumbnailGenerator</arg>
 			<arg>htmlThumbnailGenerator</arg>
 		</postConstruct>
 		</postConstruct>
-		<postConstruct name="add">
-			<arg>msofficeThumbnailGenerator</arg>
-		</postConstruct>
 	</component>
 	</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>
 		<property name="name">"htmlThumbnailGenerator"</property>
 		<postConstruct name="addCondition">
 		<postConstruct name="addCondition">
 			<arg>"mimetype"</arg>
 			<arg>"mimetype"</arg>
 			<arg>"text/html"</arg>
 			<arg>"text/html"</arg>
 		</postConstruct>
 		</postConstruct>
 	</component>
 	</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>
 </components>

+ 8 - 0
src/test/java/org/codelibs/fess/crawler/transformer/FessXpathTransformerTest.java

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