Skip to content

Commit 36b4a9c

Browse files
committed
Refactoring
1 parent 903adf8 commit 36b4a9c

34 files changed

+794
-700
lines changed

SmartImage.Lib 3/Booru/BaseGelbooru.cs renamed to SmartImage.Lib 3/Clients/Booru/BaseGelbooruClient.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System.Reflection;
22
using Flurl.Http;
33

4-
namespace SmartImage.Lib.Booru;
4+
namespace SmartImage.Lib.Clients.Booru;
5+
56
// TODO
6-
public abstract class BaseGelbooru : IDisposable
7+
public abstract class BaseGelbooruClient : IDisposable
78
{
9+
810
public FlurlClient Client { get; }
911

1012
public Url Base { get; }
@@ -15,7 +17,7 @@ public abstract class BaseGelbooru : IDisposable
1517
[CBN]
1618
public string Id { get; set; }
1719

18-
protected BaseGelbooru(Url @base)
20+
protected BaseGelbooruClient(Url @base)
1921
{
2022
Base = @base;
2123

@@ -31,15 +33,21 @@ protected BaseGelbooru(Url @base)
3133

3234
public class PostsRequest
3335
{
34-
public int Limit { get; set; }
35-
public int Pid { get; set; }
36-
public string Tags { get; set; }
37-
public long Cid { get; set; }
38-
public int Id { get; set; }
36+
37+
public int Limit { get; set; }
38+
39+
public int Pid { get; set; }
40+
41+
public string Tags { get; set; }
42+
43+
public long Cid { get; set; }
44+
45+
public int Id { get; set; }
3946

4047
public int Json { get; set; } = 1;
4148

4249
public PostsRequest() { }
50+
4351
}
4452

4553
protected int PostMax { get; set; } = 100;
@@ -62,7 +70,7 @@ public virtual async Task<IFlurlResponse> GetPostsAsync(PostsRequest r)
6270
foreach (PropertyInfo p in r.GetType().GetProperties()) {
6371
var o = p.GetValue(r);
6472

65-
if (o == null || (o is string s && string.IsNullOrWhiteSpace(s)) || o.Equals(0)) {
73+
if (o == null || o is string s && string.IsNullOrWhiteSpace(s) || o.Equals(0)) {
6674
continue;
6775
}
6876

@@ -81,12 +89,15 @@ public void Dispose()
8189
{
8290
Client?.Dispose();
8391
}
92+
8493
}
8594

86-
public class Rule34Booru : BaseGelbooru
95+
public class Rule34Booru : BaseGelbooruClient
8796
{
97+
8898
public Rule34Booru() : base("https://rule34.xxx")
8999
{
90100
PostMax = 1000;
91101
}
102+
92103
}

SmartImage.Lib 3/Engines/BaseSearchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public virtual bool VerifyQuery(SearchQuery q)
9696
bool b = true;
9797

9898
if (MaxSize.HasValue) {
99-
b = q.Size <= MaxSize;
99+
b = q.Image.Size <= MaxSize;
100100
}
101101

102102
/*if (MaxSize == NA_SIZE || q.Size == NA_SIZE) {

SmartImage.Lib 3/Engines/Impl/Search/ArchiveMoeEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public override void Dispose()
3434
protected static string GetHash(SearchQuery q)
3535
{
3636
//var digestBase64URL = digestBase64.replace('==', '').replace(/\//g, '_').replace(/\+/g, '-');
37-
var data = MD5.HashData(q.Stream);
37+
var data = MD5.HashData(q.Image.Stream);
3838
var b64 = Convert.ToBase64String(data).Replace("==", "");
3939
b64 = Regex.Replace(b64, @"\//", "_");
4040
b64 = Regex.Replace(b64, @"\+", "-");
4141

42-
q.Stream.TrySeek();
42+
q.Image.Stream.TrySeek();
4343

4444
return b64;
4545
}

SmartImage.Lib 3/Engines/Impl/Search/EHentaiEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
161161
string fileName;
162162
string filePath = null;
163163

164-
if (query.HasFile) {
165-
filePath = query.FilePath;
164+
if (query.Image.HasFile) {
165+
filePath = query.Image.FilePath;
166166
fileName = Path.GetFileName(filePath);
167167

168168
/*if (Path.GetFileName(t) != name) {
@@ -171,10 +171,10 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
171171
}
172172
else {
173173
fileName = SFILE_NAME_DEFAULT;
174-
var ok = query.TryGetFile(fileName);
174+
var ok = query.Image.TryGetFile(fileName);
175175

176176
if (ok) {
177-
filePath = query.FilePath;
177+
filePath = query.Image.FilePath;
178178
}
179179
else {
180180
Debugger.Break();

SmartImage.Lib 3/Engines/Impl/Search/FluffleEngine.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
5656
.PostMultipartAsync(c =>
5757
{
5858
// var tmp = query.WriteToFile();
59-
query.Stream.TrySeek();
59+
query.Image.Stream.TrySeek();
6060

61-
c.AddFile("file", query.Stream, "file");
62-
query.Stream.TrySeek();
61+
c.AddFile("file", query.Image.Stream, "file");
62+
query.Image.Stream.TrySeek();
6363

6464
c.AddString("includeNsfw", true.ToString());
6565
c.AddString("limit", 32.ToString());

SmartImage.Lib 3/Engines/Impl/Search/IqdbEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ private async Task<IDocument> GetDocumentAsync(SearchQuery query, CancellationTo
146146
.PostMultipartAsync(m =>
147147
{
148148
m.AddString("MAX_FILE_SIZE", MAX_FILE_SIZE.ToString());
149-
m.AddString("url", query.IsUri ? query.Value.ToString() : String.Empty);
149+
m.AddString("url", query.Image.IsUri ? query.Image.ValueString : String.Empty);
150150

151-
if (query.IsUri) { }
152-
else if (query.IsFile) {
153-
m.AddFile("file", query.Value.ToString(), fileName: "image.jpg");
151+
if (query.Image.IsUri) { }
152+
else if (query.Image.IsFile) {
153+
m.AddFile("file", query.Image.Value.ToString(), fileName: "image.jpg");
154154
}
155155

156156
return;

SmartImage.Lib 3/Engines/Impl/Search/SauceNaoEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ private async Task<IEnumerable<SauceNaoDataResult>> GetWebResultsAsync(SearchQue
168168
.WithTimeout(Timeout)
169169
.PostMultipartAsync(m =>
170170
{
171-
m.AddString("url", query.IsUri ? query.Value.ToString() : string.Empty);
171+
m.AddString("url", query.Image.IsUri ? query.Image.ValueString : string.Empty);
172172

173-
if (query.IsUri) { }
174-
else if (query.IsFile) {
175-
m.AddFile("file", query.Value.ToString(), fileName: "image.png");
173+
if (query.Image.IsUri) { }
174+
else if (query.Image.IsFile) {
175+
m.AddFile("file", query.Image.Value.ToString(), fileName: "image.png");
176176
}
177177

178178
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Author: Deci | Project: SmartImage.Lib | Name: GenericImageFilter.cs
2+
// Date: 2024/05/02 @ 11:05:10
3+
4+
using System.Diagnostics;
5+
6+
namespace SmartImage.Lib.Model;
7+
8+
public class GenericImageFilter : IImageFilter
9+
{
10+
11+
public string[] Blacklist
12+
=>
13+
[
14+
"thumbnail", "avatar", "error", "logo"
15+
];
16+
17+
public bool Predicate(UniImage us)
18+
{
19+
try {
20+
if (us.Stream.Length <= 25_000 || us.Info.DefaultMimeType == null) {
21+
return false;
22+
}
23+
24+
return true;
25+
}
26+
catch (Exception e) {
27+
Debug.WriteLine($"{e.Message}", nameof(Predicate));
28+
return true;
29+
}
30+
}
31+
32+
public bool Refine(string b)
33+
{
34+
if (!Url.IsValid(b)) {
35+
return false;
36+
}
37+
38+
var u = Url.Parse(b);
39+
var ps = u.PathSegments;
40+
41+
if (ps.Any()) {
42+
return !Blacklist.Any(i => ps.Any(p => p.Contains(i, StringComparison.InvariantCultureIgnoreCase)));
43+
}
44+
45+
return true;
46+
}
47+
48+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Author: Deci | Project: SmartImage.Lib | Name: IImageFilter.cs
2+
// Date: 2024/05/02 @ 11:05:28
3+
4+
namespace SmartImage.Lib.Model;
5+
6+
public interface IImageFilter
7+
{
8+
9+
public string[] Blacklist { get; }
10+
11+
public bool Refine(string b);
12+
13+
public bool Predicate(UniImage us);
14+
15+
}

SmartImage.Lib 3/Results/SearchResultItem.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Kantan.Net.Utilities;
77
using Novus.FileTypes;
88
using Novus.FileTypes.Uni;
9-
using SmartImage.Lib.Model;
9+
using SmartImage.Lib.Utilities;
1010

1111
namespace SmartImage.Lib.Results;
1212

@@ -101,7 +101,9 @@ public sealed record SearchResultItem : IDisposable, IComparable<SearchResultIte
101101
[CBN]
102102
public string ThumbnailTitle { get; internal set; }
103103

104-
public UniSource[] Uni { get; internal set; }
104+
public UniImage[] Uni { get; internal set; }
105+
106+
public bool HasUni => Uni != null && Uni.Any();
105107

106108
[CBN]
107109
public SearchResultItem Parent { get; internal set; }
@@ -231,7 +233,7 @@ public SearchResultItem[] AddChildren(string[] rg)
231233
}
232234

233235
// [MustUseReturnValue]
234-
public async Task<bool> LoadUniAsync(CancellationToken ct = default)
236+
public async Task<bool> ScanAsync(CancellationToken ct = default)
235237
{
236238
if (HasUni) {
237239
return true;
@@ -242,12 +244,10 @@ public async Task<bool> LoadUniAsync(CancellationToken ct = default)
242244
}
243245

244246
// Uni = await UniSource.TryGetAsync(Url, ct: ct, whitelist: FileType.Image);
245-
Uni = await BaseImageHost.ScanAsync(Url, ct: ct);
247+
Uni = await ImageScanner.ScanAsync(Url, ct: ct);
246248
return HasUni;
247249
}
248250

249-
public bool HasUni => Uni != null && Uni.Any();
250-
251251
// public IFlurlResponse Response { get; private set; }
252252

253253
public override string ToString()

0 commit comments

Comments
 (0)