Skip to content

Commit b5d6b2b

Browse files
committed
Update Flurl to 4.0.0; work on Rdx version refactor
1 parent 9c6b7a4 commit b5d6b2b

24 files changed

+697
-520
lines changed

SmartImage.Lib 3/Engines/BaseSearchEngine.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
using Novus.Utilities;
77
using SmartImage.Lib.Results;
88
using AngleSharp.Dom;
9+
using Flurl.Http;
10+
using Microsoft.Extensions.Http.Logging;
11+
using Microsoft.Extensions.Logging;
12+
using SmartImage.Lib.Utilities;
913

1014
namespace SmartImage.Lib.Engines;
1115
#nullable enable
@@ -41,7 +45,34 @@ protected BaseSearchEngine(string baseUrl)
4145
IsAdvanced = true;
4246
}
4347

44-
static BaseSearchEngine() { }
48+
protected static readonly ILogger Logger = LogUtil.Factory.CreateLogger(nameof(BaseSearchEngine));
49+
50+
protected static FlurlClient Client { get; }
51+
52+
static BaseSearchEngine()
53+
{
54+
var handler = new LoggingHttpMessageHandler(Logger)
55+
{
56+
InnerHandler = new HttpLoggingHandler(Logger)
57+
{
58+
InnerHandler = new HttpClientHandler()
59+
}
60+
};
61+
62+
BaseSearchEngine.Client = new FlurlClient(new HttpClient(handler))
63+
{
64+
Settings =
65+
{
66+
Redirects =
67+
{
68+
Enabled = true,
69+
AllowSecureToInsecure = true,
70+
ForwardAuthorizationHeader = true,
71+
MaxAutoRedirects = 20,
72+
},
73+
}
74+
};
75+
}
4576

4677
public override string ToString()
4778
{

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ protected override bool VerifyQuery(SearchQuery q)
3737
{
3838
var b = base.VerifyQuery(q);
3939
bool b2;
40+
bool ok = q.HasImage;
4041

41-
if (q.HasImage) {
42+
if (!ok) {
43+
ok = q.LoadImage();
44+
45+
}
46+
if (ok) {
4247
b2 = q.Image.Width < MAX_WIDTH;
4348
}
4449
else {
@@ -106,7 +111,7 @@ private async Task<IFlurlResponse> GetResponseByUrlAsync(Url origin, Cancellatio
106111
{ new StringContent(origin), "uri" }
107112
};
108113

109-
var res = await SearchClient.Client.Request(origin).AllowAnyHttpStatus()
114+
var res = await Client.Request(origin).AllowAnyHttpStatus()
110115
.WithCookies(out var cj)
111116
.WithTimeout(Timeout)
112117
.WithHeaders(new

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,25 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
197197
CancellationToken token = default)
198198
{
199199
const string name = "a.jpg";
200+
string t = null;
201+
if (query.HasFile) {
202+
t = query.FilePath;
200203

201-
(string t, bool b) = await query.GetFilePathOrTempAsync(name);
204+
if (Path.GetFileName(t) != name) {
205+
Debugger.Break();
206+
}
207+
}
208+
else {
209+
var ok = query.LoadFile(name);
210+
if (ok) {
211+
t = query.FilePath;
212+
}
213+
else {
214+
Debugger.Break();
215+
}
216+
}
202217

203-
if (b) {
218+
if (t != null) {
204219
Trace.WriteLine($"allocated {t}", nameof(GetDocumentAsync));
205220
}
206221

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace SmartImage.Lib.Engines.Impl.Search;
2828

2929
public class IqdbEngine : BaseSearchEngine, IEndpoint
3030
{
31+
3132
public virtual string EndpointUrl => "https://iqdb.org/";
3233

3334
public override SearchEngineOptions EngineOption => SearchEngineOptions.Iqdb;
@@ -133,7 +134,13 @@ private async Task<IDocument> GetDocumentAsync(SearchQuery query, CancellationTo
133134
{
134135

135136
try {
136-
var response = await EndpointUrl.WithSettings(NetHelper.Configure())
137+
var response = await Client.Request(EndpointUrl)
138+
.OnError(r =>
139+
{
140+
Debug.WriteLine($"{r.Exception}");
141+
r.ExceptionHandled = true;
142+
}
143+
)
137144
.WithTimeout(Timeout)
138145
.PostMultipartAsync(m =>
139146
{
@@ -245,4 +252,5 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
245252
public override void Dispose() { }
246253

247254
#endregion
255+
248256
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
4949
Root obj = null;
5050

5151
try {
52-
var s = await SearchClient.Client.Request(EndpointUrl).SetQueryParams(new
52+
var s = await Client.Request(EndpointUrl).SetQueryParams(new
5353
{
5454

5555
filter = true,

SmartImage.Lib 3/Engines/Impl/Upload/BaseUploadEngine.cs

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Flurl.Http.Configuration;
88
using Flurl.Http.Content;
99
using Kantan.Net.Utilities;
10+
using Microsoft.Extensions.Http.Logging;
11+
using Microsoft.Extensions.Logging;
1012
using Novus.OS;
1113
using Novus.Utilities;
1214
using SmartImage.Lib.Results;
@@ -16,6 +18,7 @@ namespace SmartImage.Lib.Engines.Impl.Upload;
1618

1719
public abstract class BaseUploadEngine : IEndpoint
1820
{
21+
1922
/// <summary>
2023
/// Max file size, in bytes
2124
/// </summary>
@@ -39,6 +42,35 @@ protected BaseUploadEngine(string s)
3942

4043
public TimeSpan Timeout { get; set; }
4144

45+
protected static readonly ILogger Logger = LogUtil.Factory.CreateLogger(nameof(BaseUploadEngine));
46+
47+
protected static FlurlClient Client { get; }
48+
49+
static BaseUploadEngine()
50+
{
51+
var handler = new LoggingHttpMessageHandler(Logger)
52+
{
53+
InnerHandler = new HttpLoggingHandler(Logger)
54+
{
55+
InnerHandler = new HttpClientHandler()
56+
}
57+
};
58+
59+
Client = new FlurlClient(new HttpClient(handler))
60+
{
61+
Settings =
62+
{
63+
Redirects =
64+
{
65+
Enabled = true,
66+
AllowSecureToInsecure = true,
67+
ForwardAuthorizationHeader = true,
68+
MaxAutoRedirects = 20,
69+
},
70+
}
71+
};
72+
}
73+
4274
protected virtual async Task<UploadResult> ProcessResultAsync(
4375
IFlurlResponse response, CancellationToken ct = default)
4476
{
@@ -66,14 +98,15 @@ protected virtual async Task<UploadResult> ProcessResultAsync(
6698
ok = true;
6799

68100
if (Paranoid) {
69-
var r2 = await url.WithSettings(r =>
70-
{
71-
r.OnError = rx =>
72-
{
73-
// Debugger.Break();
74-
rx.ExceptionHandled = true;
75-
};
76-
}).GetAsync(cancellationToken: ct);
101+
var r2 = await Client.Request(url)
102+
.WithSettings(r =>
103+
{
104+
r.Timeout = Timeout;
105+
}).OnError(rx =>
106+
{
107+
// Debugger.Break();
108+
rx.ExceptionHandled = true;
109+
}).GetAsync(cancellationToken: ct);
77110

78111
if (r2 == null || r2.GetContentLength() == 0) {
79112
ok = false;
@@ -85,9 +118,9 @@ protected virtual async Task<UploadResult> ProcessResultAsync(
85118

86119
return new()
87120
{
88-
Url = url,
89-
Size = response.GetContentLength(),
90-
IsValid = ok,
121+
Url = url,
122+
Size = response.GetContentLength(),
123+
IsValid = ok,
91124
Response = response
92125
};
93126
}
@@ -106,33 +139,29 @@ protected void Verify(string file)
106139
public static readonly BaseUploadEngine[] All =
107140
ReflectionHelper.CreateAllInAssembly<BaseUploadEngine>(InheritanceProperties.Subclass).ToArray();
108141

109-
/*public async Task<bool> IsAlive()
110-
{
111-
using var res = await ((IHttpClient) this).GetEndpointResponseAsync(Timeout);
112-
113-
return !res.ResponseMessage.IsSuccessStatusCode;
114-
}*/
142+
/*public async Task<bool> IsAlive()
143+
{
144+
using var res = await ((IHttpClient) this).GetEndpointResponseAsync(Timeout);
115145
146+
return !res.ResponseMessage.IsSuccessStatusCode;
147+
}*/
116148
public static BaseUploadEngine Default { get; set; } = PomfEngine.Instance;
117149

118150
public void Dispose() { }
151+
119152
}
120153

121154
public abstract class BaseCatboxEngine : BaseUploadEngine
122155
{
156+
123157
public override async Task<UploadResult> UploadFileAsync(string file, CancellationToken ct = default)
124158
{
125159
Verify(file);
126160

127-
var response = await EndpointUrl
161+
var response = await Client.Request(EndpointUrl)
128162
.WithSettings(r =>
129163
{
130-
// r.Timeout = TimeSpan.FromSeconds(10);
131-
132-
r.OnError = rx =>
133-
{
134-
rx.ExceptionHandled = true;
135-
};
164+
r.Timeout = Timeout;
136165
})
137166
.WithHeaders(new
138167
{
@@ -150,4 +179,5 @@ public override async Task<UploadResult> UploadFileAsync(string file, Cancellati
150179
}
151180

152181
protected BaseCatboxEngine(string s) : base(s) { }
182+
153183
}

SmartImage.Lib 3/Engines/Impl/Upload/PomfEngine.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace SmartImage.Lib.Engines.Impl.Upload;
1111

1212
public sealed class PomfEngine : BaseUploadEngine
1313
{
14+
1415
public PomfEngine() : base("https://pomf.lain.la/upload.php") { }
1516

1617
public override long MaxSize => 1_000_000_000;
@@ -23,15 +24,13 @@ public override async Task<UploadResult> UploadFileAsync(string file, Cancellati
2324
{
2425
Verify(file);
2526

26-
var response = await EndpointUrl
27+
var response = await Client.Request(EndpointUrl)
2728
.WithSettings(r =>
2829
{
29-
// r.Timeout = TimeSpan.FromSeconds(10);
30-
31-
r.OnError = rx =>
32-
{
33-
rx.ExceptionHandled = true;
34-
};
30+
r.Timeout = TimeSpan.FromSeconds(10);
31+
}).OnError(r =>
32+
{
33+
r.ExceptionHandled = true;
3534
})
3635
.PostMultipartAsync(mp =>
3736
{
@@ -43,30 +42,35 @@ public override async Task<UploadResult> UploadFileAsync(string file, Cancellati
4342
var bur = new UploadResult()
4443
{
4544
Value = pr,
46-
Size = pr.Files[0].Size,
45+
Size = pr.Files[0].Size,
4746
Url = pr.Files[0].Url,
48-
IsValid = pr.Success,
47+
IsValid = pr.Success,
4948
Response = response
5049
};
5150

5251
return bur;
5352
}
53+
5454
}
5555

5656
public sealed class PomfResult
5757
{
58+
5859
public bool Success { get; set; }
5960

6061
public PomfFileResult[] Files { get; set; }
62+
6163
}
6264

6365
public sealed class PomfFileResult
6466
{
67+
6568
public string Hash { get; set; }
6669

6770
public string Name { get; set; }
6871

6972
public string Url { get; set; }
7073

7174
public long Size { get; set; }
75+
7276
}

SmartImage.Lib 3/Engines/WebSearchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected virtual async Task<IDocument> GetDocumentAsync(SearchResult sr, Search
6969

7070
try {
7171

72-
var res = await SearchClient.Client.Request(sr.RawUrl)
72+
var res = await Client.Request(sr.RawUrl)
7373
.AllowAnyHttpStatus()
7474
.WithCookies(out var cj)
7575
.WithTimeout(Timeout)

SmartImage.Lib 3/SearchClient.cs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,38 +51,9 @@ public SearchClient(SearchConfig cfg)
5151

5252
static SearchClient()
5353
{
54-
var handler = new LoggingHttpMessageHandler(Logger)
55-
{
56-
InnerHandler = new HttpLoggingHandler(Logger)
57-
{
58-
InnerHandler = new HttpClientHandler()
59-
}
60-
};
61-
62-
Client = new FlurlClient(new HttpClient(handler), settings: Settings)
63-
{
64-
65-
};
54+
6655
}
6756

68-
public static FlurlClient Client { get; }
69-
70-
public static readonly FlurlHttpSettings Settings = new FlurlHttpSettings()
71-
{
72-
Redirects =
73-
{
74-
Enabled = true,
75-
AllowSecureToInsecure = true,
76-
ForwardAuthorizationHeader = true,
77-
MaxAutoRedirects = 20
78-
},
79-
OnError = r =>
80-
{
81-
Debug.WriteLine($"exception: {r.Exception}");
82-
r.ExceptionHandled = false;
83-
}
84-
};
85-
8657
[ModuleInitializer]
8758
public static void Init()
8859
{

0 commit comments

Comments
 (0)