show post images

This commit is contained in:
2025-09-23 20:33:01 +02:00
parent 8b7e36e65a
commit ef5abc3096
5 changed files with 26 additions and 9 deletions
@@ -20,7 +20,7 @@ public class FediMedium implements Serializable {
this.mediumType = IMAGE; this.mediumType = IMAGE;
this.description = "missing alt text"; this.description = "missing alt text";
this.mediumUrl = "unknown"; this.mediumUrl = "unknown";
this.mediumImage = "unknown"; this.mediumImage = "assets/splash.jpg";
} }
public String getMediumType() { public String getMediumType() {
@@ -41,7 +41,15 @@ public class FediServlet extends HttpServlet {
throws ServletException, IOException { throws ServletException, IOException {
try { try {
final JSONLoader loader = new JSONLoader(instance, account); final String scheme = request.getScheme();
final String serverName = request.getServerName();
final int serverPort = request.getServerPort();
final String contextPath = request.getContextPath();
String contextUrl = scheme + "://" + serverName + ":" + serverPort + "/" + contextPath;
if (!contextUrl.endsWith("/")) {
contextUrl += "/";
}
final JSONLoader loader = new JSONLoader(instance, account, contextUrl);
final VelocityContext context = new VelocityContext(); final VelocityContext context = new VelocityContext();
context.put("dateformatter", new PostDateFormatter()); context.put("dateformatter", new PostDateFormatter());
context.put("page", loader.loadPage()); context.put("page", loader.loadPage());
@@ -18,12 +18,14 @@ public class JSONLoader implements Serializable {
private final String instance; private final String instance;
private final String account; private final String account;
private final String contextUrl;
private long id; private long id;
public JSONLoader(String instance, String account) { public JSONLoader(final String instance, final String account, final String contextUrl) {
this.instance = instance; this.instance = instance;
this.account = account; this.account = account;
this.contextUrl = contextUrl;
this.id = -1L; this.id = -1L;
} }
@@ -67,13 +69,20 @@ public class JSONLoader implements Serializable {
post.setPostUrl(jsonStatus.getString("url")); post.setPostUrl(jsonStatus.getString("url"));
final JSONArray mediaJsonArray = jsonStatus.getJSONArray("media_attachments"); final JSONArray mediaJsonArray = jsonStatus.getJSONArray("media_attachments");
if (mediaJsonArray.length() == 0) { if (mediaJsonArray.length() == 0) {
final JSONObject jsonCard = jsonStatus.getJSONObject("card"); final Object cardItem = jsonStatus.get("card");
if (jsonCard != null && jsonCard.getString("image") != null) { if (cardItem != null && cardItem instanceof JSONObject) {
final JSONObject jsonCard = jsonStatus.getJSONObject("card");
final FediMedium medium = new FediMedium(); final FediMedium medium = new FediMedium();
medium.setMediumType(FediMedium.CARD); medium.setMediumType(FediMedium.CARD);
medium.setDescription(jsonCard.getString("description")); medium.setDescription(jsonCard.getString("description"));
medium.setMediumUrl(jsonCard.getString("url")); medium.setMediumUrl(jsonCard.getString("url"));
medium.setMediumImage(jsonCard.getString("image")); final Object imageObject = jsonCard.get("image");
if (imageObject != null && imageObject instanceof String) {
medium.setMediumImage(jsonCard.getString("image"));
} else {
final String defaultImage = contextUrl + medium.getMediumImage();
medium.setMediumImage(defaultImage);
}
post.addFediMedium(medium); post.addFediMedium(medium);
} }
} else { } else {
+2 -2
View File
@@ -21,10 +21,10 @@
<servlet-mapping> <servlet-mapping>
<servlet-name>FediServlet</servlet-name> <servlet-name>FediServlet</servlet-name>
<url-pattern>/</url-pattern> <url-pattern></url-pattern>
</servlet-mapping> </servlet-mapping>
<welcome-file-list> <welcome-file-list>
<welcome-file>/</welcome-file> <welcome-file></welcome-file>
</welcome-file-list> </welcome-file-list>
</web-app> </web-app>
Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB