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
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 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;
|
2016-06-28 05:20:55 +12:00
|
|
|
|
using ShareX.UploadersLib.Properties;
|
2014-07-23 10:32:14 +12:00
|
|
|
|
using System.Collections.Generic;
|
2016-06-28 05:20:55 +12:00
|
|
|
|
using System.Drawing;
|
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;
|
2016-05-01 20:09:11 +12:00
|
|
|
|
using System.Windows.Forms;
|
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
|
|
|
|
{
|
2016-03-23 06:24:41 +13:00
|
|
|
|
public class MediaFireFileUploaderService : FileUploaderService
|
|
|
|
|
{
|
|
|
|
|
public override FileDestination EnumValue { get; } = FileDestination.MediaFire;
|
|
|
|
|
|
2016-06-28 05:20:55 +12:00
|
|
|
|
public override Icon ServiceIcon => Resources.MediaFire;
|
|
|
|
|
|
2016-03-24 07:47:59 +13:00
|
|
|
|
public override bool CheckConfig(UploadersConfig config)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
return !string.IsNullOrEmpty(config.MediaFireUsername) && !string.IsNullOrEmpty(config.MediaFirePassword);
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 21:53:11 +12:00
|
|
|
|
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
return new MediaFire(APIKeys.MediaFireAppId, APIKeys.MediaFireApiKey, config.MediaFireUsername, config.MediaFirePassword)
|
2016-03-23 06:24:41 +13:00
|
|
|
|
{
|
2016-03-24 07:47:59 +13:00
|
|
|
|
UploadPath = NameParser.Parse(NameParserType.URL, config.MediaFirePath),
|
|
|
|
|
UseLongLink = config.MediaFireUseLongLink
|
2016-03-23 06:24:41 +13:00
|
|
|
|
};
|
|
|
|
|
}
|
2016-05-01 20:09:11 +12:00
|
|
|
|
|
|
|
|
|
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpMediaFire;
|
2016-03-23 06:24:41 +13:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 10:32:14 +12:00
|
|
|
|
public sealed class MediaFire : FileUploader
|
|
|
|
|
{
|
2014-07-23 13:59:45 +12:00
|
|
|
|
public string UploadPath { get; set; }
|
|
|
|
|
public bool UseLongLink { get; set; }
|
|
|
|
|
|
2018-10-07 08:09:15 +13:00
|
|
|
|
private static readonly string apiUrl = "https://www.mediafire.com/api/";
|
|
|
|
|
private static readonly int pollInterval = 1000;
|
|
|
|
|
private readonly string appId, apiKey, user, pasw;
|
|
|
|
|
private string sessionToken, signatureTime;
|
|
|
|
|
private int signatureKey;
|
|
|
|
|
|
2014-07-23 13:59:45 +12:00
|
|
|
|
public MediaFire(string appId, string apiKey, string user, string pasw)
|
2014-07-23 10:32:14 +12:00
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
this.appId = appId;
|
|
|
|
|
this.apiKey = apiKey;
|
|
|
|
|
this.user = user;
|
|
|
|
|
this.pasw = pasw;
|
2014-07-23 10:32:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override UploadResult Upload(Stream stream, string fileName)
|
|
|
|
|
{
|
2014-07-23 13:48:11 +12:00
|
|
|
|
AllowReportProgress = false;
|
2014-07-23 10:32:14 +12:00
|
|
|
|
GetSessionToken();
|
2014-07-23 13:48:11 +12:00
|
|
|
|
AllowReportProgress = true;
|
2014-07-23 10:32:14 +12:00
|
|
|
|
string key = SimpleUpload(stream, fileName);
|
2014-07-23 13:48:11 +12:00
|
|
|
|
AllowReportProgress = false;
|
2021-06-10 10:14:01 +12:00
|
|
|
|
string url;
|
|
|
|
|
while ((url = PollUpload(key, fileName)) == null)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(pollInterval);
|
|
|
|
|
}
|
2014-07-23 13:38:46 +12:00
|
|
|
|
return new UploadResult() { IsSuccess = true, URL = url };
|
2014-07-23 10:32:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetSessionToken()
|
|
|
|
|
{
|
2016-09-17 19:07:02 +12:00
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
2018-10-07 08:09:15 +13:00
|
|
|
|
args.Add("email", user);
|
|
|
|
|
args.Add("password", pasw);
|
|
|
|
|
args.Add("application_id", appId);
|
2014-07-23 10:32:14 +12:00
|
|
|
|
args.Add("token_version", "2");
|
|
|
|
|
args.Add("response_format", "json");
|
|
|
|
|
args.Add("signature", GetInitSignature());
|
2018-10-07 08:09:15 +13:00
|
|
|
|
string respStr = SendRequestMultiPart(apiUrl + "user/get_session_token.php", args);
|
2014-07-23 10:32:14 +12:00
|
|
|
|
GetSessionTokenResponse resp = DeserializeResponse<GetSessionTokenResponse>(respStr);
|
|
|
|
|
EnsureSuccess(resp);
|
|
|
|
|
if (resp.session_token == null || resp.time == null || resp.secret_key == null)
|
|
|
|
|
throw new IOException("Invalid response");
|
2018-10-07 08:09:15 +13:00
|
|
|
|
sessionToken = resp.session_token;
|
|
|
|
|
signatureTime = resp.time;
|
|
|
|
|
signatureKey = (int)resp.secret_key;
|
2014-07-23 10:32:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string SimpleUpload(Stream stream, string fileName)
|
|
|
|
|
{
|
2016-09-17 19:07:02 +12:00
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
2018-10-07 08:09:15 +13:00
|
|
|
|
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));
|
2019-01-07 00:47:37 +13:00
|
|
|
|
string url = URLHelpers.CreateQueryString(apiUrl + "upload/simple.php", args);
|
2016-12-27 05:47:46 +13:00
|
|
|
|
UploadResult res = SendRequestFile(url, stream, fileName, "Filedata");
|
2014-07-23 10:32:14 +12:00
|
|
|
|
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)
|
|
|
|
|
{
|
2016-09-17 19:07:02 +12:00
|
|
|
|
Dictionary<string, string> args = new Dictionary<string, string>();
|
2018-10-07 08:09:15 +13:00
|
|
|
|
args.Add("session_token", sessionToken);
|
2014-07-23 10:32:14 +12:00
|
|
|
|
args.Add("key", uploadKey);
|
|
|
|
|
args.Add("filename", fileName);
|
|
|
|
|
args.Add("response_format", "json");
|
|
|
|
|
args.Add("signature", GetSignature("upload/poll_upload.php", args));
|
2018-10-07 08:09:15 +13:00
|
|
|
|
string respStr = SendRequestMultiPart(apiUrl + "upload/poll_upload.php", args);
|
2014-07-23 10:32:14 +12:00
|
|
|
|
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()
|
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
string signatureStr = user + pasw + appId + apiKey;
|
2014-07-23 10:32:14 +12:00
|
|
|
|
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
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
string keyStr = (signatureKey % 256).ToString(CultureInfo.InvariantCulture);
|
2014-07-23 10:32:14 +12:00
|
|
|
|
string urlStr = CreateNonEscapedQuery("/api/" + urlSuffix, args);
|
2018-10-07 08:09:15 +13:00
|
|
|
|
string signatureStr = keyStr + signatureTime + urlStr;
|
2014-07-23 10:32:14 +12:00
|
|
|
|
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
|
|
|
|
{
|
2018-10-07 08:09:15 +13:00
|
|
|
|
signatureKey = (int)(((long)signatureKey * 16807) % 2147483647);
|
2014-07-23 10:32:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private T DeserializeResponse<T>(string s) where T : new()
|
|
|
|
|
{
|
|
|
|
|
var refObj = new { response = new T() };
|
2018-10-18 05:06:06 +13:00
|
|
|
|
object obj = JsonConvert.DeserializeObject(s, refObj.GetType());
|
2014-07-23 10:32:14 +12:00
|
|
|
|
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);
|
2018-05-17 01:27:11 +12:00
|
|
|
|
res[(2 * i) + 1] = IntToChar(b[i] & 0xf);
|
2014-07-23 10:32:14 +12:00
|
|
|
|
}
|
|
|
|
|
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; }
|
2014-07-23 13:38:46 +12:00
|
|
|
|
public string filename { get; set; }
|
2014-07-23 10:32:14 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-23 13:19:24 +12:00
|
|
|
|
}
|