|
| 1 | +package net.modgarden.gardenbot.commands.image; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import com.google.gson.annotations.SerializedName; |
| 6 | +import net.dv8tion.jda.api.entities.Message; |
| 7 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 8 | +import net.modgarden.gardenbot.GardenBot; |
| 9 | +import net.modgarden.gardenbot.interaction.SlashCommandInteraction; |
| 10 | +import net.modgarden.gardenbot.interaction.response.EmbedResponse; |
| 11 | +import net.modgarden.gardenbot.interaction.response.Response; |
| 12 | +import net.modgarden.gardenbot.util.BunnyCDNAPIClient; |
| 13 | +import net.modgarden.gardenbot.util.ModGardenAPIClient; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.io.InputStreamReader; |
| 18 | +import java.net.http.HttpRequest; |
| 19 | +import java.net.http.HttpResponse; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Random; |
| 22 | + |
| 23 | +public class UploadHandler { |
| 24 | + public static Response handleUpload(SlashCommandInteraction interaction) { |
| 25 | + interaction.event().deferReply(false).queue(); |
| 26 | + |
| 27 | + ModGardenEvent event; |
| 28 | + Message.Attachment attachment = interaction.event().getOption("attachment", OptionMapping::getAsAttachment); |
| 29 | + |
| 30 | + if (attachment == null || !attachment.isImage() || attachment.getContentType() == null || !attachment.getContentType().equals("image/png")) { |
| 31 | + return new EmbedResponse() |
| 32 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 33 | + .setDescription("Attachment must be a PNG.") |
| 34 | + .setColor(0x5D3E40); |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + var eventResult = ModGardenAPIClient.get("events/current/prefreeze", HttpResponse.BodyHandlers.ofInputStream()); |
| 39 | + if (eventResult.statusCode() != 200) { |
| 40 | + return new EmbedResponse() |
| 41 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 42 | + .setDescription("There is no active event to upload images for.") |
| 43 | + .setColor(0x5D3E40); |
| 44 | + } |
| 45 | + try (InputStreamReader eventReader = new InputStreamReader(eventResult.body())) { |
| 46 | + event = GardenBot.GSON.fromJson(eventReader, ModGardenEvent.class); |
| 47 | + } |
| 48 | + } catch (IOException | InterruptedException ex) { |
| 49 | + GardenBot.LOG.error("", ex); |
| 50 | + return new EmbedResponse() |
| 51 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 52 | + .setDescription(ex.getMessage() + "\nPlease report this to a team member.") |
| 53 | + .setColor(0xFF0000); |
| 54 | + } |
| 55 | + |
| 56 | + ModGardenUser user; |
| 57 | + try { |
| 58 | + var userResult = ModGardenAPIClient.get("user/" + interaction.event().getUser().getId() + "?service=discord", HttpResponse.BodyHandlers.ofInputStream()); |
| 59 | + if (userResult.statusCode() != 200) { |
| 60 | + return new EmbedResponse() |
| 61 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 62 | + .setDescription("You do not have a Mod Garden account. Please create one with **/register**.") |
| 63 | + .setColor(0x5D3E40); |
| 64 | + } |
| 65 | + |
| 66 | + // Validate that the user has submitted to the specified event. |
| 67 | + try (InputStreamReader userReader = new InputStreamReader(userResult.body())) { |
| 68 | + user = GardenBot.GSON.fromJson(userReader, ModGardenUser.class); |
| 69 | + if (!user.events.contains(event.id)) { |
| 70 | + return new EmbedResponse() |
| 71 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 72 | + .setDescription("You are not associated with any projects within " + event.displayName + ".") |
| 73 | + .setColor(0x5D3E40); |
| 74 | + } |
| 75 | + } |
| 76 | + } catch (IOException | InterruptedException ex) { |
| 77 | + GardenBot.LOG.error("", ex); |
| 78 | + return new EmbedResponse() |
| 79 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 80 | + .setDescription(ex.getMessage() + "\nPlease report this to a team member.") |
| 81 | + .setColor(0xFF0000); |
| 82 | + } |
| 83 | + StringBuilder fileNameBuilder = new StringBuilder() |
| 84 | + .append(event.slug) |
| 85 | + .append("/") |
| 86 | + .append(user.id); |
| 87 | + |
| 88 | + String hash; |
| 89 | + try { |
| 90 | + hash = selectUniqueHash(fileNameBuilder.toString()); |
| 91 | + } catch (IOException | InterruptedException ex) { |
| 92 | + GardenBot.LOG.error("", ex); |
| 93 | + return new EmbedResponse() |
| 94 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 95 | + .setDescription(ex.getMessage() + "\nPlease report this to a team member.") |
| 96 | + .setColor(0xFF0000); |
| 97 | + } |
| 98 | + if (hash.isBlank()) { |
| 99 | + return new EmbedResponse() |
| 100 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 101 | + .setDescription("Failed to generate a unique upload hash. Please try again.") |
| 102 | + .setColor(0x5D3E40); |
| 103 | + } |
| 104 | + |
| 105 | + fileNameBuilder |
| 106 | + .append("/") |
| 107 | + .append(hash) |
| 108 | + .append(".png"); |
| 109 | + |
| 110 | + try (InputStream attachmentStream = attachment.getProxy().download(attachment.getWidth(), attachment.getHeight()).join()) { |
| 111 | + HttpResponse<InputStream> uploadResponse = BunnyCDNAPIClient.put( |
| 112 | + "public/" + fileNameBuilder, |
| 113 | + HttpRequest.BodyPublishers.ofInputStream(() -> attachmentStream), |
| 114 | + HttpResponse.BodyHandlers.ofInputStream(), |
| 115 | + "Content-Type", "application/octet-stream", |
| 116 | + "Accept", "image/png" |
| 117 | + ); |
| 118 | + try (InputStreamReader streamReader = new InputStreamReader(uploadResponse.body())) { |
| 119 | + if (uploadResponse.statusCode() != 201) { |
| 120 | + JsonElement json = JsonParser.parseReader(streamReader); |
| 121 | + String errorMessage = json.isJsonObject() && json.getAsJsonObject().has("Message") ? |
| 122 | + json.getAsJsonObject().getAsJsonPrimitive("Message").getAsString() : |
| 123 | + "Undefined Error."; |
| 124 | + return new EmbedResponse() |
| 125 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 126 | + .setDescription(uploadResponse.statusCode() + ": " + errorMessage + "\nPlease report this to a team member.") |
| 127 | + .setColor(0xFF0000); |
| 128 | + } |
| 129 | + } |
| 130 | + } catch (IOException | InterruptedException ex) { |
| 131 | + GardenBot.LOG.error("", ex); |
| 132 | + return new EmbedResponse() |
| 133 | + .setTitle("Encountered an exception whilst attempting to upload an image to Mod Garden's CDN.") |
| 134 | + .setDescription(ex.getMessage() + "\nPlease report this to a team member.") |
| 135 | + .setColor(0xFF0000); |
| 136 | + } |
| 137 | + |
| 138 | + return new EmbedResponse() |
| 139 | + .setTitle("Successfully uploaded image to Mod Garden's CDN") |
| 140 | + .setDescription("Your image may be found at <https://cdn.modgarden.net/public/" + fileNameBuilder + ">") |
| 141 | + .setColor(0xA9FFA7); |
| 142 | + } |
| 143 | + |
| 144 | + public static String selectUniqueHash(String basePath) throws IOException, InterruptedException { |
| 145 | + for (int i = 0; i < 20; ++i) { |
| 146 | + String hash = generateHash(); |
| 147 | + HttpResponse<String> getResponse = BunnyCDNAPIClient.get( |
| 148 | + basePath + "/" + hash + ".png", |
| 149 | + HttpResponse.BodyHandlers.ofString() |
| 150 | + ); |
| 151 | + if (getResponse.statusCode() != 200) |
| 152 | + return hash; |
| 153 | + } |
| 154 | + return ""; |
| 155 | + } |
| 156 | + |
| 157 | + @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") |
| 158 | + private static class ModGardenUser { |
| 159 | + String id; |
| 160 | + List<String> events; |
| 161 | + } |
| 162 | + |
| 163 | + private static class ModGardenEvent { |
| 164 | + String id; |
| 165 | + String slug; |
| 166 | + @SerializedName("display_name") |
| 167 | + String displayName; |
| 168 | + } |
| 169 | + |
| 170 | + private static final String VALID_UNIQUE_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 171 | + private static final Random RANDOM = new Random(); |
| 172 | + |
| 173 | + private static String generateHash() { |
| 174 | + StringBuilder builder = new StringBuilder(); |
| 175 | + for (int i = 0; i < 6; ++i) { |
| 176 | + builder.append(VALID_UNIQUE_CHARS.charAt(RANDOM.nextInt(VALID_UNIQUE_CHARS.length()))); |
| 177 | + } |
| 178 | + return builder.toString(); |
| 179 | + } |
| 180 | +} |
0 commit comments