Skip to content

Commit eb3cb91

Browse files
authored
Add support to download public node (retrieved with GetNodeFromLink) 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
1 parent b9be9d9 commit eb3cb91

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed
502 KB
Binary file not shown.

MegaApiClient.Tests/DownloadUpload.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,28 @@ public static IEnumerable<object[]> DownloadLinkToFileInvalidParameter
191191
}
192192
}
193193

194-
[Fact]
195-
public void DownloadLink_ToFile_Succeeds()
194+
[Theory]
195+
[JsonInputsDataAttribute(new object[] { "Data/SampleZipFile.zip" }, new[] { "ZipFileLink" })]
196+
[JsonInputsDataAttribute(new object[] { "Data/SampleFile.jpg" }, new[] { "FileLink" })]
197+
public void DownloadLink_ToFile_Succeeds(string resultFile, string uri)
196198
{
197-
const string ExpectedResultFile = "Data/SampleFile.jpg";
199+
var outFile = GetTempFileName();
200+
Context.Client.DownloadFile(new Uri(uri), outFile);
201+
202+
Assert.Equal(File.ReadAllBytes(GetAbsoluteFilePath(resultFile)), File.ReadAllBytes(outFile));
203+
}
204+
205+
[Theory]
206+
[JsonInputsDataAttribute(new object[] { "Data/SampleZipFile.zip" }, new[] { "ZipFileLink" })]
207+
[JsonInputsDataAttribute(new object[] { "Data/SampleFile.jpg" }, new[] { "FileLink" })]
208+
public void GetNodeFromLink_And_Download_ToFile_Succeeds(string resultFile, string uri)
209+
{
210+
var node = Context.Client.GetNodeFromLink(new Uri(uri));
198211

199212
var outFile = GetTempFileName();
200-
Context.Client.DownloadFile(new Uri(AuthenticatedTestContext.Inputs.FileLink), outFile);
213+
Context.Client.DownloadFile(node, outFile);
201214

202-
Assert.Equal(File.ReadAllBytes(GetAbsoluteFilePath(ExpectedResultFile)), File.ReadAllBytes(outFile));
215+
Assert.Equal(File.ReadAllBytes(GetAbsoluteFilePath(resultFile)), File.ReadAllBytes(outFile));
203216
}
204217

205218
[Fact]

MegaApiClient/MegaApiClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ public Stream Download(INode node, CancellationToken? cancellationToken = null)
596596
EnsureLoggedIn();
597597

598598
// Retrieve download URL
599-
var downloadRequest = new DownloadUrlRequest(node);
599+
var downloadRequest = node is PublicNode publicNode && publicNode.ParentId == null ? (RequestBase)new DownloadUrlRequestFromId(node.Id) : new DownloadUrlRequest(node);
600600
var downloadResponse = Request<DownloadUrlResponse>(downloadRequest);
601601

602602
Stream dataStream = new BufferedStream(_webClient.GetRequestRaw(new Uri(downloadResponse.Url)));
@@ -665,13 +665,13 @@ public INode GetNodeFromLink(Uri uri)
665665

666666
EnsureLoggedIn();
667667

668-
GetPartsFromUri(uri, out var id, out _, out _, out var key);
668+
GetPartsFromUri(uri, out var id, out var iv, out var metaMac, out var key);
669669

670670
// Retrieve attributes
671671
var downloadRequest = new DownloadUrlRequestFromId(id);
672672
var downloadResponse = Request<DownloadUrlResponse>(downloadRequest);
673673

674-
return new Node(id, downloadResponse, key);
674+
return new PublicNode(new Node(id, downloadResponse, key, iv, metaMac), null);
675675
}
676676

677677
/// <summary>

MegaApiClient/Node.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ public Node(byte[] masterKey, ref List<SharedKey> sharedKeys)
2424
_sharedKeys = sharedKeys;
2525
}
2626

27-
internal Node(string id, DownloadUrlResponse downloadResponse, byte[] key)
27+
internal Node(string id, DownloadUrlResponse downloadResponse, byte[] key, byte[] iv, byte[] metaMac)
2828
{
2929
Id = id;
3030
Attributes = Crypto.DecryptAttributes(downloadResponse.SerializedAttributes.FromBase64(), key);
3131
Size = downloadResponse.Size;
3232
Type = NodeType.File;
3333
FileAttributes = DeserializeFileAttributes(downloadResponse.SerializedFileAttributes);
34+
Key = key;
35+
Iv = iv;
36+
MetaMac = metaMac;
3437
}
3538

3639
#region Public properties
@@ -271,9 +274,16 @@ private bool IsShareRoot
271274
{
272275
get
273276
{
274-
var serializedKey = _node.SerializedKey.Split('/')[0];
275-
var splitPosition = serializedKey.IndexOf(":", StringComparison.Ordinal);
276-
return serializedKey.Substring(0, splitPosition) == Id;
277+
if (_node.SerializedKey == null)
278+
{
279+
return true;
280+
}
281+
else
282+
{
283+
var serializedKey = _node.SerializedKey.Split('/')[0];
284+
var splitPosition = serializedKey.IndexOf(":", StringComparison.Ordinal);
285+
return serializedKey.Substring(0, splitPosition) == Id;
286+
}
277287
}
278288
}
279289
}

0 commit comments

Comments
 (0)