Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/main/java/net/dv8tion/jda/api/EmbedBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -907,12 +907,46 @@ public List<MessageEmbed.Field> getFields()
return fields;
}

private static boolean hasUrlFormat(@Nonnull String url)
{
if (url.regionMatches(true, 0, "https://", 0, "https://".length()))
{
return url.length() > "https://".length();
}
if (url.regionMatches(true, 0, "http://", 0, "http://".length()))
{
return url.length() > "http://".length();
}
if (url.regionMatches(true, 0, "attachment://", 0, "attachment://".length()))
{
return url.length() > "attachment://".length();
}
return false;
}

/**
* Checks if the provided {@code url} is a valid http(s) or attachment url.
* The length of the {@code url} can't exceed {@link MessageEmbed#URL_MAX_LENGTH}.
* Note that this check is case-insensitive and at least one character is required
* after the url prefix. Meaning that "HtTpS://" will return {@code false}, while
* "HtTpS://a" will return {@code true}.
*
* @param url The string to check.
*
* @return {@code true} if {@code url} is a valid http(s) or attachment url,
* {@code false} otherwise.
*/
public static boolean isUrlOrAttachment(@Nullable String url)
{
return url != null && url.length() <= MessageEmbed.URL_MAX_LENGTH && hasUrlFormat(url);
}

private void urlCheck(@Nullable String url)
{
if (url != null)
{
Checks.notLonger(url, MessageEmbed.URL_MAX_LENGTH, "URL");
Checks.check(URL_PATTERN.matcher(url).matches(), "URL must be a valid http(s) or attachment url.");
Checks.check(hasUrlFormat(url), "URL must be a valid http(s) or attachment url.");
}
}
}
Loading