|
@@ -2,6 +2,7 @@
|
|
using System.IO;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net;
|
|
|
|
+using System.Net.Mail;
|
|
using System.Text.Json;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
@@ -126,13 +127,78 @@ namespace VeloeAvaloniaKemonoPartyApp.Models
|
|
[JsonPropertyName("path")]
|
|
[JsonPropertyName("path")]
|
|
public string Path { get; set; }
|
|
public string Path { get; set; }
|
|
|
|
|
|
- public string Link
|
|
|
|
|
|
+ public string Link => $"https://kemono.su/data{Path}";
|
|
|
|
+
|
|
|
|
+ private static KemonoHttpClient s_httpClient = new();
|
|
|
|
+ private string CachePath => $"./Cache/{Path.Split('/').Last()}";
|
|
|
|
+
|
|
|
|
+ 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(Link);
|
|
|
|
+ return new MemoryStream(data);
|
|
|
|
+ }
|
|
|
|
+ catch (System.Net.Http.HttpRequestException ex)
|
|
|
|
+ {
|
|
|
|
+ return new MemoryStream();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task SaveAsync()
|
|
{
|
|
{
|
|
- get
|
|
|
|
|
|
+ if (!Directory.Exists("./Cache"))
|
|
{
|
|
{
|
|
- return $"https://kemono.su/data{Path}";
|
|
|
|
|
|
+ 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(Attachment data, Stream stream)
|
|
|
|
+ {
|
|
|
|
+ await JsonSerializer.SerializeAsync(stream, data).ConfigureAwait(false);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static async Task<Attachment> LoadFromStream(Stream stream)
|
|
|
|
+ {
|
|
|
|
+ return (await JsonSerializer.DeserializeAsync<Attachment>(stream).ConfigureAwait(false))!;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static async Task<IEnumerable<Attachment>> LoadCachedAsync()
|
|
|
|
+ {
|
|
|
|
+ if (!Directory.Exists("./Cache"))
|
|
|
|
+ {
|
|
|
|
+ Directory.CreateDirectory("./Cache");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var results = new List<Attachment>();
|
|
|
|
+
|
|
|
|
+ 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 Attachment.LoadFromStream(fs).ConfigureAwait(false));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return results;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
public class Embed
|
|
public class Embed
|