FetchData.razor 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 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 @ref="@context.ElementReference" src="@ImageContextMenuLink" style="top: 50%;transform: translateY(-50%);max-height:@(Application.Current.MainPage.Height - 80)px;max-width:@(Application.Current.MainPage.Width)px;" />
  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 id="@imageId" src="@link" onerror="@(
  86. ()=>{
  87. Task.Delay(1000).GetAwaiter();
  88. JSRuntime.InvokeVoidAsync("SwitchPic",imageId,link);
  89. }
  90. )"
  91. @onclick=@(()=>OpenOverlay(imageId,link,images.Name)) style="margin-bottom:10px; width:@(Application.Current.MainPage.Width - 50)px; height:contain;" />
  92. }
  93. //else
  94. //{
  95. // string link = "data:image/png;base64," + toImage;
  96. //string imageId = $"{images.Path}_{i}";
  97. // <img id="@imageId" src="@link" @onclick=@(()=>OpenOverlay(imageId,link,images.Name)) style="margin-bottom:10px; width:@(Application.Current.MainPage.Width - 50)px; height:contain;" />
  98. //}
  99. }
  100. }
  101. @if(post.Content.Contains("<a>"))
  102. {
  103. post.Content.Replace("<a>","<a style:\"word-break:break-all;\">");
  104. }
  105. <div style="word-break:break-word;">@((MarkupString)post.Content)</div>
  106. @foreach (var attachment in post.Attachments)
  107. {
  108. string link = "https://kemono.party" + attachment.Path;
  109. <p onclick="@(async () => await OpenLink(link))" style="color:dodgerblue; word-break:break-all;" >@attachment.Name</p>
  110. }
  111. <p >@post.Published</p>
  112. </div>
  113. }
  114. <p class="list-group-item" id="observerTarget">End</p>
  115. </div>
  116. </InfiniteScroll>
  117. @code {
  118. [Parameter]
  119. public string CreatorId { get; set; }
  120. [Parameter]
  121. public string Service { get; set; }
  122. string OverlayVisibility { get; set; }
  123. string ImageContextMenuName { get; set; }
  124. string ImageContextMenuId { get; set; }
  125. string ImageContextMenuLink { get; set; }
  126. string Message { get; set; }
  127. private WeatherForecast[] forecasts;
  128. private List<Post> postsCache = new List<Post>();
  129. private List<Post> posts = new List<Post>();
  130. private int loadedPos = 0;
  131. private Panzoom _panzoom;
  132. protected override async Task OnInitializedAsync()
  133. {
  134. //await GetPosts();
  135. OverlayVisibility = "none";
  136. }
  137. private async Task GetPosts()
  138. { // Just to make it async
  139. int i = 0;
  140. do
  141. {
  142. try
  143. {
  144. if (postsCache.Count == posts.Count)
  145. {
  146. var postsNew = await HttpClient.GetPostsList(CreatorId, Service, loadedPos);
  147. postsCache.AddRange(postsNew);
  148. }
  149. posts.AddRange(postsCache.Skip(posts.Count).Take(1));
  150. //loadedPos += postsNew.Count;
  151. loadedPos += 1;
  152. return;
  153. }
  154. catch (Exception ex)
  155. {
  156. await Task.Delay(1000);
  157. }
  158. i++;
  159. }
  160. while (i<5);
  161. Message = "LoadFailed";
  162. }
  163. private async Task OpenLink(string link)
  164. {
  165. await Browser.Default.OpenAsync(link);
  166. }
  167. void CloseOverlay()
  168. {
  169. OverlayVisibility = "none";
  170. }
  171. void OpenOverlay(string id, string link, string name)
  172. {
  173. //JSRuntime.InvokeVoidAsync("ResetZoom");
  174. _panzoom.ResetAsync();
  175. ImageContextMenuName = name;
  176. ImageContextMenuLink = link;
  177. ImageContextMenuId = id;
  178. OverlayVisibility = "block";
  179. ShouldRender();
  180. }
  181. }