MediaCrush direct url handling, added SupressWebExceptions to uploader class

This commit is contained in:
Jaex 2014-05-09 05:59:09 +03:00
parent 3c97c86622
commit 04e2f2dd91
2 changed files with 28 additions and 20 deletions

View file

@ -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;
}
}
}

View file

@ -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<string, string> 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)