Skip to content

Commit

Permalink
feat(image): added image data generation utils
Browse files Browse the repository at this point in the history
  • Loading branch information
seailz committed Sep 19, 2023
1 parent 44a321b commit 382fc21
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/seailz/discordjar/utils/image/ImageUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
package com.seailz.discordjar.utils.image;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Base64;

public class ImageUtils {

public static String generateImageData(File file) {
String fileType = null;
try {
fileType = Files.probeContentType(file.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println(fileType);
return "data:" + fileType + ";base64," + Base64.getEncoder().encodeToString(getFileBytes(file));
}

private static byte[] getFileBytes(File file) {
try {
return Files.readAllBytes(file.toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static String getUrl(String hash, ImageType type, String... params) {
String urlWithBase = "https://cdn.discordapp.com/" + type.getUrl();
String paramable = urlWithBase.replaceAll("%h", hash);
Expand Down

0 comments on commit 382fc21

Please sign in to comment.