countdown-timer/src/main/java/com/synergyhelix/countdown/CountdownController.java

55 lines
1.6 KiB
Java

package com.synergyhelix.countdown;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.*;
@RestController
public class CountdownController {
@GetMapping(value = "/", produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] countdown() throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(createImage(), "jpg", outputStream);
return outputStream.toByteArray();
}
private BufferedImage createImage() throws IOException {
final BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/chicago.jpg"));
Graphics g = image.getGraphics();
Color color = new Color(0, 0, 0, 127);
g.setColor(color);
g.fillRect(10, 5, 530, 75);
g.setColor(Color.WHITE);
g.setFont(new Font("Courier", Font.BOLD, 57));
g.drawString(calculateTimeUntilThing(), 15, 60);
g.setColor(new Color(0xE4002B));
g.setFont(g.getFont().deriveFont(46f));
g.drawString("Vacation Timer!", 130, 135);
g.dispose();
return image;
}
private String calculateTimeUntilThing() {
ZonedDateTime trip = ZonedDateTime.of(LocalDateTime.parse("2022-08-13T13:00:00"), ZoneId.of("UTC"));
Duration duration = Duration.between(Instant.now(), trip);
return DurationFormatUtils.formatDuration(duration.toMillis(), "dd'D' HH'H' mm'M' ss'S'", true);
}
}