private void BitlyShortenCallback(BitlyResponse bitlyUrl) { try { Action updateUi; if (bitlyUrl.StatusCode == 200) { updateUi = () => { Url = bitlyUrl.Data.Url; }; } else { updateUi = () => { Error = DescriptionAttribute.GetDescription(bitlyUrl.Error); }; } DispatcherHelper.CheckBeginInvokeOnUI(updateUi); } finally { DispatcherHelper.CheckBeginInvokeOnUI(() => { IsLoading = false; UrlFocused = true; }); } }
public static string ShortenUrl(string url) { if (Cache.ContainsKey(url)) { return(Cache[url]); } HttpWebResponse response; try { HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(BITLY_API + "login=spazzmatic&apiKey=" + Main + "&longUrl=" + url); wr.ContentType = "application/json"; wr.Accept = "*/*"; wr.Method = "GET"; wr.UserAgent = "DenizenBot"; response = (HttpWebResponse)wr.GetResponse(); } catch (WebException) { try { HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(BITLY_API + "login=morphan1&apiKey=" + Backup + "&longUrl=" + url); wr.ContentType = "application/json"; wr.Accept = "*/*"; wr.Method = "GET"; wr.UserAgent = "DenizenBot"; response = (HttpWebResponse)wr.GetResponse(); } catch (Exception ex) { Logger.Output(LogType.ERROR, "Error getting shortened URL from bit.ly: " + ex.Message); return(""); } } BitlyResponse br = Utilities.GetObjectFromWebResponse <BitlyResponse>(response); string shortened = br.data.url; Cache[url] = shortened; return(shortened); }
public async Task <IHttpActionResult> UploadImage() { var file = HttpContext.Current.Request.Files?[0]; if (file != null && file.ContentLength > 0) { string filename = Path.GetFileName(file.FileName); if (file.ContentLength > maxSize) { return(BadRequest($"File exceeds size of {maxSize}")); } if (!file.ContentType.StartsWith("image")) { return(BadRequest("Submitted file must be an image")); } try { string resourceUrl = await new S3Uploader().Upload($"{DateTimeOffset.Now.ToUnixTimeSeconds()}_{filename}", file.InputStream); BitlyResponse bitly = await S3Uploader.GenerateBitly(resourceUrl); return(Ok(bitly.Link)); } catch (Exception e) { return(InternalServerError(e)); } //string contents = new StreamReader(file.InputStream).ReadToEnd(); //Console.WriteLine(contents); } return(BadRequest("Invalid file provided")); }