Skip to content

Commit 0cf521f

Browse files
committed
Preview page working
1 parent 3ab49ff commit 0cf521f

File tree

16 files changed

+230
-142
lines changed

16 files changed

+230
-142
lines changed
Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
package org.riskfirst.tweetprint.builder;
22

3+
import java.io.IOException;
34
import java.util.Base64;
45

56
import org.springframework.http.HttpStatus;
67
import org.springframework.stereotype.Controller;
78
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
810
import org.springframework.web.bind.annotation.PostMapping;
911
import org.springframework.web.bind.annotation.RequestParam;
1012
import org.springframework.web.server.ResponseStatusException;
1113
import org.springframework.web.servlet.ModelAndView;
14+
import org.springframework.web.servlet.view.RedirectView;
1215

16+
import com.fasterxml.jackson.core.JsonParseException;
17+
import com.fasterxml.jackson.core.JsonProcessingException;
18+
import com.fasterxml.jackson.databind.JsonMappingException;
1319
import com.fasterxml.jackson.databind.ObjectMapper;
1420

1521

1622
@Controller
1723
public class BuilderController {
1824

19-
ObjectMapper om = new ObjectMapper();
2025

2126

2227
@GetMapping("/loadTweet")
@@ -34,25 +39,48 @@ public ModelAndView loadTweet(@RequestParam("url") String url) {
3439
return out;
3540
}
3641

42+
@GetMapping("/preview/{data}")
43+
public ModelAndView previewGet(@PathVariable("data") String data) throws Exception {
44+
OrderDetails order = decodeOrder(data);
45+
ModelAndView out = new ModelAndView("builder/preview");
46+
out.addObject("order", order);
47+
out.addObject("data", data);
48+
return out;
49+
}
50+
51+
private static ObjectMapper ORDER_MAPPER = new ObjectMapper();
52+
53+
public static OrderDetails decodeOrder(String data) throws IOException, JsonParseException, JsonMappingException {
54+
byte[] base64 = Base64.getDecoder().decode(data);
55+
OrderDetails order = ORDER_MAPPER.readValue(base64, OrderDetails.class);
56+
return order;
57+
}
58+
59+
public static String encodeOrder(OrderDetails od) throws JsonProcessingException {
60+
String base64 = Base64.getEncoder().encodeToString(ORDER_MAPPER.writeValueAsBytes(od));
61+
return base64;
62+
}
3763

38-
@PostMapping("/addressPreview")
39-
public ModelAndView loadTweet(
64+
65+
/**
66+
* Handles the post from the format page, and turns it into a preview url
67+
*/
68+
@PostMapping("/preview")
69+
public RedirectView previewPost(
4070
@RequestParam("tweetId") long tweetId,
41-
@RequestParam("cardType") CardType cardType,
71+
@RequestParam("type") CardType cardType,
4272
@RequestParam("style") Style style,
43-
@RequestParam("font") Font font,
4473
@RequestParam("arrangement") Arrangement arrangement,
4574
@RequestParam(required = false, name="response") boolean responseTweet,
46-
@RequestParam("message") String message) throws Exception {
47-
OrderDetails od = new OrderDetails(tweetId, cardType, style, arrangement, responseTweet, message, font);
48-
ModelAndView out = new ModelAndView("preview");
49-
out.addObject("tweetUrl", "http://robs-pro:8080/render-png?tweetId=1496845845404479490");
50-
String base64 = Base64.getEncoder().encodeToString(om.writeValueAsBytes(od));
51-
out.addObject("orderDetails", base64);
52-
return out;
75+
@RequestParam("message") String message,
76+
@RequestParam(defaultValue = OrderDetails.PLACEHOLDER_ADDRESS, name="address") String address,
77+
@RequestParam("font") Font font) throws Exception {
78+
OrderDetails od = new OrderDetails(tweetId, cardType, style, arrangement, responseTweet, message, address, font);
79+
String base64 = encodeOrder(od);
80+
return new RedirectView("/preview/"+base64);
5381
}
54-
5582

83+
5684
private long extractTweetId(String url) {
5785
if (url.contains("/status/")) {
5886
url = url.substring(url.lastIndexOf("/status/")+8);
@@ -66,7 +94,4 @@ private long extractTweetId(String url) {
6694
return tweetId;
6795
}
6896

69-
70-
71-
7297
}

tweetprint/src/main/java/org/riskfirst/tweetprint/builder/CardType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
public enum CardType {
44

55

6-
POST_CARD("CLASSIC-POST-GLOS-6X4", 6f/4f, "noun-postcard-1130482.svg", "Post Card", 3588 / 2, 1287),
7-
GREETINGS_CARD("GLOBAL-GRE-MOH-7X5-DIR", 6f/5f, "noun-greeting-card-4384989.svg", "Greetings Card", 6118, 2161);
6+
POSTCARD("CLASSIC-POST-GLOS-6X4", 6f/4f, "noun-postcard-1130482.svg", "Post Card", 3588 / 2, 1287),
7+
GREETINGSCARD("GLOBAL-GRE-MOH-7X5-DIR", 6f/5f, "noun-greeting-card-4384989.svg", "Greetings Card", 6118, 2161);
88

99
public final String sku;
1010
public final float ratio;

tweetprint/src/main/java/org/riskfirst/tweetprint/builder/OrderDetails.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@
66
@JsonAutoDetect(fieldVisibility = Visibility.PUBLIC_ONLY)
77
public class OrderDetails {
88

9+
public static final String PLACEHOLDER_ADDRESS = "To Someone,\n Their Address\nGoes\nHERE\nABC 123";
10+
911
public long tweetId;
10-
public CardType cardType = CardType.POST_CARD;
12+
public CardType cardType = CardType.POSTCARD;
1113
public Style style = Style.BIG;
1214
public Arrangement arrangement = Arrangement.PORTRAIT;
1315
public boolean responseTweet = true;
1416
public String message = "Dear Fred,\n\nHope you are well.\n\nWeather awful.\n\nWish you were here.\n\nLove Jim";
17+
public String address = PLACEHOLDER_ADDRESS;
1518
public Font font = Font.SANS;
1619

1720
public OrderDetails(long tweetId, CardType cardType, Style style, Arrangement arrangement, boolean responseTweet,
18-
String message, Font font) {
21+
String message, String address, Font font) {
1922
super();
2023
this.tweetId = tweetId;
2124
this.cardType = cardType;
2225
this.style = style;
2326
this.arrangement = arrangement;
2427
this.responseTweet = responseTweet;
2528
this.message = message;
29+
this.address = address;
2630
this.font = font;
2731
}
2832

tweetprint/src/main/java/org/riskfirst/tweetprint/image/ADLTweetBuilder.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.jsoup.Jsoup;
2323
import org.jsoup.select.Elements;
2424
import org.kite9.diagram.dom.ns.Kite9Namespaces;
25+
import org.riskfirst.tweetprint.builder.CardType;
26+
import org.riskfirst.tweetprint.builder.Font;
2527
import org.w3c.dom.Document;
2628
import org.w3c.dom.Element;
2729
import org.w3c.dom.Node;
@@ -42,6 +44,28 @@ private static double round(double value, int precision) {
4244
int scale = (int) Math.pow(10, precision);
4345
return (double) Math.round(value * scale) / scale;
4446
}
47+
48+
public Document convertMessageToAdl(String message, String address, Font f, CardType ct) throws Exception {
49+
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
50+
Document d = db.newDocument();
51+
Element diagram = d.createElementNS(ADL_NAMESPACE, "diagram");
52+
diagram.setAttributeNS(XSL_TEMPLATE_NAMESPACE, "xslt:template", "/public/templates/tweet/tweet-template.xsl");
53+
54+
Element frame = d.createElementNS(ADL_NAMESPACE, ct.name().toLowerCase());
55+
diagram.appendChild(frame);
56+
57+
Element messageElement = convertEmojis(frame, "message", message);
58+
messageElement.setAttribute("style","font-family: "+f.text);
59+
60+
if (ct == CardType.POSTCARD) {
61+
convertEmojis(frame, "address", address);
62+
}
63+
64+
d.appendChild(diagram);
65+
66+
67+
return d;
68+
}
4569

4670
public Document convertTweetsToAdl(List<Tweet> tweets) throws Exception {
4771
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
@@ -70,7 +94,7 @@ private Element convertTweet(Tweet status, boolean isLast, Document d) {
7094

7195
tweet.setAttribute("href", status.getUser().getProfileImageUrl());
7296
convertEmojis(tweet, "displayName", status.getUser().getDisplayedName());
73-
convertEmojis(tweet, "screenName", status.getUser().getName());
97+
convertEmojis(tweet, "screenName", "@"+status.getUser().getName());
7498
tweet.setAttribute("date", convertDateShort(status.getCreatedAt()));
7599
tweet.setAttribute("longDate", convertDateLong(status.getCreatedAt()));
76100
tweet.setAttribute("likes", convertSocialCount(status.getLikeCount()));

tweetprint/src/main/java/org/riskfirst/tweetprint/image/PreviewController.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
import org.kite9.diagram.common.Kite9XMLProcessingException;
1212
import org.kite9.diagram.dom.XMLHelper;
1313
import org.riskfirst.tweetprint.builder.Arrangement;
14+
import org.riskfirst.tweetprint.builder.BuilderController;
1415
import org.riskfirst.tweetprint.builder.CardType;
16+
import org.riskfirst.tweetprint.builder.Font;
17+
import org.riskfirst.tweetprint.builder.OrderDetails;
1518
import org.riskfirst.tweetprint.builder.Style;
1619
import org.springframework.http.RequestEntity;
1720
import org.springframework.stereotype.Controller;
1821
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.PathVariable;
1923
import org.springframework.web.bind.annotation.RequestParam;
2024
import org.w3c.dom.Document;
2125

@@ -32,7 +36,7 @@
3236
*/
3337
@Controller
3438
public class PreviewController {
35-
39+
3640
private final TwitterClient tc;
3741
private ADLTweetBuilder tweetBuilder = new ADLTweetBuilder();
3842

@@ -41,27 +45,37 @@ public PreviewController(TwitterClient tc) {
4145
this.tc = tc;
4246
}
4347

48+
@GetMapping(produces = "image/png", path="/image-preview/{order}")
49+
public void previewImage(
50+
HttpServletResponse response,
51+
@PathVariable("order") String base64,
52+
RequestEntity<?> request) throws Exception {
53+
54+
OrderDetails order = BuilderController.decodeOrder(base64);
55+
Tweet status = tc.getTweet(""+order.tweetId);
56+
convertTweet(response, order.cardType, order.style, order.arrangement, request, status);
57+
}
58+
4459
@GetMapping(produces = "image/png", path="/image-preview.png")
45-
public void preview(
60+
public void previewImage(
4661
HttpServletResponse response,
4762
@RequestParam(required = true, name="tweetId") String tweetId,
48-
@RequestParam(defaultValue = "POST_CARD", name="type") CardType type,
63+
@RequestParam(defaultValue = "POSTCARD", name="type") CardType type,
4964
@RequestParam(defaultValue = "BIG", name="style") Style style,
5065
@RequestParam(defaultValue = "PORTRAIT", name="arrangement") Arrangement arrangement,
5166
@RequestParam(defaultValue = "false", name="includeReplied") boolean includeReplied,
5267
RequestEntity<?> request) throws Exception {
5368

5469
Tweet status = tc.getTweet(tweetId);
55-
5670
convertTweet(response, type, style, arrangement, request, status);
5771
}
58-
72+
5973
private void convertTweet(HttpServletResponse response, CardType type, Style style, Arrangement arrangement,
6074
RequestEntity<?> request, Tweet status) throws Exception {
6175
float width, height;
6276
if (arrangement == Arrangement.PORTRAIT) {
63-
width = 600;
64-
height = width * type.ratio;
77+
height = 600;
78+
width = height / type.ratio;
6579
} else {
6680
width = 600;
6781
height = width / type.ratio;
@@ -73,14 +87,46 @@ private void convertTweet(HttpServletResponse response, CardType type, Style sty
7387

7488
convertToPng(adlIn, response, request.getUrl().toString(), style, width, height);
7589
}
90+
91+
@GetMapping(produces = "image/png", path="/message-preview/{order}")
92+
public void previewMessage(
93+
HttpServletResponse response,
94+
@PathVariable("order") String base64,
95+
RequestEntity<?> request) throws Exception {
96+
97+
OrderDetails order = BuilderController.decodeOrder(base64);
98+
convertMessage(response, order.cardType, order.font, request, order.message, order.address);
99+
}
100+
101+
@GetMapping(produces = "image/png", path="/message-preview.png")
102+
public void previewMessage(
103+
HttpServletResponse response,
104+
@RequestParam(defaultValue = "Your Message Here...", name="message") String message,
105+
@RequestParam(defaultValue = OrderDetails.PLACEHOLDER_ADDRESS, name="address") String address,
106+
@RequestParam(defaultValue = "POSTCARD", name="type") CardType type,
107+
@RequestParam(defaultValue = "SERIF", name="font") Font font,
108+
RequestEntity<?> request) throws Exception {
109+
convertMessage(response, type, font, request, message, address);
110+
}
111+
112+
private void convertMessage(HttpServletResponse response, CardType type, Font font, RequestEntity<?> request,
113+
String message, String address) throws Exception {
114+
Document adlIn = tweetBuilder.convertMessageToAdl(message, address, font, type);
115+
float width = 600;
116+
float height = width / type.ratio;
117+
convertToPng(adlIn, response, request.getUrl().toString(), null, width, height);
118+
119+
}
76120

77121
private void convertToPng(Document adlIn, HttpServletResponse response, String uri, Style style, float widthPx, float heightPx) {
78122
try {
79123
Kite9PNGTranscoder svgt = new Kite9PNGTranscoder();
80124
svgt.addTranscodingHint(Kite9SVGTranscoder.KEY_ENCAPSULATING, true);
81125
svgt.addTranscodingHint(Kite9SVGTranscoder.KEY_WIDTH, widthPx);
82126
svgt.addTranscodingHint(Kite9SVGTranscoder.KEY_HEIGHT, heightPx);
83-
svgt.getVariables().put("style", style.name().toLowerCase());
127+
if (style != null) {
128+
svgt.getVariables().put("style", style.name().toLowerCase());
129+
}
84130
TranscoderInput input = new TranscoderInput(adlIn);
85131
input.setURI(uri);
86132
TranscoderOutput pngOutput = new TranscoderOutput(response.getOutputStream());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.crop-block {
2+
background-color: #eeeeee;
3+
padding: 40px;
4+
}
5+
6+
#preview-image,
7+
#message-preview-image {
8+
margin: auto;
9+
box-shadow: 0 0 19px 0px rgba(0,0,0,0.61);
10+
}
11+
12+
#message {
13+
font-weight: 400;
14+
font-size: 30px;
15+
box-shadow: 0 0 19px 0px rgba(0,0,0,0.61);
16+
}
17+
18+
#loading1,
19+
#loading2 {
20+
margin: auto;
21+
width: 50px;
22+
height: 50px;
23+
}

tweetprint/src/main/resources/static/css/tweet-print-custom.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@import "components/cool-checkboxes.css";
22
@import "components/builder-radios.css";
3+
@import "components/builder-previews.css";
34

45
.twitter-profile img {
56
border-radius: 50%;
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)