X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FImageLinkFilter.java;h=7e3723ccddead0c3e0979c0562b864ab5e96feb9;hb=fd560d013c7de7b8c70b65156a41d4a6bc9aeb06;hp=9e758cfd525312fd64178f2351e589acc620a825;hpb=18ce272a6e9b0af7fa7f1874a70b9ef7ff11e592;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java
index 9e758cf..7e3723c 100644
--- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java
+++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java
@@ -1,5 +1,5 @@
/*
- * Sone - ImageLinkFilter.java - Copyright © 2011 David Roden
+ * Sone - ImageLinkFilter.java - Copyright © 2011â2013 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -21,15 +21,17 @@ 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;
import net.pterodactylus.util.template.Filter;
import net.pterodactylus.util.template.Template;
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.template.TemplateContextFactory;
import net.pterodactylus.util.template.TemplateParser;
+import com.google.common.base.Optional;
+
/**
* Template filter that turns an {@link Image} into an HTML <img> tag,
* using some parameters to influence parameters of the image.
@@ -39,7 +41,10 @@ import net.pterodactylus.util.template.TemplateParser;
public class ImageLinkFilter implements Filter {
/** The template to render for the <img> tag. */
- private static final Template linkTemplate = TemplateParser.parse(new StringReader(" 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>\"/>"));
+ private static final Template linkTemplate = TemplateParser.parse(new StringReader(" 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 +52,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;
}
@@ -58,21 +66,27 @@ public class ImageLinkFilter implements Filter {
* {@inheritDoc}
*/
@Override
- public Object format(TemplateContext templateContext, Object data, Map parameters) {
- Image image = (Image) data;
- String imageClass = parameters.get("class");
+ public Object format(TemplateContext templateContext, Object data, Map parameters) {
+ Image image = null;
+ if (data instanceof String) {
+ image = core.getImage((String) data, false);
+ } else if (data instanceof Image) {
+ image = (Image) data;
+ }
+ if (image == null) {
+ return null;
+ }
+ String imageClass = String.valueOf(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);
String mode = String.valueOf(parameters.get("mode"));
- String title = parameters.get("title");
- if ((title != null) && title.startsWith("=")) {
- title = String.valueOf(templateContext.get(title.substring(1)));
- }
+ String title = String.valueOf(parameters.get("title"));
TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
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 +96,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)) {
@@ -97,8 +106,8 @@ public class ImageLinkFilter implements Filter {
linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
}
- linkTemplateContext.set("alt", Default.forNull(title, image.getDescription()));
- linkTemplateContext.set("title", Default.forNull(title, image.getTitle()));
+ linkTemplateContext.set("alt", Optional.fromNullable(title).or(image.getDescription()));
+ linkTemplateContext.set("title", Optional.fromNullable(title).or(image.getTitle()));
StringWriter stringWriter = new StringWriter();
linkTemplate.render(linkTemplateContext, stringWriter);