diff --git a/MegaApiClient.Tests/Data/SampleZipFile.zip b/MegaApiClient.Tests/Data/SampleZipFile.zip new file mode 100644 index 0000000..d746cc2 Binary files /dev/null and b/MegaApiClient.Tests/Data/SampleZipFile.zip differ diff --git a/MegaApiClient.Tests/DownloadUpload.cs b/MegaApiClient.Tests/DownloadUpload.cs index 7cd8785..aa7b0aa 100644 --- a/MegaApiClient.Tests/DownloadUpload.cs +++ b/MegaApiClient.Tests/DownloadUpload.cs @@ -191,15 +191,28 @@ public static IEnumerable DownloadLinkToFileInvalidParameter } } - [Fact] - public void DownloadLink_ToFile_Succeeds() + [Theory] + [JsonInputsDataAttribute(new object[] { "Data/SampleZipFile.zip" }, new[] { "ZipFileLink" })] + [JsonInputsDataAttribute(new object[] { "Data/SampleFile.jpg" }, new[] { "FileLink" })] + public void DownloadLink_ToFile_Succeeds(string resultFile, string uri) { - const string ExpectedResultFile = "Data/SampleFile.jpg"; + var outFile = GetTempFileName(); + Context.Client.DownloadFile(new Uri(uri), outFile); + + Assert.Equal(File.ReadAllBytes(GetAbsoluteFilePath(resultFile)), File.ReadAllBytes(outFile)); + } + + [Theory] + [JsonInputsDataAttribute(new object[] { "Data/SampleZipFile.zip" }, new[] { "ZipFileLink" })] + [JsonInputsDataAttribute(new object[] { "Data/SampleFile.jpg" }, new[] { "FileLink" })] + public void GetNodeFromLink_And_Download_ToFile_Succeeds(string resultFile, string uri) + { + var node = Context.Client.GetNodeFromLink(new Uri(uri)); var outFile = GetTempFileName(); - Context.Client.DownloadFile(new Uri(AuthenticatedTestContext.Inputs.FileLink), outFile); + Context.Client.DownloadFile(node, outFile); - Assert.Equal(File.ReadAllBytes(GetAbsoluteFilePath(ExpectedResultFile)), File.ReadAllBytes(outFile)); + Assert.Equal(File.ReadAllBytes(GetAbsoluteFilePath(resultFile)), File.ReadAllBytes(outFile)); } [Fact] diff --git a/MegaApiClient/MegaApiClient.cs b/MegaApiClient/MegaApiClient.cs index 63d66cf..6af5b11 100644 --- a/MegaApiClient/MegaApiClient.cs +++ b/MegaApiClient/MegaApiClient.cs @@ -596,7 +596,7 @@ public Stream Download(INode node, CancellationToken? cancellationToken = null) EnsureLoggedIn(); // Retrieve download URL - var downloadRequest = new DownloadUrlRequest(node); + var downloadRequest = node is PublicNode publicNode && publicNode.ParentId == null ? (RequestBase)new DownloadUrlRequestFromId(node.Id) : new DownloadUrlRequest(node); var downloadResponse = Request(downloadRequest); Stream dataStream = new BufferedStream(_webClient.GetRequestRaw(new Uri(downloadResponse.Url))); @@ -665,13 +665,13 @@ public INode GetNodeFromLink(Uri uri) EnsureLoggedIn(); - GetPartsFromUri(uri, out var id, out _, out _, out var key); + GetPartsFromUri(uri, out var id, out var iv, out var metaMac, out var key); // Retrieve attributes var downloadRequest = new DownloadUrlRequestFromId(id); var downloadResponse = Request(downloadRequest); - return new Node(id, downloadResponse, key); + return new PublicNode(new Node(id, downloadResponse, key, iv, metaMac), null); } /// diff --git a/MegaApiClient/Node.cs b/MegaApiClient/Node.cs index c6d7a1d..d90a3e4 100644 --- a/MegaApiClient/Node.cs +++ b/MegaApiClient/Node.cs @@ -24,13 +24,16 @@ public Node(byte[] masterKey, ref List sharedKeys) _sharedKeys = sharedKeys; } - internal Node(string id, DownloadUrlResponse downloadResponse, byte[] key) + internal Node(string id, DownloadUrlResponse downloadResponse, byte[] key, byte[] iv, byte[] metaMac) { Id = id; Attributes = Crypto.DecryptAttributes(downloadResponse.SerializedAttributes.FromBase64(), key); Size = downloadResponse.Size; Type = NodeType.File; FileAttributes = DeserializeFileAttributes(downloadResponse.SerializedFileAttributes); + Key = key; + Iv = iv; + MetaMac = metaMac; } #region Public properties @@ -271,9 +274,16 @@ private bool IsShareRoot { get { - var serializedKey = _node.SerializedKey.Split('/')[0]; - var splitPosition = serializedKey.IndexOf(":", StringComparison.Ordinal); - return serializedKey.Substring(0, splitPosition) == Id; + if (_node.SerializedKey == null) + { + return true; + } + else + { + var serializedKey = _node.SerializedKey.Split('/')[0]; + var splitPosition = serializedKey.IndexOf(":", StringComparison.Ordinal); + return serializedKey.Substring(0, splitPosition) == Id; + } } } }