Skip to content

Commit

Permalink
Add support to download public node (retrieved with GetNodeFromLink) …
Browse files Browse the repository at this point in the history
…with Download(INode) method (#196)

* #195 Add support to download public node (retrieved with GetNodeFromLink) with Download(INode) method

* Use proper DownloadRequest for public nodes created by GetNodesFromLink
  • Loading branch information
gpailler authored Nov 24, 2021
1 parent b9be9d9 commit eb3cb91
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
Binary file added MegaApiClient.Tests/Data/SampleZipFile.zip
Binary file not shown.
23 changes: 18 additions & 5 deletions MegaApiClient.Tests/DownloadUpload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,28 @@ public static IEnumerable<object[]> 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]
Expand Down
6 changes: 3 additions & 3 deletions MegaApiClient/MegaApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DownloadUrlResponse>(downloadRequest);

Stream dataStream = new BufferedStream(_webClient.GetRequestRaw(new Uri(downloadResponse.Url)));
Expand Down Expand Up @@ -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<DownloadUrlResponse>(downloadRequest);

return new Node(id, downloadResponse, key);
return new PublicNode(new Node(id, downloadResponse, key, iv, metaMac), null);
}

/// <summary>
Expand Down
18 changes: 14 additions & 4 deletions MegaApiClient/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ public Node(byte[] masterKey, ref List<SharedKey> 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
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down

0 comments on commit eb3cb91

Please sign in to comment.