📄 Update year in file headers
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ImageLinkFilter.java
1 /*
2  * Sone - ImageLinkFilter.java - Copyright Â© 2011–2020 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 static java.lang.Integer.MAX_VALUE;
21 import static java.lang.String.valueOf;
22 import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
23
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.util.Map;
27
28 import net.pterodactylus.sone.core.Core;
29 import net.pterodactylus.sone.data.Image;
30 import net.pterodactylus.util.template.Filter;
31 import net.pterodactylus.util.template.HtmlFilter;
32 import net.pterodactylus.util.template.Template;
33 import net.pterodactylus.util.template.TemplateContext;
34 import net.pterodactylus.util.template.TemplateContextFactory;
35 import net.pterodactylus.util.template.TemplateParser;
36
37 import com.google.common.base.Function;
38 import com.google.common.base.Optional;
39
40 /**
41  * Template filter that turns an {@link Image} into an HTML &lt;img&gt; tag,
42  * using some parameters to influence parameters of the image.
43  */
44 public class ImageLinkFilter implements Filter {
45
46         /** The template to render for the &lt;img&gt; tag. */
47         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>\"/>"));
48
49         /** The core. */
50         private final Core core;
51
52         /** The template context factory. */
53         private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
54
55         public ImageLinkFilter(Core core) {
56                 this.core = core;
57                 templateContextFactory.addFilter("html", new HtmlFilter());
58                 templateContextFactory.addFilter("css", new CssClassNameFilter());
59         }
60
61         /**
62          * {@inheritDoc}
63          */
64         @Override
65         public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
66                 Image image = null;
67                 if (data instanceof String) {
68                         image = core.getImage((String) data, false);
69                 } else if (data instanceof Image) {
70                         image = (Image) data;
71                 }
72                 if (image == null) {
73                         return null;
74                 }
75                 String imageClass = Optional.fromNullable(parameters.get("class")).transform(getStringValue()).orNull();
76                 int maxWidth = parseInt(valueOf(parameters.get("max-width")), MAX_VALUE);
77                 int maxHeight = parseInt(valueOf(parameters.get("max-height")), MAX_VALUE);
78                 String mode = valueOf(parameters.get("mode"));
79                 String title = Optional.fromNullable(parameters.get("title")).transform(getStringValue()).orNull();
80
81                 TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
82                 linkTemplateContext.set("class", imageClass);
83                 if (image.isInserted()) {
84                         linkTemplateContext.set("src", "/" + image.getKey());
85                 } else {
86                         linkTemplateContext.set("src", "getImage.html?image=" + image.getId());
87                 }
88                 int imageWidth = image.getWidth();
89                 int imageHeight = image.getHeight();
90                 if ("enlarge".equals(mode)) {
91                         double scale = Math.max(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
92                         linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
93                         linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
94                         linkTemplateContext.set("left", String.format("%dpx", (int) (maxWidth - (imageWidth * scale)) / 2));
95                         linkTemplateContext.set("top", String.format("%dpx", (int) (maxHeight - (imageHeight * scale)) / 2));
96                 } else {
97                         double scale = 1;
98                         if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) {
99                                 scale = Math.min(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
100                         }
101                         linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
102                         linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
103                 }
104                 linkTemplateContext.set("alt", Optional.fromNullable(title).or(image.getDescription()));
105                 linkTemplateContext.set("title", Optional.fromNullable(title).or(image.getTitle()));
106
107                 StringWriter stringWriter = new StringWriter();
108                 linkTemplate.render(linkTemplateContext, stringWriter);
109                 return stringWriter.toString();
110         }
111
112         private Function<Object, String> getStringValue() {
113                 return new Function<Object, String>() {
114                         @Override
115                         public String apply(Object input) {
116                                 return (input != null) ? input.toString() : null;
117                         }
118                 };
119         }
120
121 }