Skip to content

Commit babae20

Browse files
committed
Introduced INode
1 parent cfe05c7 commit babae20

File tree

6 files changed

+222
-151
lines changed

6 files changed

+222
-151
lines changed

MegaApiClient-3.5/MegaApiClient-3.5.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
<Compile Include="..\MegaApiClient\Extensions.cs">
7676
<Link>Extensions.cs</Link>
7777
</Compile>
78+
<Compile Include="..\MegaApiClient\INode.cs">
79+
<Link>INode.cs</Link>
80+
</Compile>
7881
<Compile Include="..\MegaApiClient\IWebClient.cs">
7982
<Link>IWebClient.cs</Link>
8083
</Compile>
@@ -87,6 +90,9 @@
8790
<Compile Include="..\MegaApiClient\MegaApiClient.cs">
8891
<Link>MegaApiClient.cs</Link>
8992
</Compile>
93+
<Compile Include="..\MegaApiClient\Node.cs">
94+
<Link>Node.cs</Link>
95+
</Compile>
9096
<Compile Include="..\MegaApiClient\Properties\AssemblyInfo.cs">
9197
<Link>Properties\AssemblyInfo.cs</Link>
9298
</Compile>

MegaApiClient-4.0/MegaApiClient-4.0.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
<Compile Include="..\MegaApiClient\Extensions.cs">
7676
<Link>Extensions.cs</Link>
7777
</Compile>
78+
<Compile Include="..\MegaApiClient\INode.cs">
79+
<Link>INode.cs</Link>
80+
</Compile>
7881
<Compile Include="..\MegaApiClient\IWebClient.cs">
7982
<Link>IWebClient.cs</Link>
8083
</Compile>
@@ -87,6 +90,9 @@
8790
<Compile Include="..\MegaApiClient\MegaApiClient.cs">
8891
<Link>MegaApiClient.cs</Link>
8992
</Compile>
93+
<Compile Include="..\MegaApiClient\Node.cs">
94+
<Link>Node.cs</Link>
95+
</Compile>
9096
<Compile Include="..\MegaApiClient\Properties\AssemblyInfo.cs">
9197
<Link>Properties\AssemblyInfo.cs</Link>
9298
</Compile>

MegaApiClient/INode.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace CG.Web.MegaApiClient
4+
{
5+
public interface INode : IEquatable<INode>
6+
{
7+
string Id { get; }
8+
9+
string ParentId { get; }
10+
11+
string Owner { get; }
12+
13+
NodeType Type { get; }
14+
15+
long Size { get; }
16+
17+
string Name { get; }
18+
19+
DateTime LastModificationDate { get; }
20+
}
21+
22+
internal interface INodeCrypto
23+
{
24+
byte[] DecryptedKey { get; }
25+
26+
byte[] Key { get; }
27+
28+
byte[] Iv { get; }
29+
30+
byte[] MetaMac { get; }
31+
}
32+
33+
public enum NodeType
34+
{
35+
File = 0,
36+
Directory,
37+
Root,
38+
Inbox,
39+
Trash
40+
}
41+
}

MegaApiClient/JsonSerialization.cs

+8-139
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2525

2626
#endregion
2727

28-
using System;
29-
using System.Diagnostics;
30-
using System.Linq;
3128
using System.Runtime.Serialization;
3229
using Newtonsoft.Json;
3330
using Newtonsoft.Json.Linq;
@@ -154,7 +151,7 @@ public void OnDeserialized(StreamingContext ctx)
154151

155152
internal class DeleteRequest : RequestBase
156153
{
157-
public DeleteRequest(Node node)
154+
public DeleteRequest(INode node)
158155
: base("d")
159156
{
160157
this.Node = node.Id;
@@ -171,7 +168,7 @@ public DeleteRequest(Node node)
171168

172169
internal class GetDownloadLinkRequest : RequestBase
173170
{
174-
public GetDownloadLinkRequest(Node node)
171+
public GetDownloadLinkRequest(INode node)
175172
: base("l")
176173
{
177174
this.Id = node.Id;
@@ -188,7 +185,7 @@ public GetDownloadLinkRequest(Node node)
188185

189186
internal class CreateNodeRequest : RequestBase
190187
{
191-
private CreateNodeRequest(Node parentNode, NodeType type, string attributes, string key, string completionHandle)
188+
private CreateNodeRequest(INode parentNode, NodeType type, string attributes, string key, string completionHandle)
192189
: base("p")
193190
{
194191
this.ParentId = parentNode.Id;
@@ -204,12 +201,12 @@ private CreateNodeRequest(Node parentNode, NodeType type, string attributes, str
204201
};
205202
}
206203

207-
public static CreateNodeRequest CreateFileNodeRequest(Node parentNode, string attributes, string key, string completionHandle)
204+
public static CreateNodeRequest CreateFileNodeRequest(INode parentNode, string attributes, string key, string completionHandle)
208205
{
209206
return new CreateNodeRequest(parentNode, NodeType.File, attributes, key, completionHandle);
210207
}
211208

212-
public static CreateNodeRequest CreateFolderNodeRequest(Node parentNode, string attributes, string key)
209+
public static CreateNodeRequest CreateFolderNodeRequest(INode parentNode, string attributes, string key)
213210
{
214211
return new CreateNodeRequest(parentNode, NodeType.Directory, attributes, key, "xxxxxxxx");
215212
}
@@ -266,7 +263,7 @@ internal class UploadUrlResponse
266263

267264
internal class DownloadUrlRequest : RequestBase
268265
{
269-
public DownloadUrlRequest(Node node)
266+
public DownloadUrlRequest(INode node)
270267
: base("g")
271268
{
272269
this.Id = node.Id;
@@ -311,7 +308,7 @@ internal class DownloadUrlResponse
311308

312309
internal class MoveRequest : RequestBase
313310
{
314-
public MoveRequest(Node node, Node destinationParentNode)
311+
public MoveRequest(INode node, INode destinationParentNode)
315312
: base("m")
316313
{
317314
this.Id = node.Id;
@@ -341,133 +338,5 @@ public Attributes(string name)
341338
public string Name { get; set; }
342339
}
343340

344-
#endregion
345-
346-
347-
#region Node
348-
349-
[DebuggerDisplay("Type: {Type} - Name: {Name} - Id: {Id}")]
350-
public class Node : IEquatable<Node>
351-
{
352-
private static readonly DateTime OriginalDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
353-
354-
#region Public properties
355-
356-
[JsonProperty("h")]
357-
public string Id { get; private set; }
358-
359-
[JsonProperty("p")]
360-
public string ParentId { get; private set; }
361-
362-
[JsonProperty("u")]
363-
public string Owner { get; private set; }
364-
365-
[JsonProperty("t")]
366-
public NodeType Type { get; private set; }
367-
368-
[JsonProperty("s")]
369-
public long Size { get; private set; }
370-
371-
[JsonIgnore]
372-
public string Name { get; private set; }
373-
374-
[JsonIgnore]
375-
public DateTime LastModificationDate { get; private set; }
376-
377-
[JsonIgnore]
378-
internal byte[] DecryptedKey { get; private set; }
379-
380-
[JsonIgnore]
381-
internal byte[] Key { get; private set; }
382-
383-
[JsonIgnore]
384-
internal byte[] Iv { get; private set; }
385-
386-
[JsonIgnore]
387-
internal byte[] MetaMac { get; private set; }
388-
389-
#endregion
390-
391-
#region Deserialization
392-
393-
[JsonProperty("ts")]
394-
private long SerializedLastModificationDate { get; set; }
395-
396-
[JsonProperty("a")]
397-
private string SerializedAttributes { get; set; }
398-
399-
[JsonProperty("k")]
400-
private string SerializedKey { get; set; }
401-
402-
[OnDeserialized]
403-
public void OnDeserialized(StreamingContext ctx)
404-
{
405-
byte[] masterKey = (byte[])((object[])ctx.Context)[0];
406-
GetNodesResponse nodesResponse = (GetNodesResponse)((object[])ctx.Context)[1];
407-
408-
this.LastModificationDate = OriginalDateTime.AddSeconds(this.SerializedLastModificationDate).ToLocalTime();
409-
410-
if (this.Type == NodeType.File || this.Type == NodeType.Directory)
411-
{
412-
int splitPosition = this.SerializedKey.IndexOf(":", StringComparison.InvariantCulture);
413-
byte[] encryptedKey = this.SerializedKey.Substring(splitPosition + 1).FromBase64();
414-
415-
this.DecryptedKey = Crypto.DecryptKey(encryptedKey, masterKey);
416-
this.Key = this.DecryptedKey;
417-
418-
// If node is shared, we need to retrieve shared masterkey
419-
if (nodesResponse.SharedKeys != null)
420-
{
421-
string owner = this.SerializedKey.Substring(0, splitPosition);
422-
GetNodesResponse.SharedKey sharedKey = nodesResponse.SharedKeys.FirstOrDefault(x => x.Id == owner);
423-
if (sharedKey != null)
424-
{
425-
masterKey = Crypto.DecryptKey(sharedKey.Key.FromBase64(), masterKey);
426-
427-
if (this.Type == NodeType.Directory)
428-
{
429-
this.DecryptedKey = masterKey;
430-
}
431-
432-
this.Key = Crypto.DecryptKey(encryptedKey, masterKey);
433-
}
434-
}
435-
436-
if (this.Type == NodeType.File)
437-
{
438-
byte[] iv, metaMac, fileKey;
439-
Crypto.GetPartsFromDecryptedKey(this.DecryptedKey, out iv, out metaMac, out fileKey);
440-
441-
this.Iv = iv;
442-
this.MetaMac = metaMac;
443-
this.Key = fileKey;
444-
}
445-
446-
Attributes attributes = Crypto.DecryptAttributes(this.SerializedAttributes.FromBase64(), this.Key);
447-
this.Name = attributes.Name;
448-
}
449-
}
450-
451-
#endregion
452-
453-
#region Equality
454-
455-
public bool Equals(Node other)
456-
{
457-
return other != null && this.Id == other.Id;
458-
}
459-
460-
#endregion
461-
}
462-
463-
public enum NodeType
464-
{
465-
File = 0,
466-
Directory,
467-
Root,
468-
Inbox,
469-
Trash
470-
}
471-
472-
#endregion
341+
#endregion\
473342
}

0 commit comments

Comments
 (0)