Skip to content

Commit db93662

Browse files
committed
Fix obscure error
1 parent 9df45f1 commit db93662

22 files changed

+320
-214
lines changed

SmartImage.Lib 3/Engines/BaseSearchEngine.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using SmartImage.Lib.Results;
99
using AngleSharp.Dom;
1010
using Flurl.Http;
11+
using Kantan.Diagnostics;
1112
using Microsoft.Extensions.Http.Logging;
1213
using Microsoft.Extensions.Logging;
1314
using SmartImage.Lib.Utilities;
@@ -34,11 +35,13 @@ public abstract class BaseSearchEngine : IDisposable
3435

3536
public bool IsAdvanced { get; protected set; }
3637

37-
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(3);
38+
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(20);
39+
40+
public string? EndpointUrl { get; }
3841

3942
public TimeSpan Duration { get; protected set; }
4043

41-
protected long MaxSize { get; set; } = NA_SIZE;
44+
protected long? MaxSize { get; set; }
4245

4346
protected virtual string[] ErrorBodyMessages { get; } = [];
4447

@@ -47,6 +50,7 @@ protected BaseSearchEngine(string baseUrl, string? endpoint = null)
4750
BaseUrl = baseUrl;
4851
IsAdvanced = true;
4952
EndpointUrl = endpoint;
53+
MaxSize = null;
5054
}
5155

5256
protected static readonly ILogger Logger = LogUtil.Factory.CreateLogger(nameof(BaseSearchEngine));
@@ -83,49 +87,50 @@ public override string ToString()
8387
return $"{Name}: {BaseUrl} {Timeout}";
8488
}
8589

86-
protected virtual bool VerifyQuery(SearchQuery q)
90+
public virtual bool VerifyQuery(SearchQuery q)
8791
{
8892
if (q.Upload is not { }) {
8993
return false;
9094
}
9195

92-
bool b;
96+
bool b = true;
97+
98+
if (MaxSize.HasValue) {
99+
b = q.Size <= MaxSize;
100+
}
93101

94-
if (MaxSize == NA_SIZE || q.Size == NA_SIZE) {
102+
/*if (MaxSize == NA_SIZE || q.Size == NA_SIZE) {
95103
b = true;
96104
}
97105
98106
else {
99107
b = q.Size <= MaxSize;
100-
}
108+
}*/
101109

102110
return b;
103111
}
104112

105-
protected virtual SearchResultStatus Verify(SearchQuery q)
106-
{
107-
var b = VerifyQuery(q);
108-
109-
return !b ? SearchResultStatus.IllegalInput : SearchResultStatus.None;
110-
}
111-
112113
public virtual async Task<SearchResult> GetResultAsync(SearchQuery query, CancellationToken token = default)
113114
{
115+
var b = VerifyQuery(query);
114116

115-
var b = Verify(query);
117+
/*
118+
if (!b) {
119+
// throw new SmartImageException($"{query}");
120+
Debug.WriteLine($"{query} : Verification error", LogCategories.C_ERROR);
121+
}
122+
*/
116123

117-
/*if (!b) {
118-
throw new SmartImageException($"{query}");
119-
}*/
124+
var srs = b ? SearchResultStatus.None : SearchResultStatus.IllegalInput;
120125

121126
var res = new SearchResult(this)
122127
{
123128
RawUrl = await GetRawUrlAsync(query),
124-
Status = b,
125-
ErrorMessage = null
129+
ErrorMessage = null,
130+
Status = srs
126131
};
127132

128-
Debug.WriteLine($"{Name} | {query} - {res.Status}", nameof(GetResultAsync));
133+
Debug.WriteLine($"{Name} | {query} - {res.Status}", LogCategories.C_INFO);
129134

130135
return res;
131136
}
@@ -143,6 +148,4 @@ protected virtual ValueTask<Url> GetRawUrlAsync(SearchQuery query)
143148
public static readonly BaseSearchEngine[] All =
144149
ReflectionHelper.CreateAllInAssembly<BaseSearchEngine>(InheritanceProperties.Subclass).ToArray();
145150

146-
public string? EndpointUrl { get; }
147-
148151
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using AngleSharp.Dom;
1010
using AngleSharp.Html.Dom;
1111
using JetBrains.Annotations;
12+
using Novus.Streams;
1213
using SmartImage.Lib.Model;
1314
using SmartImage.Lib.Results;
1415

@@ -37,6 +38,8 @@ protected static string GetHash(SearchQuery q)
3738
var b64 = Convert.ToBase64String(data).Replace("==", "");
3839
b64 = Regex.Replace(b64, @"\//", "_");
3940
b64 = Regex.Replace(b64, @"\+", "-");
41+
42+
q.Uni.Stream.TrySeek();
4043

4144
return b64;
4245
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ public sealed class Ascii2DEngine : WebSearchEngine
2323

2424
public Ascii2DEngine() : base("https://ascii2d.net/search/url/")
2525
{
26-
Timeout = TimeSpan.FromSeconds(10);
27-
MaxSize = 5 * 1000 * 1000;
26+
// Timeout = TimeSpan.FromSeconds(10);
27+
MaxSize = 10000000;
2828
}
2929

3030
protected override string NodesSelector => Serialization.S_Ascii2D_Images2;
3131

3232
public override SearchEngineOptions EngineOption => SearchEngineOptions.Ascii2D;
3333

34-
public const int MAX_WIDTH = 1000;
34+
// public const int MAX_WIDTH = 1000;
3535

36-
protected override bool VerifyQuery(SearchQuery q)
36+
/*protected override bool VerifyQuery(SearchQuery q)
3737
{
3838
var b = base.VerifyQuery(q);
3939
bool b2;
@@ -44,14 +44,14 @@ protected override bool VerifyQuery(SearchQuery q)
4444
4545
}
4646
if (ok) {
47-
b2 = q.ImageInfo.Width < MAX_WIDTH;
47+
// b2 = q.ImageInfo.Width < MAX_WIDTH;
4848
}
4949
else {
5050
b2 = true;
5151
}
5252
5353
return b && b2;
54-
}
54+
}*/
5555

5656
protected override async ValueTask<Url> GetRawUrlAsync(SearchQuery query)
5757
{

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public sealed class EHentaiEngine : WebSearchEngine, IConfig, INotifyPropertyCha
4343
CheckCertificateRevocationList = false,
4444
UseCookies = true,
4545
CookieContainer = new() { },
46-
46+
4747
};
4848

4949
public override Url BaseUrl => IsLoggedIn ? ExHentaiBase : EHentaiBase;
@@ -56,6 +56,8 @@ public sealed class EHentaiEngine : WebSearchEngine, IConfig, INotifyPropertyCha
5656

5757
public bool IsLoggedIn { get; private set; }
5858

59+
private readonly CookieCollection m_cookies;
60+
5961
#region
6062

6163
private static readonly Url EHentaiIndex = "https://forums.e-hentai.org/index.php";
@@ -69,8 +71,6 @@ public sealed class EHentaiEngine : WebSearchEngine, IConfig, INotifyPropertyCha
6971

7072
static EHentaiEngine() { }
7173

72-
private readonly CookieCollection m_cookies;
73-
7474
public EHentaiEngine() : base(EHentaiBase)
7575
{
7676
m_client = new HttpClient(m_clientHandler);
@@ -230,6 +230,8 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
230230

231231
//todo
232232
m_clientHandler.CookieContainer.Add(m_cookies);
233+
234+
m_client.Timeout = Timeout;
233235

234236
Debug.WriteLine($"{LookupUrl}", nameof(GetDocumentAsync));
235237

@@ -240,6 +242,7 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
240242
{
241243
{ "User-Agent", HttpUtilities.UserAgent }
242244
},
245+
243246
};
244247

245248
var res = await m_client.SendAsync(req, token);
@@ -278,7 +281,8 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
278281

279282
private Task<IFlurlResponse> GetSessionAsync(bool useEx = false)
280283
{
281-
return (useEx ? ExHentaiBase : EHentaiBase).WithCookies(m_cookies)
284+
return (useEx ? ExHentaiBase : EHentaiBase)
285+
.WithCookies(m_cookies)
282286
.WithHeaders(new
283287
{
284288
User_Agent = HttpUtilities.UserAgent

0 commit comments

Comments
 (0)