ShareX/ShareX.UploadersLib/FileUploaders/MediaFire.cs

237 lines
9.2 KiB
C#
Raw Normal View History

2014-07-23 13:19:24 +12:00
#region License Information (GPL v3)
/*
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-07-23 13:19:24 +12: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/michalx2
using Newtonsoft.Json;
2014-12-11 12:19:28 +13:00
using ShareX.HelpersLib;
2014-07-23 10:32:14 +12:00
using System.Collections.Generic;
2014-07-23 13:19:24 +12:00
using System.Globalization;
2014-07-23 10:32:14 +12:00
using System.IO;
using System.Linq;
2014-07-23 13:19:24 +12:00
using System.Security.Cryptography;
2014-07-23 10:32:14 +12:00
using System.Text;
2014-07-23 13:19:24 +12:00
using System.Threading;
2014-07-23 10:32:14 +12:00
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib.FileUploaders
2014-07-23 10:32:14 +12:00
{
public sealed class MediaFire : FileUploader
{
private static readonly string _apiUrl = "https://www.mediafire.com/api/";
private static readonly int _pollInterval = 1000;
2014-07-23 13:59:45 +12:00
private readonly string _appId, _apiKey, _user, _pasw;
2014-07-23 10:32:14 +12:00
private string _sessionToken, _signatureTime;
private int _signatureKey;
2014-07-23 13:59:45 +12:00
public string UploadPath { get; set; }
public bool UseLongLink { get; set; }
public MediaFire(string appId, string apiKey, string user, string pasw)
2014-07-23 10:32:14 +12:00
{
_appId = appId;
_apiKey = apiKey;
_user = user;
_pasw = pasw;
}
public override UploadResult Upload(Stream stream, string fileName)
{
AllowReportProgress = false;
2014-07-23 10:32:14 +12:00
GetSessionToken();
AllowReportProgress = true;
2014-07-23 10:32:14 +12:00
string key = SimpleUpload(stream, fileName);
AllowReportProgress = false;
string url = null;
while ((url = PollUpload(key, fileName)) == null) Thread.Sleep(_pollInterval);
return new UploadResult() { IsSuccess = true, URL = url };
2014-07-23 10:32:14 +12:00
}
private void GetSessionToken()
{
var args = new Dictionary<string, string>();
args.Add("email", _user);
args.Add("password", _pasw);
args.Add("application_id", _appId);
args.Add("token_version", "2");
args.Add("response_format", "json");
args.Add("signature", GetInitSignature());
string respStr = SendRequest(HttpMethod.POST, _apiUrl + "user/get_session_token.php", args);
GetSessionTokenResponse resp = DeserializeResponse<GetSessionTokenResponse>(respStr);
EnsureSuccess(resp);
if (resp.session_token == null || resp.time == null || resp.secret_key == null)
throw new IOException("Invalid response");
_sessionToken = resp.session_token;
_signatureTime = resp.time;
_signatureKey = (int)resp.secret_key;
}
private string SimpleUpload(Stream stream, string fileName)
{
var args = new Dictionary<string, string>();
args.Add("session_token", _sessionToken);
2014-07-23 13:59:45 +12:00
args.Add("path", UploadPath);
2014-07-23 10:32:14 +12:00
args.Add("response_format", "json");
args.Add("signature", GetSignature("upload/simple.php", args));
string url = CreateQuery(_apiUrl + "upload/simple.php", args);
UploadResult res = UploadData(stream, url, fileName, "Filedata");
if (!res.IsSuccess) throw new IOException(res.ErrorsToString());
SimpleUploadResponse resp = DeserializeResponse<SimpleUploadResponse>(res.Response);
EnsureSuccess(resp);
if (resp.doupload.result != 0 || resp.doupload.key == null) throw new IOException("Invalid response");
return resp.doupload.key;
}
private string PollUpload(string uploadKey, string fileName)
{
var args = new Dictionary<string, string>();
args.Add("session_token", _sessionToken);
args.Add("key", uploadKey);
args.Add("filename", fileName);
args.Add("response_format", "json");
args.Add("signature", GetSignature("upload/poll_upload.php", args));
string respStr = SendRequest(HttpMethod.POST, _apiUrl + "upload/poll_upload.php", args);
PollUploadResponse resp = DeserializeResponse<PollUploadResponse>(respStr);
EnsureSuccess(resp);
if (resp.doupload.result == null || resp.doupload.status == null) throw new IOException("Invalid response");
2014-07-23 13:19:24 +12:00
if (resp.doupload.result != 0 || resp.doupload.fileerror != null)
{
throw new IOException(string.Format("Couldn't upload the file: {0}", resp.doupload.description ?? "Unknown error"));
2014-07-23 10:32:14 +12:00
}
2014-07-23 13:19:24 +12:00
if (resp.doupload.status == 99)
{
2014-07-23 10:32:14 +12:00
if (resp.doupload.quickkey == null) throw new IOException("Invalid response");
2014-07-23 13:59:45 +12:00
string url = URLHelpers.CombineURL("http://www.mediafire.com/view", resp.doupload.quickkey);
if (UseLongLink) url = URLHelpers.CombineURL(url, URLHelpers.URLEncode(resp.doupload.filename));
return url;
2014-07-23 10:32:14 +12:00
}
return null;
}
private void EnsureSuccess(MFResponse resp)
{
if (resp.result != "Success")
throw new IOException(string.Format("Couldn't upload the file: {0}", resp.message ?? "Unknown error"));
if (resp.new_key == "yes") NextSignatureKey();
}
private string GetInitSignature()
{
string signatureStr = _user + _pasw + _appId + _apiKey;
byte[] signatureBytes = Encoding.ASCII.GetBytes(signatureStr);
2014-07-23 13:19:24 +12:00
SHA1 sha1Gen = SHA1.Create();
2014-07-23 10:32:14 +12:00
byte[] sha1Bytes = sha1Gen.ComputeHash(signatureBytes);
return BytesToString(sha1Bytes);
}
2014-07-23 13:19:24 +12:00
private string GetSignature(string urlSuffix, Dictionary<string, string> args)
2014-07-23 10:32:14 +12:00
{
2014-07-23 13:19:24 +12:00
string keyStr = (_signatureKey % 256).ToString(CultureInfo.InvariantCulture);
2014-07-23 10:32:14 +12:00
string urlStr = CreateNonEscapedQuery("/api/" + urlSuffix, args);
string signatureStr = keyStr + _signatureTime + urlStr;
byte[] signatureBytes = Encoding.ASCII.GetBytes(signatureStr);
2014-07-23 13:19:24 +12:00
MD5 md5gen = MD5.Create();
2014-07-23 10:32:14 +12:00
byte[] md5Bytes = md5gen.ComputeHash(signatureBytes);
return BytesToString(md5Bytes);
}
2014-07-23 13:19:24 +12:00
private void NextSignatureKey()
2014-07-23 10:32:14 +12:00
{
_signatureKey = (int)(((long)_signatureKey * 16807) % 2147483647);
}
private T DeserializeResponse<T>(string s) where T : new()
{
var refObj = new { response = new T() };
var obj = JsonConvert.DeserializeObject(s, refObj.GetType());
return (T)obj.GetType().GetProperty("response").GetValue(obj, null);
}
private static char IntToChar(int x)
{
if (x < 10) return (char)(x + '0');
return (char)(x - 10 + 'a');
}
private static string BytesToString(byte[] b)
{
char[] res = new char[b.Length * 2];
2014-07-23 13:19:24 +12:00
for (int i = 0; i < b.Length; ++i)
{
2014-07-23 10:32:14 +12:00
res[2 * i] = IntToChar(b[i] >> 4);
res[2 * i + 1] = IntToChar(b[i] & 0xf);
}
return new string(res);
}
private static string CreateNonEscapedQuery(string url, Dictionary<string, string> args)
{
if (args != null && args.Count > 0)
return url + "?" + string.Join("&", args.Select(x => x.Key + "=" + x.Value).ToArray());
return url;
}
private class MFResponse
{
public string result { get; set; }
public int? error { get; set; }
public string message { get; set; }
public string new_key { get; set; }
}
private class GetSessionTokenResponse : MFResponse
{
public string session_token { get; set; }
public int? secret_key { get; set; }
public string time { get; set; }
}
private class SimpleUploadResponse : MFResponse
{
public DoUpload doupload { get; set; }
public class DoUpload
{
public int? result { get; set; }
public string key { get; set; }
}
}
private class PollUploadResponse : MFResponse
{
public DoUpload doupload { get; set; }
public class DoUpload
{
public int? result { get; set; }
public int? status { get; set; }
public string description { get; set; }
public int? fileerror { get; set; }
public string quickkey { get; set; }
public string filename { get; set; }
2014-07-23 10:32:14 +12:00
}
}
}
2014-07-23 13:19:24 +12:00
}