|
@@ -1,5 +1,11 @@
|
|
|
using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Net;
|
|
|
+using System.Text.Json;
|
|
|
using System.Text.Json.Serialization;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using VeloeKemonoPartyApp.Services;
|
|
|
|
|
|
|
|
|
namespace VeloeAvaloniaKemonoPartyApp.Models
|
|
@@ -30,6 +36,87 @@ namespace VeloeAvaloniaKemonoPartyApp.Models
|
|
|
public string Title { get; set; }
|
|
|
[JsonPropertyName("user")]
|
|
|
public string User { get; set; }
|
|
|
+
|
|
|
+ public static async Task<IEnumerable<Post>> InitPostsAsync(Creator creator)
|
|
|
+ {
|
|
|
+ return await s_httpClient.GetPostsList(creator.id,creator.service,0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static async Task<IEnumerable<Post>> LoadPostsAsync(Creator creator, int start)
|
|
|
+ {
|
|
|
+ return await s_httpClient.GetPostsList(creator.id, creator.service, start);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static KemonoHttpClient s_httpClient = new();
|
|
|
+ private string CachePath => $"./Cache/{User} - {Published}";
|
|
|
+
|
|
|
+ public async Task<Stream> LoadCoverBitmapAsync()
|
|
|
+ {
|
|
|
+ if (System.IO.File.Exists(CachePath + ".bmp"))
|
|
|
+ {
|
|
|
+ return System.IO.File.OpenRead(CachePath + ".bmp");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var data = await s_httpClient.httpClient.GetByteArrayAsync(Attachments.FirstOrDefault()?.Link);
|
|
|
+ return new MemoryStream(data);
|
|
|
+ }
|
|
|
+ catch (System.Net.Http.HttpRequestException ex)
|
|
|
+ {
|
|
|
+ return new MemoryStream();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task SaveAsync()
|
|
|
+ {
|
|
|
+ if (!Directory.Exists("./Cache"))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory("./Cache");
|
|
|
+ }
|
|
|
+
|
|
|
+ using (var fs = System.IO.File.OpenWrite(CachePath))
|
|
|
+ {
|
|
|
+ await SaveToStreamAsync(this, fs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public Stream SaveCoverBitmapStream()
|
|
|
+ {
|
|
|
+ return System.IO.File.OpenWrite(CachePath + ".bmp");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static async Task SaveToStreamAsync(Post data, Stream stream)
|
|
|
+ {
|
|
|
+ await JsonSerializer.SerializeAsync(stream, data).ConfigureAwait(false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static async Task<Post> LoadFromStream(Stream stream)
|
|
|
+ {
|
|
|
+ return (await JsonSerializer.DeserializeAsync<Post>(stream).ConfigureAwait(false))!;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static async Task<IEnumerable<Post>> LoadCachedAsync()
|
|
|
+ {
|
|
|
+ if (!Directory.Exists("./Cache"))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory("./Cache");
|
|
|
+ }
|
|
|
+
|
|
|
+ var results = new List<Post>();
|
|
|
+
|
|
|
+ foreach (var file in Directory.EnumerateFiles("./Cache"))
|
|
|
+ {
|
|
|
+ if (!string.IsNullOrWhiteSpace(new DirectoryInfo(file).Extension)) continue;
|
|
|
+
|
|
|
+ await using var fs = System.IO.File.OpenRead(file);
|
|
|
+ results.Add(await Post.LoadFromStream(fs).ConfigureAwait(false));
|
|
|
+ }
|
|
|
+
|
|
|
+ return results;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public class Attachment
|