|
12 | 12 | using System.Diagnostics; |
13 | 13 | using System.Net; |
14 | 14 | using System.Drawing; |
| 15 | +using System.Text.RegularExpressions; |
15 | 16 |
|
16 | 17 | namespace fCraft { |
17 | 18 | /// <summary> Contains commands related to world management. </summary> |
@@ -962,13 +963,21 @@ private static void GenHMHandler(Player player, CommandReader cmd) { |
962 | 963 |
|
963 | 964 | // generate the map |
964 | 965 | int mapWidth = 0, mapLength = 0; |
965 | | - player.SendNow(Packet.Message(0, "Downloading file from: &9" + url, player)); |
966 | | - heightmap = DownloadImage(url, player); |
| 966 | + try { |
| 967 | + heightmap = DownloadImage(url, player); |
| 968 | + } |
| 969 | + catch (ArgumentException ex) { |
| 970 | + player.Message("&WGenHeightMap: Error setting up: " + ex.Message); |
| 971 | + } |
| 972 | + catch (Exception ex) { |
| 973 | + Logger.Log(LogType.Warning, "GenHeightMap: Error downloading image from {0}: {1}", url, ex); |
| 974 | + player.Message("&WGenHeightMap: Error downloading: " + ex.Message); |
| 975 | + } |
967 | 976 | if (heightmap == null) return; |
968 | 977 | mapWidth = heightmap.Width; |
969 | 978 | mapLength = heightmap.Height; |
970 | 979 | if (!Map.IsValidDimension(mapWidth) || !Map.IsValidDimension(mapLength)) { |
971 | | - player.Message("Invalid image size along {0} &S(Must be inbetween 16 and 1024)", Map.IsValidDimension(mapWidth) ? "height: &F" + mapLength : "width: &F" + mapWidth); |
| 980 | + player.Message("&WInvalid image size along {0} &S(Must be inbetween 16 and 1024)", Map.IsValidDimension(mapWidth) ? "height: &F" + mapLength : "width: &F" + mapWidth); |
972 | 981 | return; |
973 | 982 | } |
974 | 983 | player.SendNow(Packet.Message(0, "Generating HeightMap...", player)); |
@@ -1106,48 +1115,82 @@ public static Bitmap DownloadImage(string url, Player player) { |
1106 | 1115 | using (WebResponse resp = reqLen.GetResponse()) { |
1107 | 1116 | int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength); |
1108 | 1117 | } |
1109 | | - if (ContentLength > 5000000) { |
1110 | | - player.Message("&WImage size is too large {0}MB > 5MB", ContentLength / 1000000); |
| 1118 | + if (ContentLength > 10000000) { |
| 1119 | + player.Message("&WImage size is too large {0}MB > 10MB", ContentLength / 1000000); |
1111 | 1120 | return null; |
1112 | 1121 | } |
1113 | 1122 |
|
1114 | | - HttpWebRequest request = HttpUtil.CreateRequest(new Uri(url), TimeSpan.FromSeconds(6)); |
| 1123 | + player.Message("Downloading file from: &9" + url); |
| 1124 | + if (ContentLength > 1000000) { |
| 1125 | + player.Message("Depending on the image size, this could take some time."); |
| 1126 | + } |
| 1127 | + HttpWebRequest request = HttpUtil.CreateRequest(new Uri(url), TimeSpan.FromSeconds(15)); |
1115 | 1128 | using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { |
1116 | | - if ((response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Moved || |
1117 | | - response.StatusCode == HttpStatusCode.Redirect) && |
1118 | | - response.ContentType.CaselessStarts("image")) { |
1119 | | - // if the remote file was found, download it |
1120 | | - using (Stream inputStream = response.GetResponseStream()) { |
1121 | | - // TODO: check file size limit? |
1122 | | - return new Bitmap(inputStream); |
| 1129 | + if (response.ContentType.CaselessStarts("image")) { |
| 1130 | + if ((response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Moved || |
| 1131 | + response.StatusCode == HttpStatusCode.Redirect)) { |
| 1132 | + // if the remote file was found, download it |
| 1133 | + using (Stream inputStream = response.GetResponseStream()) { |
| 1134 | + // TODO: check file size limit? |
| 1135 | + player.Message("&SImage successfully downloaded!"); |
| 1136 | + return new Bitmap(inputStream); |
| 1137 | + } |
| 1138 | + } else { |
| 1139 | + player.Message("&WFailed to download the image from the given url."); |
| 1140 | + throw new Exception("Error downloading image: " + response.StatusCode); |
1123 | 1141 | } |
1124 | 1142 | } else { |
1125 | | - player.Message("&WFailed to download the image from the given url."); |
1126 | | - throw new Exception("Error downloading image: " + response.StatusCode); |
| 1143 | + player.Message("&WThe given URL is not an image."); |
| 1144 | + throw new Exception("URL is not Content Type of Image"); |
1127 | 1145 | } |
1128 | 1146 | } |
1129 | 1147 |
|
1130 | 1148 | } |
1131 | 1149 |
|
1132 | 1150 | public static bool parseUrl(ref string urlString, Player player) { |
1133 | 1151 | if (urlString.NullOrWhiteSpace()) { |
1134 | | - player.Message("You must provide a url to a heightmap image."); |
| 1152 | + player.Message("You must provide a URL to an image."); |
1135 | 1153 | return false; |
1136 | 1154 | } |
1137 | | - |
1138 | | - if (urlString.StartsWith("http://imgur.com/")) urlString = "http://i.imgur.com/" + urlString.Substring("http://imgur.com/".Length) + ".png"; |
1139 | | - if (urlString.StartsWith("++")) urlString = "http://i.imgur.com/" + urlString.Substring(2) + ".png"; |
1140 | | - if (!urlString.CaselessStarts("http://") && !urlString.CaselessStarts("https://")) urlString = "http://" + urlString; |
1141 | | - |
1142 | | - if (!urlString.CaselessStarts("http://i.imgur.com/") && !urlString.CaselessStarts("https://123DMWM.com/")) { |
1143 | | - player.Message("For safety reasons we only accept images uploaded to &9http://imgur.com/ &SSorry for this inconvenience."); |
1144 | | - player.Message(" You cannot use: &9" + urlString); |
1145 | | - return false; |
| 1155 | + string queryStr = ""; |
| 1156 | + if (urlString.Contains('?')) { |
| 1157 | + queryStr = urlString.Substring(urlString.IndexOf('?')); |
| 1158 | + urlString = urlString.Substring(0, urlString.IndexOf('?')); |
1146 | 1159 | } |
1147 | | - |
1148 | | - if (!urlString.CaselessEnds(".png") && !urlString.CaselessEnds(".jpg") && !urlString.CaselessEnds(".bmp")) { |
1149 | | - player.Message("URL must be a link to an image (.png/.jpg/.bmp"); |
1150 | | - return false; |
| 1160 | + if (urlString.StartsWith("++")) { |
| 1161 | + urlString = "https://i.imgur.com/" + urlString.Substring(2) + ".png"; |
| 1162 | + } |
| 1163 | + if (!urlString.CaselessStarts("http://") && !urlString.CaselessStarts("https://")) { |
| 1164 | + urlString = "http://" + urlString; |
| 1165 | + } |
| 1166 | + if (urlString.CaselessContains("imgur.com")) { |
| 1167 | + if (urlString.CaselessContains("//www.imgur.com/") || urlString.CaselessStarts("www.imgur.com/")) { |
| 1168 | + urlString = urlString.ReplaceString("www.imgur.com", "i.imgur.com", StringComparison.OrdinalIgnoreCase); |
| 1169 | + } |
| 1170 | + if (urlString.CaselessContains("//imgur.com/") || urlString.CaselessStarts("imgur.com/")) { |
| 1171 | + urlString = urlString.ReplaceString("imgur.com", "i.imgur.com", StringComparison.OrdinalIgnoreCase); |
| 1172 | + } |
| 1173 | + if (urlString.CaselessContains("//i.imgur.com/") && !Regex.Match(urlString, @"\.[a-z0-9]{2,5}$", RegexOptions.IgnoreCase).Success) { |
| 1174 | + urlString += ".png"; |
| 1175 | + } |
| 1176 | + if (urlString.CaselessContains("/gallery/") || urlString.CaselessContains("/a/")) { |
| 1177 | + player.Message("You cannot use an Imgur Gallery or Album URL."); |
| 1178 | + return false; |
| 1179 | + } |
| 1180 | + } |
| 1181 | + if (player.Info.Rank != RankManager.HighestRank) {//let highest rank use any URL |
| 1182 | + if (!urlString.CaselessEnds(".png") && !urlString.CaselessEnds(".jpg") && !urlString.CaselessEnds(".jpeg") && !urlString.CaselessEnds(".bmp") && !urlString.CaselessEnds(".gif")) { |
| 1183 | + player.Message("URL must be a link to an image (.png/.jpg/.bmp"); |
| 1184 | + return false; |
| 1185 | + } |
| 1186 | + if (!urlString.CaselessContains("i.imgur.com") && !urlString.CaselessContains("123dmwm.com/I/")) { |
| 1187 | + player.Message("For safety reasons we only accept images uploaded to &9https://imgur.com/ &SSorry for this inconvenience."); |
| 1188 | + player.Message(" You cannot use: &9" + urlString); |
| 1189 | + return false; |
| 1190 | + } |
| 1191 | + } |
| 1192 | + if (queryStr != "") { |
| 1193 | + urlString += queryStr; |
1151 | 1194 | } |
1152 | 1195 |
|
1153 | 1196 | return true; |
|
0 commit comments