React correctly when parameters are not given.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ImageLinkFilter.java
1 /*
2  * Sone - ImageLinkFilter.java - Copyright © 2011–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import java.io.StringReader;
21 import java.io.StringWriter;
22 import java.util.Map;
23
24 import net.pterodactylus.sone.core.Core;
25 import net.pterodactylus.sone.data.Image;
26 import net.pterodactylus.util.number.Numbers;
27 import net.pterodactylus.util.template.Filter;
28 import net.pterodactylus.util.template.Template;
29 import net.pterodactylus.util.template.TemplateContext;
30 import net.pterodactylus.util.template.TemplateContextFactory;
31 import net.pterodactylus.util.template.TemplateParser;
32
33 import com.google.common.base.Optional;
34
35 /**
36  * Template filter that turns an {@link Image} into an HTML &lt;img&gt; tag,
37  * using some parameters to influence parameters of the image.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class ImageLinkFilter implements Filter {
42
43         /** The template to render for the &lt;img&gt; tag. */
44         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>\"/>"));
45
46         /** The core. */
47         private final Core core;
48
49         /** The template context factory. */
50         private final TemplateContextFactory templateContextFactory;
51
52         /**
53          * Creates a new image link filter.
54          *
55          * @param core
56          *            The core
57          * @param templateContextFactory
58          *            The template context factory
59          */
60         public ImageLinkFilter(Core core, TemplateContextFactory templateContextFactory) {
61                 this.core = core;
62                 this.templateContextFactory = templateContextFactory;
63         }
64
65         @Override
66         public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
67                 Image image = null;
68                 if (data instanceof String) {
69                         image = core.getImage((String) data).orNull();
70                 } else if (data instanceof Image) {
71                         image = (Image) data;
72                 }
73                 if (image == null) {
74                         return null;
75                 }
76                 String imageClass = getParameterAsString(parameters, "class");
77                 int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE);
78                 int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE);
79                 String mode = getParameterAsString(parameters, "mode");
80                 String title = getParameterAsString(parameters, "title");
81
82                 TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
83                 linkTemplateContext.set("class", imageClass);
84                 if (image.isInserted()) {
85                         linkTemplateContext.set("src", "/" + image.getKey());
86                         linkTemplateContext.set("forceDownload", true);
87                 } else {
88                         linkTemplateContext.set("src", "getImage.html?image=" + image.getId());
89                 }
90                 int imageWidth = image.getWidth();
91                 int imageHeight = image.getHeight();
92                 if ("enlarge".equals(mode)) {
93                         double scale = Math.max(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
94                         linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
95                         linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
96                         linkTemplateContext.set("left", String.format("%dpx", (int) (maxWidth - (imageWidth * scale)) / 2));
97                         linkTemplateContext.set("top", String.format("%dpx", (int) (maxHeight - (imageHeight * scale)) / 2));
98                 } else {
99                         double scale = 1;
100                         if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) {
101                                 scale = Math.min(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
102                         }
103                         linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
104                         linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
105                 }
106                 linkTemplateContext.set("alt", Optional.fromNullable(title).or(image.getDescription()));
107                 linkTemplateContext.set("title", Optional.fromNullable(title).or(image.getTitle()));
108
109                 StringWriter stringWriter = new StringWriter();
110                 linkTemplate.render(linkTemplateContext, stringWriter);
111                 return stringWriter.toString();
112         }
113
114         private static String getParameterAsString(Map<String, Object> parameters, String key) {
115                 Object parameter = parameters.get(key);
116                 return (parameter == null) ? null : String.valueOf(parameter);
117         }
118
119 }