Allow the ID of an image as input for the image link filter.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ImageLinkFilter.java
index 35cc140..1684be7 100644 (file)
@@ -21,6 +21,7 @@ import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.Map;
 
+import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.object.Default;
@@ -38,8 +39,11 @@ import net.pterodactylus.util.template.TemplateParser;
  */
 public class ImageLinkFilter implements Filter {
 
-       /** The template to render for the <img%gt; tag. */
-       private static final Template linkTemplate = TemplateParser.parse(new StringReader("<img<%ifnull !class> class=\"<%class|css>\"<%/if> src=\"<%src|html>\" alt=\"<%alt|html>\" title=\"<%title|html>\" width=\"<%width|html>\" height=\"<%height|html>\" style=\"position: relative;<%ifnull ! top>top: <% top|html>;<%/if><%ifnull ! left>left: <% left|html>;<%/if>\"/>"));
+       /** The template to render for the &lt;img&gt; tag. */
+       private static final Template linkTemplate = TemplateParser.parse(new StringReader("<img<%ifnull !class> class=\"<%class|css>\"<%/if> src=\"<%src|html><%if forceDownload>?forcedownload=true<%/if>\" alt=\"<%alt|html>\" title=\"<%title|html>\" width=\"<%width|html>\" height=\"<%height|html>\" style=\"position: relative;<%ifnull ! top>top: <% top|html>;<%/if><%ifnull ! left>left: <% left|html>;<%/if>\"/>"));
+
+       /** The core. */
+       private final Core core;
 
        /** The template context factory. */
        private final TemplateContextFactory templateContextFactory;
@@ -47,10 +51,13 @@ public class ImageLinkFilter implements Filter {
        /**
         * Creates a new image link filter.
         *
+        * @param core
+        *            The core
         * @param templateContextFactory
         *            The template context factory
         */
-       public ImageLinkFilter(TemplateContextFactory templateContextFactory) {
+       public ImageLinkFilter(Core core, TemplateContextFactory templateContextFactory) {
+               this.core = core;
                this.templateContextFactory = templateContextFactory;
        }
 
@@ -59,7 +66,14 @@ public class ImageLinkFilter implements Filter {
         */
        @Override
        public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
-               Image image = (Image) data;
+               Image image = null;
+               if (data instanceof String) {
+                       image = core.getImage((String) data, false);
+               } else if (data instanceof Image) {
+                       image = (Image) data;
+               } else {
+                       return null;
+               }
                String imageClass = parameters.get("class");
                int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE);
                int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE);
@@ -73,6 +87,7 @@ public class ImageLinkFilter implements Filter {
                linkTemplateContext.set("class", imageClass);
                if (image.isInserted()) {
                        linkTemplateContext.set("src", "/" + image.getKey());
+                       linkTemplateContext.set("forceDownload", true);
                } else {
                        linkTemplateContext.set("src", "getImage.html?image=" + image.getId());
                }
@@ -82,13 +97,8 @@ public class ImageLinkFilter implements Filter {
                        double scale = Math.max(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
                        linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
                        linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
-                       if (scale >= 1) {
-                               linkTemplateContext.set("left", String.format("%dpx", (int) ((imageWidth * scale) - maxWidth) / 2));
-                               linkTemplateContext.set("top", String.format("%dpx", (int) ((imageHeight * scale) - maxHeight) / 2));
-                       } else {
-                               linkTemplateContext.set("left", String.format("%dpx", (int) (maxWidth - (imageWidth * scale)) / 2));
-                               linkTemplateContext.set("top", String.format("%dpx", (int) (maxHeight - (imageHeight * scale)) / 2));
-                       }
+                       linkTemplateContext.set("left", String.format("%dpx", (int) (maxWidth - (imageWidth * scale)) / 2));
+                       linkTemplateContext.set("top", String.format("%dpx", (int) (maxHeight - (imageHeight * scale)) / 2));
                } else {
                        double scale = 1;
                        if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) {