From d07175ca9b4f0887631165fe7082310ed1fd3ad0 Mon Sep 17 00:00:00 2001 From: Gregoire Pailler Date: Sat, 29 Jan 2022 15:55:44 +0800 Subject: [PATCH] Update samples by adding namespace and replacing INodeInfo with INode --- docs/articles/faq.md | 2 +- docs/articles/samples.md | 41 +++++++++++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/docs/articles/faq.md b/docs/articles/faq.md index 3a732bf..5fa97b8 100644 --- a/docs/articles/faq.md +++ b/docs/articles/faq.md @@ -27,7 +27,7 @@ async void Main() client.LoginAnonymous(); Uri fileLink = new Uri("https://mega.nz/#!bkwkHC7D!AWJuto8_fhleAI2WG0RvACtKkL_s9tAtvBXXDUp2bQk"); - INodeInfo node = client.GetNodeFromLink(fileLink); + INode node = client.GetNodeFromLink(fileLink); IProgress progressHandler = new Progress(x => Console.WriteLine("{0}%", x)); await client.DownloadFileAsync(fileLink, node.Name, progressHandler); diff --git a/docs/articles/samples.md b/docs/articles/samples.md index f1c733d..b74468b 100644 --- a/docs/articles/samples.md +++ b/docs/articles/samples.md @@ -4,9 +4,11 @@ uid: samples ### List all files / folders on your Mega account ```csharp +using CG.Web.MegaApiClient; + void Main() { - var client = new MegaApiClient(); + MegaApiClient client = new MegaApiClient(); client.Login("username@domain.com", "passw0rd"); // GetNodes retrieves all files/folders metadata from Mega @@ -37,13 +39,15 @@ void DisplayNodesRecursive(IEnumerable nodes, INode parent, int level = 0 ### Download file from a Mega link ```csharp +using CG.Web.MegaApiClient; + void Main() { - var client = new MegaApiClient(); + MegaApiClient client = new MegaApiClient(); client.LoginAnonymous(); - Uri fileLink = new Uri("https://mega.nz/#!bkwkHC7D!AWJuto8_fhleAI2WG0RvACtKkL_s9tAtvBXXDUp2bQk"); - INodeInfo node = client.GetNodeFromLink(fileLink); + Uri fileLink = new Uri("https://mega.nz/file/W0UAgJaK#XOYyTETrIy8daz3_dw3fdh6Hh8EFEdrnbyoop1r9R6g"); + INode node = client.GetNodeFromLink(fileLink); Console.WriteLine($"Downloading {node.Name}"); client.DownloadFile(fileLink, node.Name); @@ -55,29 +59,48 @@ void Main() ### Download folder content from a Mega link ```csharp +using CG.Web.MegaApiClient; + void Main() { - var client = new MegaApiClient(); + MegaApiClient client = new MegaApiClient(); client.LoginAnonymous(); - Uri folderLink = new Uri("https://mega.nz/#F!e1ogxQ7T!ee4Q_ocD1bSLmNeg9B6kBw"); + Uri folderLink = new Uri("https://mega.nz/folder/e4diDZ7T#iJnegBO_m6OXBQp27lHCrg"); IEnumerable nodes = client.GetNodesFromLink(folderLink); foreach (INode node in nodes.Where(x => x.Type == NodeType.File)) { - Console.WriteLine($"Downloading {node.Name}"); - client.DownloadFile(node, node.Name); + string parents = GetParents(node, nodes); + Directory.CreateDirectory(parents); + Console.WriteLine($"Downloading {parents}\\{node.Name}"); + client.DownloadFile(node, Path.Combine(parents, node.Name)); } client.Logout(); } + +string GetParents(INode node, IEnumerable nodes) +{ + List parents = new List(); + while (node.ParentId != null) + { + INode parentNode = nodes.Single(x => x.Id == node.ParentId); + parents.Insert(0, parentNode.Name); + node = parentNode; + } + + return string.Join('\\', parents); +} ``` ### Upload a file to your Mega account and retrieve public download link ```csharp +using CG.Web.MegaApiClient; + void Main() { - var client = new MegaApiClient(); + MegaApiClient client = new MegaApiClient(); client.Login("username@domain.com", "passw0rd"); IEnumerable nodes = client.GetNodes();