Initial support for MediaCrush

This commit is contained in:
Drew DeVault 2014-01-04 10:12:52 -08:00
parent d31f60ed4d
commit ed39601d2d
5 changed files with 177 additions and 12 deletions

View file

@ -620,6 +620,9 @@ public UploadResult UploadImage(Stream stream, string fileName)
case ImageDestination.Immio:
imageUploader = new ImmioUploader();
break;
case ImageDestination.MediaCrush:
imageUploader = new MediaCrushUploader();
break;
case ImageDestination.CustomImageUploader:
if (Program.UploadersConfig.CustomUploadersList.IsValidIndex(Program.UploadersConfig.CustomImageUploaderSelected))
{

View file

@ -52,6 +52,8 @@ public enum ImageDestination
yFrog,
[Description("imm.io")]
Immio,
[Description("MediaCrush")]
MediaCrush,
[Description("Custom image uploader")]
CustomImageUploader,
[Description("File uploader")]

View file

@ -0,0 +1,154 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2008-2013 ShareX Developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
using System.Net;
using Newtonsoft.Json.Linq;
using System.Threading;
using Newtonsoft.Json;
#endregion License Information (GPL v3)
using System;
using System.IO;
namespace UploadersLib.ImageUploaders
{
public class MediaCrushUploader : ImageUploader
{
public MediaCrushUploader()
{
}
public override UploadResult Upload(Stream stream, string fileName)
{
UploadResult result;
try
{
result = UploadData(stream, "https://mediacru.sh/api/upload/file", fileName, suppressWebExceptions: false);
}
catch (WebException e)
{
var response = e.Response as HttpWebResponse;
if (response == null)
throw;
if (response.StatusCode == HttpStatusCode.Conflict)
return HandleDuplicate(response);
throw;
}
var hash = JToken.Parse(result.Response)["hash"].Value<string>();
while (true)
{
var request = (HttpWebRequest)WebRequest.Create("https://mediacru.sh/api/" + hash + "/status");
var httpResponse = request.GetResponse();
JToken response;
using (var streamReader = new StreamReader(httpResponse))
response = JToken.Parse(streamReader.ReadToEnd());
var status = response["status"];
var done = !(status == "processing" || status == "pending");
if (!done)
{
Thread.Sleep(1000);
continue;
}
if (status == "done" || status == "ready")
{
var blob = response[hash].ToObject<MediaCrushBlob>();
return new UploadResult
{
DeletionURL = "https://mediacru.sh/" + blob.Hash + "/delete",
IsSuccess = true,
ThumbnailURL = "https://mediacru.sh" + blob.Files[0].Path,
URL = blob.Url,
IsURLExpected = false
};
}
else
{
switch (status)
{
case "unrecognized":
// Note: MediaCrush accepts just about _every_ kind of media file,
// so the file itself is probably corrupted or just not actually a media file
throw new Exception("This file is not an acceptable file type.");
case "timeout":
throw new Exception("This file took too long to process.");
default:
throw new Exception("This file failed to process.");
}
}
}
}
public UploadResult HandleDuplicate(HttpWebResponse httpResponse)
{
JToken response;
using (var streamReader = new StreamReader(httpResponse))
response = JToken.Parse(streamReader.ReadToEnd());
var hash = response["hash"].Value<string>();
var blob = response[hash].ToObject<MediaCrushBlob>();
return new UploadResult
{
DeletionURL = "https://mediacru.sh/" + blob.Hash + "/delete",
IsSuccess = true,
ThumbnailURL = "https://mediacru.sh" + blob.Files[0].Path,
URL = blob.Url,
IsURLExpected = false
};
}
}
internal class MediaCrushBlob
{
public class File
{
[JsonProperty("file")]
public string Path { get; set; }
[JsonProperty("type")]
public string Mimetype { get; set; }
}
[JsonProperty("blob_type")]
public string BlobType { get; set; }
[JsonProperty("compression")]
public double Compression { get; set; }
[JsonProperty("files")]
public File[] Files { get; set; }
[JsonProperty("extras")]
public File[] Extras { get; set; }
[JsonProperty("original")]
public string Original { get; set; }
[JsonProperty("type")]
public string UserMimetype { get; set; }
[JsonProperty("hash")]
public string Hash { get; set; }
[JsonIgnore]
public string Url
{
get
{
return "https://mediacru.sh/" + Hash;
}
}
}
}

View file

@ -208,7 +208,8 @@ private HttpWebResponse GetResponseUsingPost(string url, Stream dataStream, stri
}
protected UploadResult UploadData(Stream dataStream, string url, string fileName, string fileFormName = "file",
Dictionary<string, string> arguments = null, CookieCollection cookies = null, NameValueCollection headers = null)
Dictionary<string, string> arguments = null, CookieCollection cookies = null, NameValueCollection headers = null,
bool suppressWebExceptions = true)
{
UploadResult result = new UploadResult();
@ -237,6 +238,12 @@ protected UploadResult UploadData(Stream dataStream, string url, string fileName
result.Response = ResponseToString(request.GetResponse());
result.IsSuccess = true;
}
catch (WebException e)
{
if (!suppressWebExceptions)
throw;
if (!stopUpload) result.Response = AddWebError(e);
}
catch (Exception e)
{
if (!stopUpload) result.Response = AddWebError(e);

View file

@ -68,17 +68,10 @@
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
</PropertyGroup>
<ItemGroup>
<Reference Include="AsyncBridge.Net35, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b3b1c0202c0d6a87, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\AsyncBridge.Net35.0.2.0\lib\net35-Client\AsyncBridge.Net35.dll</HintPath>
</Reference>
<Reference Include="MegaApiClient, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0480d311efbeb4e2, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\MegaApiClient.1.0.0\lib\MegaApiClient.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Renci.SshNet, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\SSH.NET.2013.4.7\lib\net35\Renci.SshNet.dll</HintPath>
@ -95,10 +88,6 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel.Web" />
<Reference Include="System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\TaskParallelLibrary.1.0.2856.0\lib\Net35\System.Threading.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Windows.Forms" />
@ -106,6 +95,15 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="AsyncBridge.Net35">
<HintPath>..\packages\AsyncBridge.Net35.0.2.0\lib\net35-Client\AsyncBridge.Net35.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading">
<HintPath>..\packages\TaskParallelLibrary.1.0.2856.0\lib\Net35\System.Threading.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ApiKeys\ApiKeys.cs" />
@ -302,6 +300,7 @@
<Compile Include="URLShorteners\IsgdURLShortener.cs" />
<Compile Include="URLShorteners\TinyURLShortener.cs" />
<Compile Include="URLShorteners\TurlURLShortener.cs" />
<Compile Include="ImageUploaders\MediaCrushUploader.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="ApiKeys\ApiKeysUI.resx">