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