Skip to content

Commit 220112b

Browse files
https imgur links work now.
1 parent e68f5b0 commit 220112b

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

fCraft/Drawing/DrawOps/DrawImageOperation.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,37 +72,42 @@ public ImageDrawOperation([NotNull] Player player, [NotNull] BlockPalette palett
7272

7373
public override bool ReadParams(CommandReader cmd) {
7474
// get image URL
75-
string urlString = cmd.Next();
76-
if (urlString.NullOrWhiteSpace()) return false;
75+
string str = cmd.Next();
76+
if (str.NullOrWhiteSpace()) return false;
7777

78-
if (urlString.StartsWith("http://imgur.com/")) {
79-
urlString = "http://i.imgur.com/" + urlString.Substring("http://imgur.com/".Length) + ".png";
80-
}
8178
// if string starts with "++", load image from imgur
82-
if (urlString.StartsWith("++")) {
83-
urlString = "http://i.imgur.com/" + urlString.Substring(2) + ".png";
84-
}
79+
if (str.StartsWith("++")) {
80+
str = "http://i.imgur.com/" + str.Substring(2) + ".png";
81+
}
8582
// prepend the protocol, if needed (assume http)
86-
if (!urlString.CaselessStarts("http://") && !urlString.CaselessStarts("https://")) {
87-
urlString = "http://" + urlString;
83+
if (!str.CaselessStarts("http://") && !str.CaselessStarts("https://")) {
84+
str = "http://" + str;
8885
}
89-
if (!urlString.CaselessStarts("http://i.imgur.com")) {
90-
Player.Message("For safety reasons we only accept images uploaded to &9http://imgur.com/ &SSorry for this inconvenience.");
91-
return false;
86+
87+
// Convert imgur web page url to direct image url
88+
if (str.StartsWith("http://imgur.com/")) {
89+
str = "http://i.imgur.com/" + str.Substring("http://imgur.com/".Length) + ".png";
9290
}
93-
if (!urlString.CaselessEnds(".png") && !urlString.CaselessEnds(".jpg") && !urlString.CaselessEnds(".gif")) {
91+
if (str.StartsWith("https://imgur.com/")) {
92+
str = "https://i.imgur.com/" + str.Substring("https://imgur.com/".Length) + ".png";
93+
}
94+
95+
if (!str.CaselessEnds(".png") && !str.CaselessEnds(".jpg") && !str.CaselessEnds(".gif")) {
9496
Player.Message("URL must be a link to an image");
9597
return false;
9698
}
9799

98100
// validate the image URL
99101
Uri url;
100-
if (!Uri.TryCreate(urlString, UriKind.Absolute, out url)) {
102+
if (!Uri.TryCreate(str, UriKind.Absolute, out url)) {
101103
Player.Message("DrawImage: Invalid URL given.");
102104
return false;
103105
} else if (!url.Scheme.Equals(Uri.UriSchemeHttp) && !url.Scheme.Equals(Uri.UriSchemeHttps)) {
104106
Player.Message("DrawImage: Invalid URL given. Only HTTP and HTTPS links are allowed.");
105107
return false;
108+
} else if (!url.Host.CaselessEquals("i.imgur.com")) {
109+
Player.Message("For safety reasons we only accept images uploaded to &9http://imgur.com/ &SSorry for this inconvenience.");
110+
return false;
106111
}
107112
ImageUrl = url;
108113

0 commit comments

Comments
 (0)