Beispiel #1
0
        //private class MegaNodeEnum
        //{
        //    public MegaNode Node;
        //    public string Path;
        //    public IEnumerator<INode> Childs;
        //}

        //public void LoadMegaNodes(string file)
        //{
        //    _megaNodes = zMongo.ReadFileAs<Dictionary<string, Dictionary<string, List<MegaNode>>>>(file);
        //}

        //public void SaveMegaNodes(string file)
        //{
        //    GetMegaNodes().zSave(file, jsonIndent: true);
        //}

        //public void UploadFile(string file, string megaDirectory, Action<double> progression = null)
        //{
        //    string filepath = "filetoupload";
        //    using (var stream = new ProgressionStream(new FileStream(file, FileMode.Open), progression))
        //    {
        //        var parent = _client.GetNodes().FirstOrDefault(x => x.Type == NodeType.Root);
        //        _client.Upload(stream, Path.GetFileName(filepath), parent);
        //    }
        //}

        //public void DownloadWithProgression()
        //{
        //    using (var fileStream = new FileStream(filePath, FileMode.Create))
        //    {
        //        using (var downloadStream = new ProgressionStream(client.Download(node), this.PrintProgression))
        //        {
        //            downloadStream.CopyTo(fileStream);
        //        }
        //    }
        //}

        //private MegaNode GetDirectoryNode(string directory)
        //{
        //    if (directory != "/" && directory.EndsWith("/"))
        //        directory = directory.TrimEnd('/');
        //    if (_megaNodes.ContainsKey(directory))
        //        return _megaNodes[directory];
        //}

        //public IEnumerable<MegaNode> GetNodes(string path = "/")
        //{
        //    GetMegaNodes();
        //    bool directory = false;
        //    string path2 = path;
        //    if (path != "/" && path.EndsWith("/"))
        //    {
        //        //path2 = path.Substring(0, path.Length - 1);
        //        path2 = path.TrimEnd('/');
        //        directory = true;
        //    }
        //    if (!_megaNodes.ContainsKey(path2))
        //    {
        //        // search a file
        //        bool foundFile = false;
        //        if (!directory)
        //        {
        //            int i = path.LastIndexOf('/');
        //            if (i > 0)
        //            {
        //                string path3 = path.Substring(0, i);
        //                if (_megaNodes.ContainsKey(path3))
        //                {
        //                    var childs = _megaNodes[path3];
        //                    string file = path.Substring(i + 1);
        //                    if (childs.ContainsKey(file))
        //                    {
        //                        foundFile = true;
        //                        foreach (MegaNode node in childs[file])
        //                            yield return node;
        //                    }
        //                }
        //            }
        //        }
        //        if (!foundFile)
        //            Trace.WriteLine($"file not found \"{path}\"");
        //        yield break;
        //    }
        //    Stack<IEnumerator<MegaNode>> stack = new Stack<IEnumerator<MegaNode>>();
        //    stack.Push(_megaNodes[path2].Values.SelectMany(nodes => nodes).GetEnumerator());
        //    while (stack.Count > 0)
        //    {
        //        IEnumerator<MegaNode> enumerator = stack.Peek();
        //        if (enumerator.MoveNext())
        //        {
        //            MegaNode megaNode = enumerator.Current;
        //            yield return megaNode;
        //            if ((megaNode.Type == NodeType.Directory || megaNode.Type == NodeType.Root || megaNode.Type == NodeType.Inbox || megaNode.Type == NodeType.Trash) && _megaNodes.ContainsKey(megaNode.Path))
        //                stack.Push(_megaNodes[megaNode.Path].Values.SelectMany(nodes => nodes).GetEnumerator());
        //        }
        //        else
        //            stack.Pop();
        //    }
        //}

        public IEnumerable <MegaNode> GetNodes(string directory = null, NodesOptions options = NodesOptions.Default)
        {
            GetMegaNodes();
            //bool directory = false;
            //string path2 = directory;
            if (directory == null)
            {
                directory = "/";
            }
            if (directory != "/" && directory.EndsWith("/"))
            {
                directory = directory.TrimEnd('/');
            }
            if (!_megaNodes.ContainsKey(directory))
            {
                Trace.WriteLine($"directory not found \"{directory}\"");
                yield break;
            }
            bool returnFile      = (options & NodesOptions.File) == NodesOptions.File;
            bool returnDirectory = (options & NodesOptions.Directory) == NodesOptions.Directory;
            bool recursive       = (options & NodesOptions.Recursive) == NodesOptions.Recursive;
            Stack <IEnumerator <MegaNode> > stack      = new Stack <IEnumerator <MegaNode> >();
            IEnumerator <MegaNode>          enumerator = _megaNodes[directory].Childs.GetEnumerator();

            stack.Push(enumerator);
            while (true)
            {
                if (enumerator.MoveNext())
                {
                    MegaNode megaNode = enumerator.Current;
                    if (megaNode.Node.Type != NodeType.File)
                    {
                        if (returnDirectory)
                        {
                            yield return(megaNode);
                        }
                        if (recursive)
                        {
                            enumerator = ((MegaDirectoryNode)megaNode).Childs.GetEnumerator();
                            stack.Push(enumerator);
                        }
                    }
                    else if (returnFile)
                    {
                        yield return(megaNode);
                    }
                }
                else
                {
                    stack.Pop();
                    if (stack.Count == 0)
                    {
                        break;
                    }
                    enumerator = stack.Peek();
                }
            }
        }
Beispiel #2
0
        // not used
        //private IEnumerable<MegaDirectoryNode_v2> BrowseDirectoryNodes(Dictionary<string, MegaDirectoryNode_v2> megaNodes)
        //{
        //    Stack<IEnumerator<MegaNode_v2>> stack = new Stack<IEnumerator<MegaNode_v2>>();
        //    foreach (MegaDirectoryNode_v2 rootNode in new MegaDirectoryNode_v2[] { _root, _inbox, _trash })
        //    {
        //        yield return rootNode;
        //        IEnumerator<MegaNode_v2> enumerator = rootNode.Childs.GetEnumerator();
        //        stack.Push(enumerator);
        //        while (true)
        //        {
        //            if (enumerator.MoveNext())
        //            {
        //                MegaNode_v2 node = enumerator.Current;
        //                //yield return node;
        //                if (node.Node.Type == NodeType.Directory)
        //                {
        //                    MegaDirectoryNode_v2 directoryNode = (MegaDirectoryNode_v2)node;
        //                    yield return directoryNode;
        //                    enumerator = directoryNode.Childs.GetEnumerator();
        //                    stack.Push(enumerator);
        //                }
        //            }
        //            else
        //            {
        //                stack.Pop();
        //                if (stack.Count == 0)
        //                    break;
        //                enumerator = stack.Peek();
        //            }
        //        }
        //    }
        //}

        private IEnumerable <MegaNode> BrowseNodes(Dictionary <string, MegaDirectoryNode> megaNodes)
        {
            Stack <IEnumerator <MegaNode> > stack = new Stack <IEnumerator <MegaNode> >();

            foreach (MegaDirectoryNode rootNode in new MegaDirectoryNode[] { _root, _inbox, _trash })
            {
                yield return(rootNode);

                IEnumerator <MegaNode> enumerator = rootNode.Childs.GetEnumerator();
                stack.Push(enumerator);
                while (true)
                {
                    if (enumerator.MoveNext())
                    {
                        MegaNode node = enumerator.Current;
                        yield return(node);

                        if (node.Node.Type == NodeType.Directory)
                        {
                            MegaDirectoryNode directoryNode = (MegaDirectoryNode)node;
                            //yield return directoryNode;
                            enumerator = directoryNode.Childs.GetEnumerator();
                            stack.Push(enumerator);
                        }
                    }
                    else
                    {
                        stack.Pop();
                        if (stack.Count == 0)
                        {
                            break;
                        }
                        enumerator = stack.Peek();
                    }
                }
            }
        }
Beispiel #3
0
        private Dictionary <string, MegaDirectoryNode> GetMegaNodes1(IEnumerable <INode> nodes)
        {
            // Dictionary : one node by directory, key is mega id
            Dictionary <string, MegaDirectoryNode> megaNodes = new Dictionary <string, MegaDirectoryNode>();

            _dictionaryNodes1_nodeWithUnknowParent = 0;
            _dictionaryNodes1_nodeAlreadyCreated   = 0;
            _nodeCount          = 0;
            _fileNodeCount      = 0;
            _directoryNodeCount = 0;
            foreach (INode node in nodes)
            {
                _nodeCount++;

                //if (type == NodeType.Root)
                //    _root = node;
                //else if (type == NodeType.Inbox)
                //    _inbox = node;
                //else if (type == NodeType.Trash)
                //    _trash = node;

                // MegaNode_v2 : Id, Node, Path, Childs

                MegaDirectoryNode parentMegaNode = null;
                string            parentId       = node.ParentId;
                if (parentId != null && parentId != "")
                {
                    if (!megaNodes.TryGetValue(parentId, out parentMegaNode))
                    {
                        _dictionaryNodes1_nodeWithUnknowParent++;
                        parentMegaNode = new MegaDirectoryNode {
                            Id = parentId
                        };
                        megaNodes.Add(parentId, parentMegaNode);
                        //Trace.WriteLine($"nodeWithUnknowParent : Id {node.Id} Name {node.Name}");
                    }
                }

                MegaNode megaNode;
                NodeType type = node.Type;
                if (type != NodeType.File)
                {
                    string            id = node.Id;
                    MegaDirectoryNode megaDirectoryNode;
                    if (!megaNodes.TryGetValue(id, out megaDirectoryNode))
                    {
                        megaDirectoryNode = new MegaDirectoryNode {
                            Id = id, Node = node, Name = GetName(node)
                        };
                        megaNodes.Add(id, megaDirectoryNode);
                    }
                    else
                    {
                        _dictionaryNodes1_nodeAlreadyCreated++;
                        if (megaDirectoryNode.Node != null)
                        {
                            throw new PBException($"node should be null");
                        }
                        megaDirectoryNode.Node = node;
                        megaDirectoryNode.Name = GetName(node);
                        //Trace.WriteLine($"nodeAlreadyCreated : Id {node.Id} Name {node.Name}");
                    }

                    if (type == NodeType.Root)
                    {
                        _root = megaDirectoryNode;
                    }
                    else if (type == NodeType.Inbox)
                    {
                        _inbox = megaDirectoryNode;
                    }
                    else if (type == NodeType.Trash)
                    {
                        _trash = megaDirectoryNode;
                    }

                    megaNode = megaDirectoryNode;
                    if (type == NodeType.Directory)
                    {
                        _directoryNodeCount++;
                    }
                }
                else
                {
                    megaNode = new MegaNode {
                        Id = node.Id, Node = node, Name = GetName(node)
                    };
                    _fileNodeCount++;
                }

                if (parentMegaNode != null)
                {
                    megaNode.Parent = parentMegaNode;
                    parentMegaNode.Childs.Add(megaNode);
                }
            }
            return(megaNodes);
        }