From 04e2f2dd91a7e6eb7701c6a1c42aaad7e51a9001 Mon Sep 17 00:00:00 2001 From: Jaex Date: Fri, 9 May 2014 05:59:09 +0300 Subject: [PATCH] MediaCrush direct url handling, added SupressWebExceptions to uploader class --- .../FileUploaders/MediaCrushUploader.cs | 31 +++++++++---------- UploadersLib/Uploader.cs | 17 +++++++--- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/UploadersLib/FileUploaders/MediaCrushUploader.cs b/UploadersLib/FileUploaders/MediaCrushUploader.cs index 007484b46..0c39e42ba 100644 --- a/UploadersLib/FileUploaders/MediaCrushUploader.cs +++ b/UploadersLib/FileUploaders/MediaCrushUploader.cs @@ -28,6 +28,7 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; +using System.Collections.Generic; using System.IO; using System.Net; using System.Security.Cryptography; @@ -39,6 +40,8 @@ public class MediaCrushUploader : FileUploader { public override UploadResult Upload(Stream stream, string fileName) { + SuppressWebExceptions = true; + string hash = CreateHash(stream); UploadResult result = CheckExists(hash); @@ -50,7 +53,7 @@ public override UploadResult Upload(Stream stream, string fileName) try { - result = UploadData(stream, "https://mediacru.sh/api/upload/file", fileName, suppressWebExceptions: false); + result = UploadData(stream, "https://mediacru.sh/api/upload/file", fileName); } catch (WebException e) { @@ -161,6 +164,8 @@ public class MediaCrushFile public string Path { get; set; } [JsonProperty("type")] public string Mimetype { get; set; } + [JsonProperty("url")] + public string URL { get; set; } } [JsonProperty("blob_type")] @@ -192,9 +197,16 @@ public string DirectURL { get { - if (Files != null && Files.Length > 0 && IsDirectURLPossible(Files[0])) + if (Files != null && Files.Length > 0) { - return "https://mediacru.sh" + Files[0].Path; + if (BlobType == "image") + { + return Files[0].URL; + } + else if (BlobType == "video" || BlobType == "audio") + { + return "https://mediacru.sh/" + Hash + "/direct"; + } } return URL; @@ -209,18 +221,5 @@ public string DeletionURL return "https://mediacru.sh/" + Hash + "/delete"; } } - - private bool IsDirectURLPossible(MediaCrushFile file) - { - switch (file.Mimetype) - { - case "image/png": - case "image/jpeg": - case "image/bmp": - return true; - } - - return false; - } } } \ No newline at end of file diff --git a/UploadersLib/Uploader.cs b/UploadersLib/Uploader.cs index 34f8ba18e..3e8238fda 100644 --- a/UploadersLib/Uploader.cs +++ b/UploadersLib/Uploader.cs @@ -50,6 +50,7 @@ public class Uploader public bool IsUploading { get; protected set; } public int BufferSize { get; set; } public bool AllowReportProgress { get; set; } + public bool SuppressWebExceptions { get; set; } protected bool stopUpload; @@ -176,6 +177,11 @@ public virtual void StopUpload() return (HttpWebResponse)request.GetResponse(); } + catch (WebException e) + { + if (SuppressWebExceptions) throw; + if (!stopUpload) AddWebError(e); + } catch (Exception e) { if (!stopUpload) AddWebError(e); @@ -248,6 +254,11 @@ private HttpWebResponse PostResponseJSON(string url, string json, CookieCollecti return (HttpWebResponse)request.GetResponse(); } + catch (WebException e) + { + if (SuppressWebExceptions) throw; + if (!stopUpload) AddWebError(e); + } catch (Exception e) { if (!stopUpload) AddWebError(e); @@ -262,7 +273,7 @@ private HttpWebResponse PostResponseJSON(string url, string json, CookieCollecti protected UploadResult UploadData(Stream dataStream, string url, string fileName, string fileFormName = "file", Dictionary arguments = null, CookieCollection cookies = null, NameValueCollection headers = null, - ResponseType responseType = ResponseType.Text, bool suppressWebExceptions = true) + ResponseType responseType = ResponseType.Text) { UploadResult result = new UploadResult(); @@ -293,9 +304,7 @@ private HttpWebResponse PostResponseJSON(string url, string json, CookieCollecti } catch (WebException e) { - if (!suppressWebExceptions) - throw; - + if (SuppressWebExceptions) throw; if (!stopUpload) result.Response = AddWebError(e); } catch (Exception e)