ShareX/ShareX.UploadersLib/FileUploaders/Mega.cs

225 lines
7 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2023-01-10 09:31:02 +13:00
Copyright (c) 2007-2023 ShareX Team
2013-11-03 23:53:49 +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)
using CG.Web.MegaApiClient;
2016-06-28 05:20:55 +12:00
using ShareX.UploadersLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
using System.Collections.Generic;
2016-06-28 05:20:55 +12:00
using System.Drawing;
2013-11-03 23:53:49 +13:00
using System.IO;
using System.Linq;
using System.Windows.Forms;
2013-11-03 23:53:49 +13:00
2014-12-11 09:25:20 +13:00
namespace ShareX.UploadersLib.FileUploaders
2013-11-03 23:53:49 +13:00
{
public class MegaFileUploaderService : FileUploaderService
{
public override FileDestination EnumValue { get; } = FileDestination.Mega;
2016-06-28 05:20:55 +12:00
public override Icon ServiceIcon => Resources.Mega;
public override bool CheckConfig(UploadersConfig config)
{
return config.MegaAuthInfos != null && config.MegaAuthInfos.Email != null && config.MegaAuthInfos.Hash != null &&
config.MegaAuthInfos.PasswordAesKey != null;
}
public override GenericUploader CreateUploader(UploadersConfig config, TaskReferenceHelper taskInfo)
{
2020-06-01 13:17:04 +12:00
return new Mega(config.MegaAuthInfos?.GetMegaApiClientAuthInfos(), config.MegaParentNodeId);
}
public override TabPage GetUploadersConfigTabPage(UploadersConfigForm form) => form.tpMega;
}
2013-11-03 23:53:49 +13:00
public sealed class Mega : FileUploader, IWebClient
{
// Pack all chunks in a single upload fragment
// (by default, MegaApiClient splits files in 1MB fragments and do multiple uploads)
2017-12-06 06:44:57 +13:00
// It allows to have a consistent upload progression in ShareX
private const int UploadChunksPackSize = -1;
2018-10-07 08:09:15 +13:00
private readonly MegaApiClient megaClient;
private readonly MegaApiClient.AuthInfos authInfos;
private readonly string parentNodeId;
2013-11-03 23:53:49 +13:00
2016-04-07 04:02:48 +12:00
public Mega() : this(null, null)
2013-11-03 23:53:49 +13:00
{
}
2016-04-07 04:02:48 +12:00
public Mega(MegaApiClient.AuthInfos authInfos) : this(authInfos, null)
2013-11-03 23:53:49 +13:00
{
}
public Mega(MegaApiClient.AuthInfos authInfos, string parentNodeId)
{
AllowReportProgress = false;
2017-12-06 06:44:57 +13:00
Options options = new Options(chunksPackSize: UploadChunksPackSize);
2018-10-07 08:09:15 +13:00
megaClient = new MegaApiClient(options, this);
this.authInfos = authInfos;
this.parentNodeId = parentNodeId;
2013-11-03 23:53:49 +13:00
}
public bool TryLogin()
{
try
{
Login();
return true;
}
catch (ApiException)
{
return false;
}
}
private void Login()
{
2018-10-07 08:09:15 +13:00
if (authInfos == null)
2013-11-03 23:53:49 +13:00
{
2018-10-07 08:09:15 +13:00
megaClient.LoginAnonymous();
2013-11-03 23:53:49 +13:00
}
else
{
2018-10-07 08:09:15 +13:00
megaClient.Login(authInfos);
2013-11-03 23:53:49 +13:00
}
}
internal IEnumerable<DisplayNode> GetDisplayNodes()
{
2018-10-07 08:09:15 +13:00
IEnumerable<INode> nodes = megaClient.GetNodes().Where(n => n.Type == NodeType.Directory || n.Type == NodeType.Root).ToArray();
2013-11-03 23:53:49 +13:00
List<DisplayNode> displayNodes = new List<DisplayNode>();
2015-06-29 01:04:27 +12:00
foreach (INode node in nodes)
2013-11-03 23:53:49 +13:00
{
displayNodes.Add(new DisplayNode(node, nodes));
}
displayNodes.Sort((x, y) => string.Compare(x.DisplayName, y.DisplayName, StringComparison.CurrentCultureIgnoreCase));
displayNodes.Insert(0, DisplayNode.EmptyNode);
return displayNodes;
}
2015-06-29 01:04:27 +12:00
public INode GetParentNode()
2013-11-03 23:53:49 +13:00
{
2018-10-07 08:09:15 +13:00
if (authInfos == null || parentNodeId == null)
2013-11-03 23:53:49 +13:00
{
2018-10-07 08:09:15 +13:00
return megaClient.GetNodes().SingleOrDefault(n => n.Type == NodeType.Root);
2013-11-03 23:53:49 +13:00
}
2018-10-07 08:09:15 +13:00
return megaClient.GetNodes().SingleOrDefault(n => n.Id == parentNodeId);
2013-11-03 23:53:49 +13:00
}
public override UploadResult Upload(Stream stream, string fileName)
{
Login();
2018-10-07 08:09:15 +13:00
INode createdNode = megaClient.Upload(stream, fileName, GetParentNode());
2013-11-03 23:53:49 +13:00
UploadResult res = new UploadResult();
res.IsURLExpected = true;
2018-10-07 08:09:15 +13:00
res.URL = megaClient.GetDownloadLink(createdNode).ToString();
2013-11-03 23:53:49 +13:00
return res;
}
#region IWebClient
2021-05-11 00:06:28 +12:00
public Stream GetRequestRaw(Uri url)
{
throw new NotImplementedException();
}
2013-11-03 23:53:49 +13:00
public string PostRequestJson(Uri url, string jsonData)
{
2019-03-15 00:06:54 +13:00
return SendRequest(HttpMethod.POST, url.ToString(), jsonData, RequestHelpers.ContentTypeJSON);
2013-11-03 23:53:49 +13:00
}
public string PostRequestRaw(Uri url, Stream dataStream)
{
try
{
AllowReportProgress = true;
2016-12-23 20:24:38 +13:00
return SendRequest(HttpMethod.POST, url.ToString(), dataStream, "application/octet-stream");
2013-11-03 23:53:49 +13:00
}
finally
{
AllowReportProgress = false;
}
}
2021-05-11 00:06:28 +12:00
public Stream PostRequestRawAsStream(Uri url, Stream dataStream)
2013-11-03 23:53:49 +13:00
{
throw new NotImplementedException();
}
#endregion IWebClient
internal class DisplayNode
{
public static readonly DisplayNode EmptyNode = new DisplayNode();
private DisplayNode()
{
DisplayName = "[Select a folder]";
}
2015-06-29 01:04:27 +12:00
public DisplayNode(INode node, IEnumerable<INode> nodes)
2013-11-03 23:53:49 +13:00
{
Node = node;
DisplayName = GenerateDisplayName(node, nodes);
}
2015-06-29 01:04:27 +12:00
public INode Node { get; private set; }
2013-11-03 23:53:49 +13:00
public string DisplayName { get; private set; }
2015-06-29 01:04:27 +12:00
private string GenerateDisplayName(INode node, IEnumerable<INode> nodes)
2013-11-03 23:53:49 +13:00
{
List<string> nodesTree = new List<string>();
2015-06-29 01:04:27 +12:00
INode parent = node;
2013-11-03 23:53:49 +13:00
do
{
if (parent.Type == NodeType.Directory)
{
nodesTree.Add(parent.Name);
}
else
{
nodesTree.Add(parent.Type.ToString());
}
parent = nodes.FirstOrDefault(n => n.Id == parent.ParentId);
}
while (parent != null);
nodesTree.Reverse();
return string.Join(@"\", nodesTree.ToArray());
}
}
}
}