|
@@ -1,11 +1,10 @@
|
|
|
-using System.Collections.Generic;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
-using System.Net;
|
|
|
-using System.Net.Mail;
|
|
|
-using System.Text.Json;
|
|
|
using System.Text.Json.Serialization;
|
|
|
using System.Threading.Tasks;
|
|
|
+using VeloeAvaloniaKemonoPartyApp.Services;
|
|
|
using VeloeKemonoPartyApp.Services;
|
|
|
|
|
|
|
|
@@ -70,54 +69,6 @@ namespace VeloeAvaloniaKemonoPartyApp.Models
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- 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
|
|
@@ -130,75 +81,53 @@ namespace VeloeAvaloniaKemonoPartyApp.Models
|
|
|
public string Link => $"https://kemono.su/data{Path}";
|
|
|
|
|
|
private static KemonoHttpClient s_httpClient = new();
|
|
|
- private string CachePath => $"./Cache/{Path.Split('/').Last()}";
|
|
|
+
|
|
|
+ private static StorageService s_storage = new();
|
|
|
+
|
|
|
+ private string CachePath => $"./Cache/{FileName}";
|
|
|
+
|
|
|
+ private string FileName => Path.Split('/').Last();
|
|
|
|
|
|
public async Task<Stream> LoadCoverBitmapAsync()
|
|
|
{
|
|
|
- if (System.IO.File.Exists(CachePath + ".bmp"))
|
|
|
+ if (System.IO.File.Exists(System.IO.Path.Combine(await s_storage.GetDocumentsFolderAsync(), FileName + ".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)
|
|
|
+ var stream = System.IO.File.OpenRead(System.IO.Path.Combine(await s_storage.GetDocumentsFolderAsync(), FileName + ".bmp"));
|
|
|
+
|
|
|
+ byte[] hash = System.Security.Cryptography.SHA256.Create().ComputeHash(stream);
|
|
|
+
|
|
|
+ var hashstr = BitConverter.ToString(hash).Replace("-", String.Empty).ToLower();
|
|
|
+ if (hashstr == FileName.Split('.').First())
|
|
|
{
|
|
|
- return new MemoryStream();
|
|
|
+ stream.Position = 0;
|
|
|
+ return stream;
|
|
|
}
|
|
|
+ stream.Close();
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- public async Task SaveAsync()
|
|
|
- {
|
|
|
- if (!Directory.Exists("./Cache"))
|
|
|
- {
|
|
|
- Directory.CreateDirectory("./Cache");
|
|
|
- }
|
|
|
-
|
|
|
- using (var fs = System.IO.File.OpenWrite(CachePath))
|
|
|
+
|
|
|
+ try
|
|
|
{
|
|
|
- await SaveToStreamAsync(this, fs);
|
|
|
- }
|
|
|
- }
|
|
|
+ var data = await s_httpClient.httpClient.GetByteArrayAsync(Link);
|
|
|
|
|
|
- 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))!;
|
|
|
- }
|
|
|
+ using (var stream = System.IO.File.OpenWrite(System.IO.Path.Combine(await s_storage.GetDocumentsFolderAsync(), FileName + ".bmp")))
|
|
|
+ {
|
|
|
+ await stream.WriteAsync(data);
|
|
|
+ }
|
|
|
|
|
|
- public static async Task<IEnumerable<Attachment>> LoadCachedAsync()
|
|
|
- {
|
|
|
- if (!Directory.Exists("./Cache"))
|
|
|
+ return new MemoryStream(data);
|
|
|
+ }
|
|
|
+ catch (System.Net.Http.HttpRequestException ex)
|
|
|
{
|
|
|
- Directory.CreateDirectory("./Cache");
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
- var results = new List<Attachment>();
|
|
|
-
|
|
|
- foreach (var file in Directory.EnumerateFiles("./Cache"))
|
|
|
+ catch (OperationCanceledException ex)
|
|
|
{
|
|
|
- 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));
|
|
|
+ //System.IO.File.Delete(CachePath + ".bmp");
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
- return results;
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public class Embed
|