ShareX/UploadersLib/FileUploaders/MediaCrushUploader.cs

228 lines
7.1 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
2014-05-13 21:06:40 +12:00
Copyright (C) 2007-2014 ShareX Developers
2014-01-05 07:12:52 +13:00
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)
// Credits: https://github.com/SirCmpwn
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
2014-05-09 14:10:11 +12:00
namespace UploadersLib.FileUploaders
2014-01-05 07:12:52 +13:00
{
2014-05-09 14:10:11 +12:00
public class MediaCrushUploader : FileUploader
2014-01-05 07:12:52 +13:00
{
public override UploadResult Upload(Stream stream, string fileName)
{
SuppressWebExceptions = true;
2014-01-05 12:21:44 +13:00
string hash = CreateHash(stream);
2014-01-05 11:46:18 +13:00
UploadResult result = CheckExists(hash);
2014-01-05 12:21:44 +13:00
if (result != null)
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);
2014-01-05 07:12:52 +13:00
}
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-05-21 11:52:54 +12:00
while (!stopUpload)
2014-01-05 07:12:52 +13:00
{
result.Response = SendRequest(HttpMethod.GET, "https://mediacru.sh/api/" + hash + "/status");
2014-01-05 11:46:18 +13:00
JToken jsonResponse = JToken.Parse(result.Response);
string status = jsonResponse["status"].Value<string>();
2014-01-05 12:21:44 +13:00
switch (status)
2014-01-05 07:12:52 +13:00
{
2014-01-05 12:21:44 +13:00
case "processing":
case "pending":
Thread.Sleep(1000);
break;
case "done":
case "ready":
MediaCrushBlob blob = jsonResponse[hash].ToObject<MediaCrushBlob>();
2014-01-05 15:15:44 +13:00
result.URL = blob.DirectURL;
2014-01-05 12:21:44 +13:00
result.DeletionURL = blob.DeletionURL;
return result;
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 07:12:52 +13:00
}
}
2014-05-21 11:52:54 +12:00
return result;
2014-01-05 12:21:44 +13:00
}
2014-01-05 11:46:18 +13:00
2014-01-05 12:21:44 +13:00
private string CreateHash(Stream stream)
{
using (MD5 md5 = MD5.Create())
{
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Seek(0, SeekOrigin.Begin);
string hash = Convert.ToBase64String(md5.ComputeHash(buffer));
return hash.Replace('+', '-').Replace('/', '_').Remove(12);
}
2014-01-05 07:12:52 +13:00
}
2014-01-05 12:21:44 +13:00
private UploadResult HandleDuplicate(HttpWebResponse httpResponse)
2014-01-05 07:12:52 +13:00
{
JToken response;
2014-01-05 11:46:18 +13:00
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
2014-05-21 11:52:54 +12:00
{
2014-01-05 07:12:52 +13:00
response = JToken.Parse(streamReader.ReadToEnd());
2014-05-21 11:52:54 +12:00
}
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
{
2014-01-05 15:15:44 +13:00
URL = blob.DirectURL,
2014-01-05 12:21:44 +13:00
DeletionURL = blob.DeletionURL
2014-01-05 07:12:52 +13:00
};
}
2014-01-05 07:34:25 +13:00
private UploadResult CheckExists(string hash)
{
try
{
string response = SendRequest(HttpMethod.GET, "https://mediacru.sh/api/" + hash);
2014-01-05 11:46:18 +13:00
2014-01-05 12:21:44 +13:00
if (!string.IsNullOrEmpty(response))
2014-01-05 07:34:25 +13:00
{
2014-01-05 12:21:44 +13:00
MediaCrushBlob blob = JsonConvert.DeserializeObject<MediaCrushBlob>(response);
return new UploadResult(response)
{
2014-01-05 15:15:44 +13:00
URL = blob.DirectURL,
2014-01-05 12:21:44 +13:00
DeletionURL = blob.DeletionURL
};
}
2014-01-05 07:34:25 +13:00
}
catch
{
}
2014-01-05 12:21:44 +13:00
return null;
2014-01-05 07:34:25 +13:00
}
2014-01-05 07:12:52 +13:00
}
internal class MediaCrushBlob
{
public class MediaCrushFile
2014-01-05 07:12:52 +13:00
{
[JsonProperty("file")]
public string Path { get; set; }
[JsonProperty("type")]
public string Mimetype { get; set; }
[JsonProperty("url")]
public string URL { get; set; }
2014-01-05 07:12:52 +13:00
}
[JsonProperty("blob_type")]
public string BlobType { get; set; }
[JsonProperty("compression")]
public double Compression { get; set; }
[JsonProperty("files")]
public MediaCrushFile[] Files { get; set; }
2014-01-05 07:12:52 +13:00
[JsonProperty("extras")]
public MediaCrushFile[] Extras { get; set; }
2014-01-05 07:12:52 +13:00
[JsonProperty("original")]
public string Original { get; set; }
[JsonProperty("type")]
public string UserMimetype { get; set; }
[JsonProperty("hash")]
public string Hash { get; set; }
[JsonIgnore]
2014-01-05 12:21:44 +13:00
public string URL
2014-01-05 07:12:52 +13:00
{
get
{
return "https://mediacru.sh/" + Hash;
}
}
2014-01-05 12:21:44 +13:00
[JsonIgnore]
public string DirectURL
{
get
{
if (Files != null && Files.Length > 0)
2014-01-05 12:21:44 +13:00
{
if (BlobType == "image")
{
return Files[0].URL;
}
else if (BlobType == "video" || BlobType == "audio")
{
return "https://mediacru.sh/" + Hash + "/direct";
}
2014-01-05 12:21:44 +13:00
}
2014-01-05 15:15:44 +13:00
return URL;
2014-01-05 12:21:44 +13:00
}
}
[JsonIgnore]
public string DeletionURL
{
get
{
return "https://mediacru.sh/" + Hash + "/delete";
}
}
2014-01-05 07:12:52 +13:00
}
2014-01-05 11:46:18 +13:00
}