ApiClient.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using FNZCM.BlazorWasm.UI;
  2. using FNZCM.BlazorWasm.UI.Components;
  3. using FNZCM.Shared.Helpers;
  4. using FNZCM.Shared.MediaModels;
  5. using FNZCM.Shared.MetadataModels;
  6. using Microsoft.AspNetCore.Components.WebAssembly.Http;
  7. using Newtonsoft.Json;
  8. using System.Net.Http.Json;
  9. namespace FNZCM.BlazorWasm.Helpers
  10. {
  11. public class ApiClient
  12. {
  13. private readonly HttpClient http;
  14. public Uri BaseAddress => http.BaseAddress;
  15. public ApiClient(HttpClient http) => this.http = http;
  16. public async Task<LoadingProgress> GetProgressAsync()
  17. {
  18. var url = $"metadata/progress.json";
  19. var response = await http.GetAsync(url);
  20. var obj = await response.Content.ReadFromJsonAsync<LoadingProgress>();
  21. return obj;
  22. }
  23. private async Task<string> GetStringAsync(string url)
  24. {
  25. var response = await http.GetAsync(url);
  26. var str = await response.Content.ReadAsStringAsync();
  27. return str;
  28. }
  29. public async Task<Dictionary<string, Library>> GetLibrariesAsync(ProgressBar[] progress)
  30. {
  31. var url = $"metadata/file-set.json";
  32. url += $"?etag={await GetStringAsync($"{url}/checksum")}";
  33. var request = new HttpRequestMessage(HttpMethod.Get, url);
  34. request.SetBrowserResponseStreamingEnabled(true);
  35. request.SetBrowserRequestCache(BrowserRequestCache.Default);
  36. var response = await http.SendAsync(request);
  37. var jsonStream = await response.Content.ReadAsStreamAsync();
  38. using var streamReader = new StreamReader(jsonStream);
  39. using var jsonReader = new JsonTextReader(streamReader);
  40. var serializer = new JsonSerializer();
  41. if (!jsonReader.Read()) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  42. if (jsonReader.TokenType != JsonToken.StartObject) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  43. var obj = new Dictionary<string, Library>();
  44. while (true)
  45. {
  46. await progress[0].SetProgress((float)jsonStream.Position / jsonStream.Length, $"Loading fileset {jsonStream.Position.BytesToFileSize()}/{jsonStream.Length.BytesToFileSize()}...");
  47. if (!jsonReader.Read()) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  48. if (jsonReader.TokenType == JsonToken.EndObject) break;
  49. if (jsonReader.TokenType != JsonToken.PropertyName) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  50. var key = (string)jsonReader.Value;
  51. if (!jsonReader.Read()) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  52. if (jsonReader.TokenType != JsonToken.StartObject) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  53. obj[key] = serializer.Deserialize<Library>(jsonReader);
  54. }
  55. await progress[0].SetProgress((float)jsonStream.Position / jsonStream.Length, $"Loading fileset {jsonStream.Position.BytesToFileSize()}/{jsonStream.Length.BytesToFileSize()}...OK", true);
  56. return obj;
  57. }
  58. public async Task<Dictionary<string, MediaTag>> GetMediaTagsAsync(ProgressBar[] progress = null)
  59. {
  60. var url = $"metadata/tag-dict.json";
  61. url += $"?etag={await GetStringAsync($"{url}/checksum")}";
  62. var request = new HttpRequestMessage(HttpMethod.Get, url);
  63. request.SetBrowserResponseStreamingEnabled(true);
  64. request.SetBrowserRequestCache(BrowserRequestCache.Default);
  65. var response = await http.SendAsync(request);
  66. var jsonStream = await response.Content.ReadAsStreamAsync();
  67. using var streamReader = new StreamReader(jsonStream);
  68. using var jsonReader = new JsonTextReader(streamReader);
  69. var serializer = new JsonSerializer();
  70. if (!jsonReader.Read()) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  71. if (jsonReader.TokenType != JsonToken.StartObject) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  72. var obj = new Dictionary<string, MediaTag>();
  73. while (true)
  74. {
  75. await progress[1].SetProgress((float)jsonStream.Position / jsonStream.Length, $"Loading tags {jsonStream.Position.BytesToFileSize()}/{jsonStream.Length.BytesToFileSize()}...");
  76. if (!jsonReader.Read()) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  77. if (jsonReader.TokenType == JsonToken.EndObject) break;
  78. if (jsonReader.TokenType != JsonToken.PropertyName) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  79. var key = (string)jsonReader.Value;
  80. if (!jsonReader.Read()) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  81. if (jsonReader.TokenType != JsonToken.StartObject) throw new JsonException($"Deserialization failed at line {jsonReader.LineNumber} position {jsonReader.LinePosition}.");
  82. obj[key] = serializer.Deserialize<MediaTag>(jsonReader);
  83. }
  84. await progress[1].SetProgress((float)jsonStream.Position / jsonStream.Length, $"Loading tags {jsonStream.Position.BytesToFileSize()}/{jsonStream.Length.BytesToFileSize()}...OK", true);
  85. return obj;
  86. }
  87. public async Task ReloadBackEnd(string password, bool? flag)
  88. {
  89. var url = flag switch
  90. {
  91. null => "admin/?action=ReloadModules&pass=" + password,
  92. true => "admin/?action=ReloadFully&pass=" + password,
  93. false => "admin/?action=Reload&pass=" + password,
  94. };
  95. var request = new HttpRequestMessage(HttpMethod.Get, url);
  96. request.SetBrowserRequestCache(BrowserRequestCache.Default);
  97. var response = await http.SendAsync(request);
  98. var text = await response.Content.ReadAsStringAsync();
  99. }
  100. }
  101. }