ShareX/ShareX.UploadersLib/FileUploaders/MediaCrushUploader.cs

218 lines
7.1 KiB
C#
Raw Normal View History

2014-12-31 23:07:19 +13:00
#region License Information (GPL v3)
2014-01-05 07:12:52 +13:00
/*
ShareX - A program that allows you to take screenshots and share any file type
2016-01-04 04:16:01 +13:00
Copyright (c) 2007-2016 ShareX Team
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;
using ShareX.HelpersLib;
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-12-11 09:25:20 +13:00
namespace ShareX.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 string APIURL { get; private set; }
2014-08-09 14:05:56 +12:00
public bool DirectLink { get; set; }
public MediaCrushUploader()
{
APIURL = "https://mediacru.sh";
}
public MediaCrushUploader(string apiURL)
{
APIURL = URLHelpers.FixPrefix(apiURL);
}
2014-01-05 07:12:52 +13:00
public override UploadResult Upload(Stream stream, string fileName)
{
WebExceptionThrow = 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, URLHelpers.CombineURL(APIURL, "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
while (!StopUploadRequested)
2014-01-05 07:12:52 +13:00
{
result.Response = SendRequest(HttpMethod.GET, URLHelpers.CombineURL(APIURL, "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(500);
2014-01-05 12:21:44 +13:00
break;
case "done":
case "ready":
MediaCrushBlob blob = jsonResponse[hash].ToObject<MediaCrushBlob>();
return UpdateResult(result, blob);
2014-01-05 12:21:44 +13:00
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>();
return UpdateResult(new UploadResult(), blob);
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, URLHelpers.CombineURL(APIURL, "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 UpdateResult(new UploadResult(response), blob);
2014-01-05 12:21:44 +13:00
}
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
}
private UploadResult UpdateResult(UploadResult result, MediaCrushBlob blob)
{
string url = URLHelpers.CombineURL(APIURL, blob.Hash);
if (DirectLink)
{
if (blob.Files != null && blob.Files.Length > 0)
{
if (blob.BlobType == "image")
{
url = blob.Files[0].URL;
}
else if (blob.BlobType == "video" || blob.BlobType == "audio")
{
url = URLHelpers.CombineURL(APIURL, blob.Hash + "/direct");
}
}
}
result.URL = url;
result.DeletionURL = URLHelpers.CombineURL(APIURL, blob.Hash + "/delete");
return result;
}
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; }
}
2014-01-05 11:46:18 +13:00
}