Advertisement
Vaerys_Dawn

ImageURL sending

Feb 24th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 KB | None | 0 0
  1. //new code
  2.  
  3. public static RequestBuffer.RequestFuture<IMessage> sendFileURL(String message, String imageURL, IChannel channel, boolean loadMessage) {
  4.     if (Utility.isImageLink(imageURL) || imageURL.contains("giphy.gif")) {
  5.         return sendMessage(message + "\n" + imageURL, channel);
  6.     }
  7.     IMessage loading = null;
  8.     if (loadMessage) {
  9.         loading = sendMessage("`Loading...`", channel).get();
  10.     }
  11.  
  12.     RequestBuffer.RequestFuture<IMessage> sent = RequestBuffer.request(() -> {
  13.         IMessage sentMessage = null;
  14.         InputStream stream = null;
  15.         int responseCode = -1;
  16.         try {
  17.             //connect to the Image URL
  18.             HttpURLConnection connection = (HttpURLConnection) new URL(imageURL).openConnection();
  19.             connection.setRequestProperty("User-Agent", Constants.MOZILLA_USER_AGENT);
  20.  
  21.             //get responseCode in case of IOException;
  22.             responseCode = connection.getResponseCode();
  23.  
  24.             //turn the image connection into an inputStream
  25.             stream = connection.getInputStream();
  26.  
  27.             //image's file name
  28.             String filename = FilenameUtils.getName(new URL(imageURL).getPath());
  29.  
  30.             //send file
  31.             if (StringUtils.containsOnly(message, "\n") || (message == null) || message.equals("") && imageURL != null) {
  32.                 sentMessage = channel.sendFile("", stream, filename);
  33.             } else if (message != null && !message.isEmpty() && imageURL != null) {
  34.                 sentMessage = channel.sendFile(Utility.removeMentions(message), false, stream, filename);
  35.             } else {
  36.                 logger.debug("Error sending File to channel with id: " + channel.getLongID() + " on guild with id: " + channel.getGuild().getLongID() +
  37.                         ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: No file to send");
  38.             }
  39.         } catch (MissingPermissionsException e) {
  40.             //send message and url with url closed
  41.             missingPermissions("URL_FILE", channel);
  42.             sentMessage = sendMessage(message + " <" + imageURL + ">", channel).get();
  43.         } catch (RateLimitException e) {
  44.             //send exception back out, needed for request handling.
  45.             throw e;
  46.         } catch (MalformedURLException e) {
  47.             //this should never show up. seriously
  48.             Utility.sendStack(e);
  49.         } catch (SSLHandshakeException e) {
  50.             //something to do with the ssl handshake failed. unsure what causes this.
  51.             sendMessage(message + " " + imageURL + " `FAILED TO EMBED - Failed SSL Handshake`", channel).get();
  52.         } catch (IOException e) {
  53.             //the file failed to be grabbed.
  54.             String response = " `FAILED TO EMBED - ERROR:" + responseCode + "`";
  55.             if (responseCode == 403) {
  56.                 sentMessage = sendMessage(message + "\n" + imageURL + " `ERROR: 403, IMAGE FAILED TO RESPOND, IMAGE LINK NEEDS UPDATING.`", channel).get();
  57.             } else if (responseCode != -1) {
  58.                 sentMessage = sendMessage(message + "\n" + imageURL + response, channel).get();
  59.             } else {
  60.                 sentMessage = sendMessage(message + "\n" + imageURL, channel).get();
  61.             }
  62.         } catch (IllegalArgumentException e) {
  63.             //the host failed, unsure as to the cause. inspect.
  64.             if (e.getMessage().contains("http host = null")) {
  65.                 sendMessage("> `HTTP HOST ERROR, CHECK URL FOR ERRORS.`", channel);
  66.             }
  67.         }
  68.         try {
  69.             //close off the stream
  70.             if (stream != null) stream.close();
  71.         } catch (IOException e) {
  72.             //how the hell did this even happen
  73.             Utility.sendStack(e);
  74.         }
  75.         //return the completed message
  76.         return sentMessage;
  77.     });
  78.     if (loading != null) {
  79.         deleteMessage(loading);
  80.     }
  81.     return sent;
  82. }
  83.  
  84.  
  85.  
  86. //old code
  87.  
  88. public static IMessage sendFileURL(String message, String imageURL, IChannel channel, boolean loadMessage) {
  89.     IMessage toDelete = null;
  90.     if (loadMessage) {
  91.         toDelete = sendMessage("`Loading...`", channel).get();
  92.     }
  93.     IMessage sentMessage = null;
  94.     HttpURLConnection connection = null;
  95.     try {
  96.         connection = (HttpURLConnection) new URL(imageURL).openConnection();
  97.         connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) " + "AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
  98.         //setup for the stream
  99.  
  100.         InputStream stream = connection.getInputStream();
  101.         sentMessage = RequestBuffer.request(() -> {
  102.             try {
  103.                 //set up the file name
  104.                 URL url = new URL(imageURL);
  105.                 String filename = FilenameUtils.getName(url.getPath());
  106.                 if (filename.equalsIgnoreCase("giphy.gif")) {
  107.                     return sendMessage(message + " " + imageURL, channel).get();
  108.                 }
  109.                 //checks if url is valid
  110.                 if (!Utility.isImageLink(filename)) {
  111.                     return sendMessage(message + " " + imageURL, channel).get();
  112.                 }
  113.                 //sends message/files
  114.                 if (StringUtils.containsOnly(message, "\n") || (message == null) || message.equals("") && imageURL != null) {
  115.                     return channel.sendFile("", stream, filename);
  116.                 } else if (message != null && !message.isEmpty() && imageURL != null) {
  117.                     return channel.sendFile(Utility.removeMentions(message), false, stream, filename);
  118.                 } else {
  119.                     logger.debug("Error sending File to channel with id: " + channel.getLongID() + " on guild with id: " + channel.getGuild().getLongID() +
  120.                             ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: No file to send");
  121.                     return null;
  122.                 }
  123.             } catch (MalformedURLException e) {
  124.                 return sendMessage(message + " " + imageURL, channel).get();
  125.             } catch (MissingPermissionsException e) {
  126.                 missingPermissions("URL_FILE", channel);
  127.                 return sendMessage(message + " <" + imageURL + ">", channel).get();
  128.             }
  129.         }).get();
  130.         stream.close();
  131.     } catch (MalformedURLException e) {
  132.         Utility.sendStack(e);
  133.     } catch (SSLHandshakeException e) {
  134.         sendMessage(message + " " + imageURL + " `FAILED TO EMBED - Failed SSL Handshake`", channel).get();
  135.     } catch (IOException e) {
  136.         try {
  137.             if (connection != null) {
  138.                 int responseCode = connection.getResponseCode();
  139.                 sendMessage(message + " " + imageURL + " `FAILED TO EMBED - ERROR:" + responseCode + "`", channel).get();
  140.             } else {
  141.                 Utility.sendStack(e);
  142.             }
  143.         } catch (IOException e1) {
  144.             e1.printStackTrace();
  145.         }
  146.     } catch (IllegalArgumentException e) {
  147.         if (e.getMessage().contains("http host = null")) {
  148.             sendMessage("> `HTTP HOST ERROR, CHECK URL FOR ERRORS.`", channel);
  149.         }
  150.     }
  151.     if (loadMessage && toDelete != null) {
  152.         deleteMessage(toDelete);
  153.     }
  154.     return sentMessage;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement