#region License Information (GPL v3) /* ShareX - A program that allows you to take screenshots and share any file type Copyright © 2007-2015 ShareX Developers 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 . */ #endregion License Information (GPL v3) using CG.Web.MegaApiClient; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace ShareX.UploadersLib.FileUploaders { public sealed class Mega : FileUploader, IWebClient { private readonly MegaApiClient _megaClient; private readonly MegaApiClient.AuthInfos _authInfos; private readonly string _parentNodeId; public Mega() : this(null, null) { } public Mega(MegaApiClient.AuthInfos authInfos) : this(authInfos, null) { } public Mega(MegaApiClient.AuthInfos authInfos, string parentNodeId) { AllowReportProgress = false; _megaClient = new MegaApiClient(this); _authInfos = authInfos; _parentNodeId = parentNodeId; } public bool TryLogin() { try { Login(); return true; } catch (ApiException) { return false; } } private void Login() { if (_authInfos == null) { _megaClient.LoginAnonymous(); } else { _megaClient.Login(_authInfos); } } internal IEnumerable GetDisplayNodes() { IEnumerable nodes = _megaClient.GetNodes().Where(n => n.Type == NodeType.Directory || n.Type == NodeType.Root).ToArray(); List displayNodes = new List(); foreach (INode node in nodes) { 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; } public INode GetParentNode() { if (_authInfos == null || _parentNodeId == null) { return _megaClient.GetNodes().SingleOrDefault(n => n.Type == NodeType.Root); } return _megaClient.GetNodes().SingleOrDefault(n => n.Id == _parentNodeId); } public override UploadResult Upload(Stream stream, string fileName) { Login(); INode createdNode = _megaClient.Upload(stream, fileName, GetParentNode()); UploadResult res = new UploadResult(); res.IsURLExpected = true; res.URL = _megaClient.GetDownloadLink(createdNode).ToString(); return res; } #region IWebClient public string PostRequestJson(Uri url, string jsonData) { return SendRequestJSON(url.ToString(), jsonData); } public string PostRequestRaw(Uri url, Stream dataStream) { try { AllowReportProgress = true; return SendRequestStream(url.ToString(), dataStream, "application/octet-stream"); } finally { AllowReportProgress = false; } } public Stream GetRequestRaw(Uri url) { throw new NotImplementedException(); } #endregion IWebClient internal class DisplayNode { public static readonly DisplayNode EmptyNode = new DisplayNode(); private DisplayNode() { DisplayName = "[Select a folder]"; } public DisplayNode(INode node, IEnumerable nodes) { Node = node; DisplayName = GenerateDisplayName(node, nodes); } public INode Node { get; private set; } public string DisplayName { get; private set; } private string GenerateDisplayName(INode node, IEnumerable nodes) { List nodesTree = new List(); INode parent = node; 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()); } } } }