Skip to content

Commit

Permalink
Try adding support for discord's new things
Browse files Browse the repository at this point in the history
  • Loading branch information
AverageGithub authored Jun 6, 2024
1 parent 16ff330 commit 88b7847
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/main/java/me/ryzeon/transcripts/DiscordHtmlTranscripts.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;


/**
Expand Down Expand Up @@ -117,7 +120,7 @@ public InputStream generateFromMessages(Collection<Message> messages) throws IOE

Element authorAvatar = document.createElement("img");
authorAvatar.addClass("chatlog__author-avatar");
authorAvatar.attr("src", author.getAvatarUrl());
authorAvatar.attr("src", getData(author.getAvatarUrl()));
authorAvatar.attr("alt", "Avatar");
authorAvatar.attr("loading", "lazy");

Expand Down Expand Up @@ -186,7 +189,7 @@ public InputStream generateFromMessages(Collection<Message> messages) throws IOE

Element attachmentImage = document.createElement("img");
attachmentImage.addClass("chatlog__attachment-media");
attachmentImage.attr("src", attach.getProxy().getUrl());
attachmentImage.attr("src", getData(attach.getProxy().getUrl()));
attachmentImage.attr("alt", "Image attachment");
attachmentImage.attr("loading", "lazy");
attachmentImage.attr("title",
Expand All @@ -197,7 +200,7 @@ public InputStream generateFromMessages(Collection<Message> messages) throws IOE
} else if (videoFormats.contains(attachmentType)) {
Element attachmentVideo = document.createElement("video");
attachmentVideo.addClass("chatlog__attachment-media");
attachmentVideo.attr("src", attach.getProxy().getUrl());
attachmentVideo.attr("src", getData(attach.getProxy().getUrl()));
attachmentVideo.attr("alt", "Video attachment");
attachmentVideo.attr("controls", true);
attachmentVideo.attr("title",
Expand All @@ -207,7 +210,7 @@ public InputStream generateFromMessages(Collection<Message> messages) throws IOE
} else if (audioFormats.contains(attachmentType)) {
Element attachmentAudio = document.createElement("audio");
attachmentAudio.addClass("chatlog__attachment-media");
attachmentAudio.attr("src", attach.getProxy().getUrl());
attachmentAudio.attr("src", getData(attach.getProxy().getUrl()));
attachmentAudio.attr("alt", "Audio attachment");
attachmentAudio.attr("controls", true);
attachmentAudio.attr("title",
Expand Down Expand Up @@ -484,4 +487,23 @@ private InputStream findFile(String fileName) {
}
return inputStream;
}

private static String getData(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
String contentType = connection.getContentType();

InputStream stream = connection.getInputStream();
byte[] bytes = stream.readAllBytes();
String base64 = Base64.getEncoder().encodeToString(bytes);

stream.close();
connection.disconnect();
return String.format("data:%s;base64,%s", contentType, base64);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}

0 comments on commit 88b7847

Please sign in to comment.