internal void BackupFile(MegaNodeHelper node, int?version = null) { if (node == null) { return; } lock (serverNodes) { var newPath = Path.Combine( Path.GetDirectoryName(node.Path), String.Format("{0}.backup{1}{2}", Path.GetFileNameWithoutExtension(node.Path), version, Path.GetExtension(node.Path))); var found = FindNodeByPath(newPath); if (found != null) { BackupFile(node, version == null ? 1 : ++version); } else { var newName = Path.GetFileName(newPath); node.SetName(newName); api.UpdateNodeAttrSync(node.Node); } } }
/// <summary> /// UPDATE (move) /// </summary> internal SyncedNodeAttributes MoveFile(string oldPath, string newPath, string expectedId) { lock (serverNodes) { var target = AssertExist(oldPath); AssertId(target, expectedId); AssertPathClear(newPath); // rename if (Path.GetDirectoryName(oldPath) == Path.GetDirectoryName(newPath)) { target.SetName(Path.GetFileName(newPath)); api.UpdateNodeAttrSync(target.Node); } else // move { MegaNodeHelper parent = AssertParentForPath(newPath); api.MoveNodeSync(target.Node.Id, parent.Node.Id); target.Parent = FindNodeByPath(Path.GetDirectoryName(newPath)); } return(new SyncedNodeAttributes { Path = newPath, Id = target.Node.Id }); } }
public MegaNode CreateDirectory(string path) { var folders = Path.GetDirectoryName(path.TrimStart(new char[] { '\\' })) .Split(new char[] { '\\' }); if (string.IsNullOrEmpty(folders[0])) { return(rootFolder.Node); } var parent = rootFolder; MegaNodeHelper helper = null; foreach (var folder in folders) { lock (serverNodes) { helper = serverNodes .Where(h => h.Node.Attributes.Name == folder && h.Node.ParentId == parent.Node.Id) .FirstOrDefault(); if (helper == null) { var n = api.CreateFolderSync(parent.Node.Id, folder); helper = new MegaNodeHelper { Node = n, Parent = parent == rootFolder ? null : parent }; } parent = helper; } } return(helper.Node); }
private void AssertId(MegaNodeHelper target, string expectedId) { if (target.Node.Id != expectedId) { throw new MegaStoreConcurrencyException("The node has different id") { Node = target }; } }
private void AssertNoChildren(MegaNodeHelper node) { if (IsDirectory(node)) { var children = serverNodes.Where(n => n.Node.ParentId == node.Node.Id).FirstOrDefault(); if (children != null) { throw new MegaStoreConstraintException(MegaStoreConstraintType.NotEmpty, "The folder is not empty"); } } }
private void AddChildren(List <MegaNodeHelper> target, MegaNodeHelper parent, List <MegaNode> serverNodes, bool r = false) { var children = serverNodes.Where(n => n.ParentId == parent.Node.Id); foreach (var child in children) { var c = new MegaNodeHelper { Node = child, // do not set the root folder as parent Parent = r ? parent : null }; target.Add(c); AddChildren(target, c, serverNodes, true); } }
private void RefreshNodeList() { lock (serverNodes) { refreshing = true; refreshTimer.Stop(); serverNodes = new List <MegaNodeHelper>(); List <MegaNode> nodes = null; nodes = api.GetNodesSync(); // todo ignore root folder in the trash var rootNode = nodes.Where(n => n.Attributes.Name == rootFolderName).FirstOrDefault(); if (rootNode == null) { rootNode = api.CreateFolderSync(nodes.Where(n => n.Type == MegaNodeType.RootFolder).First().Id, rootFolderName); } rootFolder = new MegaNodeHelper { Node = rootNode }; if (string.IsNullOrEmpty(recyclingId)) { var recycleNode = nodes.Where(n => n.Type == MegaNodeType.Trash).FirstOrDefault(); if (recycleNode == null) { RecycleDeletedItems = false; } else { recyclingId = recycleNode.Id; } } AddChildren(serverNodes, rootFolder, nodes); refreshTimer.Start(); refreshing = false; } OnUpdated(); }
public bool IsDirectory(MegaNodeHelper node) { return(node.Node.Type == MegaNodeType.Folder); }