private static bool SetOption(string option) { switch (option) { case "-?": case "-h": case "--help": _help = true; break; // ls options case "-R": _lsOption1 |= NodesOptions.Recursive; break; case "-l": _lsOption2 |= LsOptions.LongView; break; default: Trace.WriteLine($"unknow option \"{option}\""); return(false); } return(true); }
//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(); } } }
public static void Ls(string[] parameters, NodesOptions option1, LsOptions option2) { if (parameters.Length > 1) { Trace.WriteLine("syntax error"); return; } if (parameters.Length == 1) { Cd(parameters); } MegaClient megaClient = GetMegaClient(); //NodesOptions options = NodesOptions.Directory | NodesOptions.File; TraceNodes(megaClient.GetNodes(GetMegaEnvironment().GetDirectory(), option1), option1, option2); }
public static void TraceNodes(IEnumerable <MegaNode> nodes, NodesOptions option1, LsOptions option2) { int fileCount = 0; int directoryCount = 0; bool recursive = (option1 & NodesOptions.Recursive) == NodesOptions.Recursive; bool longView = (option2 & LsOptions.LongView) == LsOptions.LongView; if (longView) { Trace.WriteLine("LastModificationDate Size Path"); } foreach (MegaNode node in nodes) { if (longView) { Trace.WriteLine($"{node.Node.LastModificationDate} {node.Node.Size,15:### ### ### ###} {node.Path}"); } else if (recursive) { Trace.WriteLine(node.Path); } else { Trace.WriteLine(node.Name); } if (node.Node.Type == NodeType.File) { fileCount++; } else { directoryCount++; } } Trace.WriteLine($"{fileCount} files, {directoryCount} directories, total {fileCount + directoryCount}"); }
public static void Test_TraceMegaNodes_02(string name, string directory = "/", NodesOptions options = NodesOptions.Default, int limit = 0, bool log = false) { MegaClient megaClient = new MegaClient(); string email, password; if (!GetMegaLogin(name, out email, out password)) { return; } megaClient.Email = email; megaClient.Password = password; megaClient.Login(); TraceNodes(megaClient.GetNodes(directory, options), limit: limit, log: log); }