X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FImageLinkFilter.java;h=4742ae35a07d17ec001e98d9504094f730c113a2;hp=9ede85143caf60f0c99700e5fbe62e278fc8d0e2;hb=62573c314957b1851f4fbe693b8746686caa940a;hpb=f6d9b5d748432a6765e508447b2cc954bd45d807 diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java index 9ede851..4742ae3 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–2013 David Roden + * Sone - ImageLinkFilter.java - Copyright © 2011–2016 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 @@ -33,18 +33,17 @@ import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.template.TemplateContextFactory; import net.pterodactylus.util.template.TemplateParser; +import com.google.common.base.Function; 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. - * - * @author David ‘Bombe’ Roden */ 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><%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>\"/>")); + 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>\"/>")); /** The core. */ private final Core core; @@ -79,17 +78,16 @@ public class ImageLinkFilter implements Filter { if (image == null) { return null; } - String imageClass = valueOf(parameters.get("class")); + String imageClass = Optional.fromNullable(parameters.get("class")).transform(getStringValue()).orNull(); int maxWidth = parseInt(valueOf(parameters.get("max-width")), MAX_VALUE); int maxHeight = parseInt(valueOf(parameters.get("max-height")), MAX_VALUE); String mode = valueOf(parameters.get("mode")); - String title = valueOf(parameters.get("title")); + String title = Optional.fromNullable(parameters.get("title")).transform(getStringValue()).orNull(); 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()); } @@ -117,4 +115,13 @@ public class ImageLinkFilter implements Filter { return stringWriter.toString(); } + private Function getStringValue() { + return new Function() { + @Override + public String apply(Object input) { + return (input != null) ? input.toString() : null; + } + }; + } + }