ShareX/UploadersLib/ImageUploaders/MediaCrushUploader.cs

198 lines
6.5 KiB
C#
Raw Normal View History

2014-01-05 07:12:52 +13:00
#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/>.
*/
#endregion License Information (GPL v3)
2014-01-05 11:46:18 +13:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2014-01-05 07:12:52 +13:00
using System;
using System.IO;
2014-01-05 11:46:18 +13:00
using System.Net;
2014-01-05 07:34:25 +13:00
using System.Security.Cryptography;
2014-01-05 11:46:18 +13:00
using System.Threading;
2014-01-05 07:12:52 +13:00
namespace UploadersLib.ImageUploaders
{
public class MediaCrushUploader : ImageUploader
{
public MediaCrushUploader()
{
}
public override UploadResult Upload(Stream stream, string fileName)
{
2014-01-05 11:46:18 +13:00
string hash;
using (MD5 md5 = MD5.Create())
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Seek(0, SeekOrigin.Begin);
hash = Convert.ToBase64String(md5.ComputeHash(buffer));
hash = hash.Replace('+', '-').Replace('/', '_').Remove(12);
}
UploadResult result = CheckExists(hash);
2014-01-05 07:34:25 +13:00
if (result.IsSuccess)
2014-01-05 11:46:18 +13:00
{
2014-01-05 07:34:25 +13:00
return result;
2014-01-05 11:46:18 +13:00
}
2014-01-05 07:12:52 +13:00
try
{
result = UploadData(stream, "https://mediacru.sh/api/upload/file", fileName, suppressWebExceptions: false);
}
catch (WebException e)
{
2014-01-05 11:46:18 +13:00
HttpWebResponse response = e.Response as HttpWebResponse;
2014-01-05 07:12:52 +13:00
if (response == null)
2014-01-05 11:46:18 +13:00
{
2014-01-05 07:12:52 +13:00
throw;
2014-01-05 11:46:18 +13:00
}
2014-01-05 07:12:52 +13:00
if (response.StatusCode == HttpStatusCode.Conflict)
2014-01-05 11:46:18 +13:00
{
2014-01-05 07:12:52 +13:00
return HandleDuplicate(response);
2014-01-05 11:46:18 +13:00
}
2014-01-05 07:12:52 +13:00
throw;
}
2014-01-05 11:46:18 +13:00
2014-01-05 07:34:25 +13:00
hash = JToken.Parse(result.Response)["hash"].Value<string>();
2014-01-05 11:46:18 +13:00
2014-01-05 07:12:52 +13:00
while (true)
{
2014-01-05 11:46:18 +13:00
result.Response = SendGetRequest("https://mediacru.sh/api/" + hash + "/status");
JToken jsonResponse = JToken.Parse(result.Response);
string status = jsonResponse["status"].Value<string>();
if (status == "processing" || status == "pending")
2014-01-05 07:12:52 +13:00
{
Thread.Sleep(1000);
continue;
}
2014-01-05 11:46:18 +13:00
2014-01-05 07:12:52 +13:00
if (status == "done" || status == "ready")
{
2014-01-05 11:46:18 +13:00
MediaCrushBlob blob = jsonResponse[hash].ToObject<MediaCrushBlob>();
result.URL = "https://mediacru.sh" + blob.Files[0].Path;
result.DeletionURL = "https://mediacru.sh/" + blob.Hash + "/delete";
break;
2014-01-05 07:12:52 +13:00
}
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.");
}
}
}
2014-01-05 11:46:18 +13:00
return result;
2014-01-05 07:12:52 +13:00
}
public UploadResult HandleDuplicate(HttpWebResponse httpResponse)
{
JToken response;
2014-01-05 11:46:18 +13:00
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
2014-01-05 07:12:52 +13:00
response = JToken.Parse(streamReader.ReadToEnd());
2014-01-05 11:46:18 +13:00
string hash = response["hash"].Value<string>();
MediaCrushBlob blob = response[hash].ToObject<MediaCrushBlob>();
2014-01-05 07:12:52 +13:00
return new UploadResult
{
URL = blob.Url,
2014-01-05 11:46:18 +13:00
ThumbnailURL = "https://mediacru.sh" + blob.Files[0].Path,
DeletionURL = "https://mediacru.sh/" + blob.Hash + "/delete",
IsSuccess = true
2014-01-05 07:12:52 +13:00
};
}
2014-01-05 07:34:25 +13:00
private UploadResult CheckExists(string hash)
{
try
{
2014-01-05 11:46:18 +13:00
string response = SendGetRequest("https://mediacru.sh/api/" + hash);
MediaCrushBlob blob = JsonConvert.DeserializeObject<MediaCrushBlob>(response);
2014-01-05 07:34:25 +13:00
return new UploadResult
{
URL = blob.Url,
2014-01-05 11:46:18 +13:00
ThumbnailURL = "https://mediacru.sh" + blob.Files[0].Path,
DeletionURL = "https://mediacru.sh/" + blob.Hash + "/delete",
Response = blob.ToString(),
IsSuccess = true
2014-01-05 07:34:25 +13:00
};
}
catch
{
return new UploadResult { IsSuccess = false };
}
}
2014-01-05 07:12:52 +13:00
}
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;
}
}
}
2014-01-05 11:46:18 +13:00
}