FetchData.razor 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. @page "/fetchdata/{Service}/{CreatorId}"
  2. @using Microsoft.AspNetCore.Components.Rendering;
  3. @using Sve.Blazor.InfiniteScroll.Components
  4. @using VeloeKemonoPartyApp.Data
  5. @using VeloeKemonoPartyApp.Services;
  6. @inject WeatherForecastService ForecastService
  7. @inject KemonoHttpClient HttpClient
  8. @inject IJSRuntime JSRuntime
  9. <div id="image-menu" onclick="@(()=>CloseOverlay())"
  10. style="
  11. opacity: 1;
  12. background-color:#ccc;
  13. position:fixed;
  14. width:100%;
  15. height:100%;
  16. top:0px;
  17. left:0px;
  18. justify-content:center;
  19. pointer-events:none;
  20. display:@OverlayVisibility;
  21. z-index:900;">
  22. <div style="pointer-events:all;">
  23. <div style="position:fixed; top:0px; left:0px;opacity:1; background-color:red; align-items:center; height:@(Application.Current.MainPage.Height - 80)px; text-align: center;">
  24. <div id="panzoom_block" style="position: absolute;top: 50%;transform: translateY(-50%);height:@(Application.Current.MainPage.Height - 80)px;width:@(Application.Current.MainPage.Width)px;">
  25. <Panzoom @ref="_panzoom">
  26. <img id="panzoom" @ref="@context.ElementReference" alt="image" src="@ImageContextMenuLink" style="max-height:@(Application.Current.MainPage.Height - 80)px;max-width:@(Application.Current.MainPage.Width)px; display:block; margin-top:auto; margin-bottom:auto" />
  27. </Panzoom>
  28. </div>
  29. </div>
  30. <div style="position:fixed; bottom:10px; left:10px;">
  31. <h3>@ImageContextMenuName</h3>
  32. <button id="reload-image-button" style="z-index:1000;" @onclick=@(() => JSRuntime.InvokeVoidAsync("SwitchPic",ImageContextMenuId,ImageContextMenuLink))>Reload image</button>
  33. </div>
  34. </div>
  35. </div>
  36. <p>@Message</p>
  37. <InfiniteScroll ObserverTargetId="observerTarget" ObservableTargetReached="(e) => GetPosts()" >
  38. <div>
  39. @foreach (var post in posts)
  40. {
  41. <div>
  42. <h2>@post.Title</h2>
  43. @{
  44. int i = 0;
  45. foreach (var images in post.Attachments)
  46. {
  47. /*
  48. string toImage = System.IO.Path.Combine(FileSystem.CacheDirectory, System.IO.Path.GetFileName(new Uri(images.Path).LocalPath));
  49. string toFolder = FileSystem.CacheDirectory;
  50. for (int tries = 0; tries < 5; tries++)
  51. {
  52. try
  53. {
  54. if (!System.IO.File.Exists(toImage))
  55. {
  56. Task.Run(() =>
  57. {
  58. using (var s = new HttpClient().GetStreamAsync(images.Link).GetAwaiter().GetResult())
  59. {
  60. if (!Directory.Exists(toFolder))
  61. Directory.CreateDirectory(toFolder);
  62. using (var fs = new FileStream(toImage, FileMode.CreateNew))
  63. {
  64. s.CopyToAsync(fs).GetAwaiter();
  65. }
  66. }
  67. }).GetAwaiter();
  68. }
  69. toImage = Convert.ToBase64String(System.IO.File.ReadAllBytes(toImage));
  70. //DependencyService.Get<KemonoHttpClient>().GetImageAsync($"https://kemono.party/data{images.Path}", toFolder, toImage).GetAwaiter();
  71. break;
  72. }
  73. catch (Exception ex)
  74. {
  75. if (tries == 4)
  76. toImage = "logo_vivelapub.png";
  77. //await Task.Delay(500);
  78. }
  79. }
  80. */
  81. if (System.IO.Path.GetExtension(images.Path) is (".png" or ".jpg" or ".jpeg" or ".gif")){
  82. //if (System.IO.Path.GetExtension(toImage) is (".png" or ".jpg" or ".jpeg" or ".gif")){
  83. string link = images.Link;
  84. string imageId = $"{images.Path}_{i}";
  85. <img
  86. id="@imageId"
  87. src="@link"
  88. onerror="@(
  89. ()=>{
  90. Task.Delay(1000).GetAwaiter();
  91. JSRuntime.InvokeVoidAsync("SwitchPic",imageId,link);
  92. }
  93. )"
  94. @onclick=@(()=>Task.Run(()=>OpenOverlay(imageId,link,images.Name))) style="margin-bottom:10px; width:@(Application.Current.MainPage.Width - 50)px; height:contain;" />
  95. }
  96. //else
  97. //{
  98. // string link = "data:image/png;base64," + toImage;
  99. //string imageId = $"{images.Path}_{i}";
  100. // <img id="@imageId" src="@link" @onclick=@(()=>OpenOverlay(imageId,link,images.Name)) style="margin-bottom:10px; width:@(Application.Current.MainPage.Width - 50)px; height:contain;" />
  101. //}
  102. }
  103. }
  104. @if(post.Content.Contains("<a>"))
  105. {
  106. post.Content.Replace("<a>","<a style:\"word-break:break-all;\">");
  107. }
  108. <div style="word-break:break-word;">@((MarkupString)post.Content)</div>
  109. @foreach (var attachment in post.Attachments)
  110. {
  111. string link = "https://kemono.party" + attachment.Path;
  112. <p onclick="@(async () => await OpenLink(link))" style="color:dodgerblue; word-break:break-all;" >@attachment.Name</p>
  113. }
  114. <p >@post.Published</p>
  115. </div>
  116. }
  117. <p class="list-group-item" id="observerTarget">End</p>
  118. </div>
  119. </InfiniteScroll>
  120. @code {
  121. [Parameter]
  122. public string CreatorId { get; set; }
  123. [Parameter]
  124. public string Service { get; set; }
  125. string OverlayVisibility { get; set; }
  126. string ImageContextMenuName { get; set; }
  127. string ImageContextMenuId { get; set; }
  128. string ImageContextMenuLink { get; set; }
  129. string Message { get; set; }
  130. private WeatherForecast[] forecasts;
  131. private List<Post> postsCache = new List<Post>();
  132. private List<Post> posts = new List<Post>();
  133. private int loadedPos = 0;
  134. private Panzoom _panzoom;
  135. protected override async Task OnInitializedAsync()
  136. {
  137. //await GetPosts();
  138. OverlayVisibility = "none";
  139. }
  140. private async Task GetPosts()
  141. { // Just to make it async
  142. int i = 0;
  143. do
  144. {
  145. try
  146. {
  147. if (postsCache.Count == posts.Count)
  148. {
  149. var postsNew = await HttpClient.GetPostsList(CreatorId, Service, loadedPos);
  150. postsCache.AddRange(postsNew);
  151. }
  152. posts.AddRange(postsCache.Skip(posts.Count).Take(1));
  153. //loadedPos += postsNew.Count;
  154. loadedPos += 1;
  155. return;
  156. }
  157. catch (Exception ex)
  158. {
  159. await Task.Delay(1000);
  160. }
  161. i++;
  162. }
  163. while (i<5);
  164. Message = "LoadFailed";
  165. }
  166. private async Task OpenLink(string link)
  167. {
  168. await Browser.Default.OpenAsync(link);
  169. }
  170. void CloseOverlay()
  171. {
  172. OverlayVisibility = "none";
  173. }
  174. async void OpenOverlay(string id, string link, string name)
  175. {
  176. JSRuntime.InvokeVoidAsync("SetOverlayImage", id);
  177. ImageContextMenuName = name;
  178. ImageContextMenuLink = link;
  179. ImageContextMenuId = id;
  180. OverlayVisibility = "block";
  181. await _panzoom.ResetAsync();
  182. await JSRuntime.InvokeVoidAsync("SetPanzoom");
  183. JSRuntime.InvokeVoidAsync("PanzoomCenter");
  184. ShouldRender();
  185. }
  186. }