public async Task <bool> UpdateFiles() { QueryResults = null; HitItems.Clear(); FileItems.Clear(); if (Group != null) { var api = IndexApiClient.Create(cts.Token); var results = await api.GetFiles(Group); if (results is FileInfo[] entries) { foreach (var entry in entries.OrderBy(x => x.Path.ToUpperInvariant())) { FileItems.Add(new FileItem(entry.Fid, entry.Path, entry.Size, !Unchecked.Contains(entry.Fid))); } return(true); } else { return(false); } } else { return(true); } }
public async Task <bool> Authenticate(string username, string password) { var api = IndexApiClient.Create(cts.Token); var result = await api.Authenticate(username, password); if (result is ErrorResponse e) { Console.WriteLine("{0}", e.ErrorDescription); return(false); } return(true); }
public async Task <bool> UpdateGroups() { string lastSelection = Group; Groups.Clear(); var api = IndexApiClient.Create(cts.Token); var results = await api.GetFileGroups(); if (results is FileGroupInfo[] groups) { string defaultGroup = "default"; Groups.Add(defaultGroup); foreach (var group in groups.OrderBy(x => x.Name)) { if (group.Name != defaultGroup) { Groups.Add(group.Name); } } if (lastSelection != null && Groups.Contains(lastSelection)) { Group = lastSelection; NotifyOfChange("Group"); } return(true); } else { if (Group != null) { Group = null; NotifyOfChange("Group"); } return(false); } }
private async Task <FileContents> DownloadFile(int fid) { var contents = FileContents.Find(fid); if (contents == null) { var api = IndexApiClient.Create(cts.Token); var result = await api.DownloadFile(fid); if (result is FileContents c) { contents = c; } else if (result is Exception e) { throw e; } else { throw new NotImplementedException(); } } return(contents); }
public FileGroupService(CancellationToken ct) { client = IndexApiClient.Create(ct); }
public void Register(CommandLine commandLine, CommandQueue commandQueue) { commandLine .AddHandler("-print-id.next", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.GetIds(); task.Wait(); if (task.Result is IdStatus status) { Console.WriteLine("{0}", status); } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-reset-id.next", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.ResetIds(); task.Wait(); if (task.Result is IdStatus status) { Console.WriteLine("{0}", status); } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-print-unused-files", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.GetUnusedFiles(); task.Wait(); if (task.Result is FileInfo[] unused) { foreach (var info in unused) { Console.WriteLine("{0}", info); } } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-delete-unused-files", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.DeleteUnusedFiles(); task.Wait(); if (task.Result is FileInfo[] deleted) { if (deleted.Length > 0) { Console.Write("Deleted: "); foreach (var info in deleted) { Console.WriteLine("{0}", info); } } else { Console.WriteLine("Unused files not found."); } } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-print-unused-contents", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.GetUnusedContents(); task.Wait(); if (task.Result is int[] unused) { var sb = new StringBuilder(); foreach (int fid in unused) { sb.AppendFormat(" {0}", fid); } if (sb.Length > 0) { Console.WriteLine("{0}", sb.ToString(1, sb.Length - 1)); } } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-delete-unused-contents", (e) => { commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.DeleteUnusedContents(); task.Wait(); if (task.Result is int[] deleted) { if (deleted.Length > 0) { var sb = new StringBuilder(); foreach (int fid in deleted) { sb.AppendFormat(" {0}", fid); } Console.WriteLine("Deleted:{0}", sb.ToString()); } else { Console.WriteLine("Unused contents not found."); } } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddHandler("-print-index-stats", (e) => { if (!e.MoveNext()) { throw new Exception("Group name is not specified."); } var groupname = (string)e.Current; commandQueue.Add(() => { var client = IndexApiClient.Create(cts.Token); var task = client.GetIndexStats(groupname); task.Wait(); if (task.Result is IndexStats stats) { Console.WriteLine("{0}", stats); } else if (task.Result is Exception x) { throw x; } else { throw new NotImplementedException(); } }); }) .AddUsageHeader("Usage <diagnostics>:") .AddUsage("{0} -print-id.next", Program.Name) .AddUsage("{0} -reset-id.next", Program.Name) .AddUsage("{0} -print-unused-files", Program.Name) .AddUsage("{0} -delete-unused-files", Program.Name) .AddUsage("{0} -print-unused-contents", Program.Name) .AddUsage("{0} -delete-unused-contents", Program.Name) .AddUsage("{0} -print-index-stats GROUPNAME", Program.Name); }
public void Register(CommandLine commandLine, CommandQueue commandQueue) { commandLine .AddHandler("-index-api", (e) => { if (!e.MoveNext()) { throw new Exception("URL is not specified."); } var a = (string)e.Current; if (!a.StartsWith("http://") && !a.StartsWith("https://")) { throw new Exception("URL does not look valid."); } IndexApiClient.Url = a; }) .AddHandler("-username", (e) => { if (!e.MoveNext()) { throw new Exception("Username is not specified."); } IndexApiClient.ChangeUser((string)e.Current); }) .AddHandler("-print-extensions", (e) => { commandQueue.Add(() => { var list = pref.GetExtensions(); foreach (string ext in list) { Console.WriteLine("{0}", ext); } }); }) .AddHandler("-add-extensions", (e) => { if (!e.MoveNext()) { throw new Exception("Extensions are not specified."); } var exts = (string)e.Current; commandQueue.Add(() => { Console.WriteLine("Adding extensions setting..."); var res = pref.AddExtensions(exts); Console.WriteLine("{0}", res); }); }) .AddHandler("-clear-extensions", (e) => { commandQueue.Add(() => { Console.WriteLine("Clearing extensions setting..."); pref.ClearExtensions(); Console.WriteLine("Done."); }); }) .AddHandler("-print-skip-dirs", (e) => { commandQueue.Add(() => { var list = pref.GetSkipDirs(); foreach (string dir in list) { Console.WriteLine("{0}", dir); } }); }) .AddHandler("-add-skip-dirs", (e) => { if (!e.MoveNext()) { throw new Exception("Directories are not specified."); } var dirs = (string)e.Current; commandQueue.Add(() => { Console.WriteLine("Adding skip-dirs setting..."); pref.AddSkipDirs(dirs); Console.WriteLine("Done."); }); }) .AddHandler("-clear-skip-dirs", (e) => { commandQueue.Add(() => { Console.WriteLine("Clearing skip-dirs setting..."); pref.ClearSkipDirs(); Console.WriteLine("Done."); }); }) .AddTranslation("-u", "-username") .AddTranslation("-pe", "-print-extensions") .AddTranslation("-print-ext", "-print-extensions") .AddTranslation("-ext", "-add-extensions") .AddTranslation("-add-ext", "-add-extensions") .AddTranslation("-pd", "-print-skip-dirs") .AddTranslation("-skip", "-add-skip-dirs") .AddTranslation("-skip-dirs", "-add-skip-dirs") .AddUsageHeader("Usage <configuration>:") .AddUsage("{0} -print-extensions", Program.Name) .AddUsage("{0} -add-extensions EXT[,EXT2...]", Program.Name) .AddUsage("{0} -clear-extensions", Program.Name) .AddUsage("{0} -print-skip-dirs", Program.Name) .AddUsage("{0} -add-skip-dirs DIR[,DIR2...]", Program.Name) .AddUsage("{0} -clear-skip-dirs", Program.Name) .AddOption("-index-api URL") .AddOption("-username USERNAME"); }
public PreferenceService(CancellationToken ct) { client = IndexApiClient.Create(ct); }
public List <HitFile> FindTextSequence(string group, string[] sequence, SearchOptions head, SearchOptions tail) { const int LIMIT = 65536; List <HitRanges> rangesList; var client = IndexApiClient.Create(ct); if (sequence.Length == 1) { if (head == SearchOptions.Exact && tail == SearchOptions.Exact) { var task = client.FindText(group, sequence[0], SearchOptions.Exact); task.Wait(); if (task.Result is TextDistribution[] hits) { if (hits.Length == 0) { return(new List <HitFile>()); } rangesList = HitRangesListExtension.ToList(hits); } else if (task.Result is Exception e) { throw e; } else { throw new NotImplementedException(); } } else { var option = head == SearchOptions.EndsWith && tail == SearchOptions.StartsWith ? SearchOptions.Contains : head == SearchOptions.EndsWith && tail == SearchOptions.Exact ? SearchOptions.EndsWith : head == SearchOptions.Exact && tail == SearchOptions.StartsWith ? SearchOptions.StartsWith : throw new NotImplementedException(); { var task = client.FindText(group, sequence[0], option, LIMIT); task.Wait(); if (task.Result is TextDistribution[] hits) { if (hits.Length == 0) { return(new List <HitFile>()); } rangesList = HitRangesListExtension.ToList(hits); } else if (task.Result is Exception e) { throw e; } else { throw new NotImplementedException(); } } for (int offset = LIMIT; true; offset += LIMIT) { var task = client.FindText(group, sequence[0], option, LIMIT, offset); task.Wait(); if (task.Result is TextDistribution[] hits) { if (hits.Length == 0) { break; } rangesList.Merge(hits); } else if (task.Result is Exception e) { throw e; } else { throw new NotImplementedException(); } } } } else if (sequence.Length > 1) { { var task = client.FindText(group, sequence[0], head, LIMIT); task.Wait(); if (task.Result is TextDistribution[] hits) { if (hits.Length == 0) { return(new List <HitFile>()); } rangesList = HitRangesListExtension.ToList(hits); } else if (task.Result is Exception e) { throw e; } else { throw new NotImplementedException(); } } for (int offset = LIMIT; true; offset += LIMIT) { var task = client.FindText(group, sequence[0], head, LIMIT, offset); task.Wait(); if (task.Result is TextDistribution[] hits) { if (hits.Length == 0) { break; } rangesList.Merge(hits); } else if (task.Result is Exception e) { throw e; } else { throw new NotImplementedException(); } } for (int index = 1; index < sequence.Length - 1; index++) { var task = client.FindText(group, sequence[index], SearchOptions.Exact); task.Wait(); if (task.Result is TextDistribution[] hits) { if (hits.Length == 0 || rangesList.Append(hits).Count == 0) { return(new List <HitFile>()); } } else if (task.Result is Exception e) { throw e; } else { throw new NotImplementedException(); } } var last = new List <TextDistribution>(); { var task = client.FindText(group, sequence[sequence.Length - 1], tail, LIMIT); task.Wait(); if (task.Result is TextDistribution[] hits) { if (hits.Length == 0) { return(new List <HitFile>()); } last.AddRange(hits); } else if (task.Result is Exception e) { throw e; } else { throw new NotImplementedException(); } } for (int offset = LIMIT; true; offset += LIMIT) { var task = client.FindText(group, sequence[sequence.Length - 1], tail, LIMIT, offset); task.Wait(); if (task.Result is TextDistribution[] hits) { if (hits.Length == 0) { break; } last.Merge(hits); } else if (task.Result is Exception e) { throw e; } else { throw new NotImplementedException(); } } rangesList.Append(last); } else { rangesList = new List <HitRanges>(); } return(ToHitFileList(sequence, head, tail, rangesList)); }
public AuthenticationService(CancellationToken ct) { client = IndexApiClient.Create(ct); }
public UserService(CancellationToken ct) { client = IndexApiClient.Create(ct); }