Skip to content

Commit

Permalink
Added support for new URL format (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpailler authored Apr 7, 2020
1 parent 5ebbbd8 commit f548ef1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
6 changes: 4 additions & 2 deletions MegaApiClient.Tests/Context/AuthenticatedTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public class AuthenticatedTestContext : TestContext, IDisposable
internal const string MasterKeyHashV1 = "Q7LYmSw2Et4GjL9KWZKcq1oEzC1dX3wZDEdhS3YJICk";
internal const string MasterKeyHashV2 = "pRYA3b7JExchAvcTPaGpgl5h9U42iKncvXhPku7YIcA";

internal const string FileLink = "https://mega.nz/#!bkwkHC7D!AWJuto8_fhleAI2WG0RvACtKkL_s9tAtvBXXDUp2bQk";
internal const string FolderLink = "https://mega.nz/#F!e1ogxQ7T!ee4Q_ocD1bSLmNeg9B6kBw";
internal const string FileLinkLegacy = "https://mega.nz/#!bkwkHC7D!AWJuto8_fhleAI2WG0RvACtKkL_s9tAtvBXXDUp2bQk";
internal const string FileLink = "https://mega.nz/file/bkwkHC7D#AWJuto8_fhleAI2WG0RvACtKkL_s9tAtvBXXDUp2bQk";
internal const string FolderLinkLegacy = "https://mega.nz/#F!e1ogxQ7T!ee4Q_ocD1bSLmNeg9B6kBw";
internal const string FolderLink = "https://mega.nz/folder/e1ogxQ7T#ee4Q_ocD1bSLmNeg9B6kBw";
internal const string FileId = "P8BBzaTS";
internal const int FileSize = 523265;
internal const string FolderId = "e5IjHLLJ";
Expand Down
22 changes: 15 additions & 7 deletions MegaApiClient.Tests/NodeOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,29 @@ public void Rename_Succeeds(NodeType nodeType)
}
}

[Fact]
public void GetNodeFromLink_Browse_Succeeds()
[Theory]
[InlineData(AuthenticatedTestContext.FileLink)]
[InlineData(AuthenticatedTestContext.FileLinkLegacy)]
public void GetNodeFromLink_Browse_Succeeds(string fileLink)
{
var node = this.context.Client.GetNodeFromLink(new Uri(AuthenticatedTestContext.FileLink));
var node = this.context.Client.GetNodeFromLink(new Uri(fileLink));

Assert.NotNull(node);
Assert.Equal("SharedFile.jpg", node.Name);
Assert.Equal(523265, node.Size);
Assert.Equal(DateTime.Parse("2015-07-14T14:04:51.0000000+08:00"), node.ModificationDate);
}

[Fact]
public void GetNodesFromLink_Succeeds()
[Theory]
[InlineData(AuthenticatedTestContext.FolderLink, null)]
[InlineData(AuthenticatedTestContext.FolderLink, "/file/SELECTED_FILE_NODE_ID")]
[InlineData(AuthenticatedTestContext.FolderLink, "/folder/SELECTED_FOLDER_NODE_ID")]
[InlineData(AuthenticatedTestContext.FolderLinkLegacy, null)]
[InlineData(AuthenticatedTestContext.FolderLinkLegacy, "?SELECTED_FILE_NODE_ID")]
[InlineData(AuthenticatedTestContext.FolderLinkLegacy, "!SELECTED_FOLDER_NODE_ID")]
public void GetNodesFromLink_Succeeds(string folderLink, string suffix)
{
var nodes = this.context.Client.GetNodesFromLink(new Uri(AuthenticatedTestContext.FolderLink));
var nodes = this.context.Client.GetNodesFromLink(new Uri(folderLink + suffix));

Assert.Equal(4, nodes.Count());
INode node;
Expand Down Expand Up @@ -335,4 +343,4 @@ public void GetNodesFromLink_Succeeds()
Assert.Equal(DateTime.Parse("2018-03-13T11:37:51.0000000+07:00"), node.CreationDate);
}
}
}
}
53 changes: 44 additions & 9 deletions MegaApiClient/MegaApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ public Uri GetDownloadLink(INode node)
string response = this.Request<string>(request);

return new Uri(BaseUri, string.Format(
"/#{0}!{1}!{2}",
node.Type == NodeType.Directory ? "F" : string.Empty,
"/{0}/{1}#{2}",
node.Type == NodeType.Directory ? "folder" : "file",
response,
node.Type == NodeType.Directory ? nodeCrypto.SharedKey.ToBase64() : nodeCrypto.FullKey.ToBase64()));
}
Expand Down Expand Up @@ -1121,17 +1121,12 @@ private void EnsureLoggedOut()

private void GetPartsFromUri(Uri uri, out string id, out byte[] iv, out byte[] metaMac, out byte[] key)
{
Regex uriRegex = new Regex("#(?<type>F?)!(?<id>.+)!(?<key>.+)");
Match match = uriRegex.Match(uri.Fragment);
if (match.Success == false)
if (!this.TryGetPartsFromUri(uri, out id, out var decryptedKey, out var isFolder)
&& !this.TryGetPartsFromLegacyUri(uri, out id, out decryptedKey, out isFolder))
{
throw new ArgumentException(string.Format("Invalid uri. Unable to extract Id and Key from the uri {0}", uri));
}

id = match.Groups["id"].Value;
byte[] decryptedKey = match.Groups["key"].Value.FromBase64();
var isFolder = match.Groups["type"].Value == "F";

if (isFolder)
{
iv = null;
Expand All @@ -1144,6 +1139,46 @@ private void GetPartsFromUri(Uri uri, out string id, out byte[] iv, out byte[] m
}
}

private bool TryGetPartsFromUri(Uri uri, out string id, out byte[] decryptedKey, out bool isFolder)
{
Regex uriRegex = new Regex(@"/(?<type>(file|folder))/(?<id>[^#]+)#(?<key>[^$/]+)");
Match match = uriRegex.Match(uri.PathAndQuery + uri.Fragment);
if (match.Success)
{
id = match.Groups["id"].Value;
decryptedKey = match.Groups["key"].Value.FromBase64();
isFolder = match.Groups["type"].Value == "folder";
return true;
}
else
{
id = null;
decryptedKey = null;
isFolder = default;
return false;
}
}

private bool TryGetPartsFromLegacyUri(Uri uri, out string id, out byte[] decryptedKey, out bool isFolder)
{
Regex uriRegex = new Regex(@"#(?<type>F?)!(?<id>[^!]+)!(?<key>[^$!\?]+)");
Match match = uriRegex.Match(uri.Fragment);
if (match.Success)
{
id = match.Groups["id"].Value;
decryptedKey = match.Groups["key"].Value.FromBase64();
isFolder = match.Groups["type"].Value == "F";
return true;
}
else
{
id = null;
decryptedKey = null;
isFolder = default;
return false;
}
}

private IEnumerable<int> ComputeChunksSizesToUpload(long[] chunksPositions, long streamLength)
{
for (int i = 0; i < chunksPositions.Length; i++)
Expand Down

0 comments on commit f548ef1

Please sign in to comment.