Skip to content

Commit 176dffa

Browse files
committed
Fix null pointers; rework result/engine model; *
1 parent 8d34e6c commit 176dffa

File tree

11 files changed

+137
-120
lines changed

11 files changed

+137
-120
lines changed

SmartImage.Lib 3/Engines/BaseSearchEngine.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,26 @@ public virtual async Task<SearchResult> GetResultAsync(SearchQuery query, Cancel
125125

126126
var res = new SearchResult(this)
127127
{
128-
RawUrl = await GetRawUrlAsync(query),
128+
RawUrl = GetRawUrl(query),
129129
ErrorMessage = null,
130-
Status = srs
130+
Status = srs
131131
};
132132

133+
lock (res.Results) {
134+
res.Results.Add(res.GetRawResultItem());
135+
}
136+
133137
Debug.WriteLine($"{Name} | {query} - {res.Status}", LogCategories.C_INFO);
134138

135139
return res;
136140
}
137141

138-
protected virtual ValueTask<Url> GetRawUrlAsync(SearchQuery query)
142+
protected virtual Url GetRawUrl(SearchQuery query)
139143
{
140144
//
141145
Url u = ((BaseUrl + query.Upload));
142146

143-
return ValueTask.FromResult(u);
147+
return u;
144148
}
145149

146150
public abstract void Dispose();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ protected static string GetHash(SearchQuery q)
3838
var b64 = Convert.ToBase64String(data).Replace("==", "");
3939
b64 = Regex.Replace(b64, @"\//", "_");
4040
b64 = Regex.Replace(b64, @"\+", "-");
41-
41+
4242
q.Image.Stream.TrySeek();
4343

4444
return b64;
4545
}
4646

47-
protected override ValueTask<Url> GetRawUrlAsync(SearchQuery query)
47+
protected override Url GetRawUrl(SearchQuery query)
4848
{
4949
Base64Hash = GetHash(query);
5050

51-
return ValueTask.FromResult(BaseUrl.AppendPathSegments("image").AppendPathSegment(Base64Hash));
51+
return (BaseUrl.AppendPathSegments("image").AppendPathSegment(Base64Hash));
5252
}
5353

5454
protected override ValueTask<SearchResultItem> ParseResultItem(INode n, SearchResult r)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public Ascii2DEngine() : base("https://ascii2d.net/search/url/")
5353
return b && b2;
5454
}*/
5555

56-
protected override async ValueTask<Url> GetRawUrlAsync(SearchQuery query)
56+
protected override Url GetRawUrl(SearchQuery query)
5757
{
58-
var url = await base.GetRawUrlAsync(query);
58+
var url = base.GetRawUrl(query);
5959

6060
/*url = url.SetQueryParams(new
6161
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
8585
return sr;
8686
}
8787

88-
protected override async ValueTask<Url> GetRawUrlAsync(SearchQuery query)
88+
protected override Url GetRawUrl(SearchQuery query)
8989
{
90-
return await base.GetRawUrlAsync(query);
90+
return base.GetRawUrl(query);
9191
}
9292

9393
public override SearchEngineOptions EngineOption => SearchEngineOptions.Fluffle;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
121121
{
122122
// var sr = await base.GetResultAsync(query, token);
123123

124-
var url = await GetRawUrlAsync(query);
124+
var url = GetRawUrl(query);
125125

126126
var sr = new SearchResult(this)
127127
{

SmartImage.Lib 3/Results/SearchResult.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Concurrent;
22
using System.ComponentModel;
3+
using System.Diagnostics;
34
using System.Net;
45
using System.Runtime.CompilerServices;
56
using Flurl.Http;
@@ -67,7 +68,10 @@ public sealed class SearchResult : IDisposable, INotifyPropertyChanged
6768
/// </summary>
6869
public Url RawUrl { get; internal set; }
6970

70-
public List<SearchResultItem> Results { get; internal set; }
71+
/// <summary>
72+
/// Results; first element should be <see cref="GetRawResultItem"/>
73+
/// </summary>
74+
public List<SearchResultItem> Results { get; }
7175

7276
[CBN]
7377
public string ErrorMessage { get; internal set; }
@@ -81,6 +85,8 @@ public sealed class SearchResult : IDisposable, INotifyPropertyChanged
8185
public SearchResultItem GetBestResult()
8286
{
8387
if (Results.Count == 0) {
88+
// This should never happen so long as results contains the raw item
89+
Debugger.Break();
8490
return null;
8591
}
8692

@@ -91,7 +97,9 @@ public SearchResultItem GetBestResult()
9197
internal SearchResult(BaseSearchEngine bse)
9298
{
9399
Engine = bse;
94-
Results = [];
100+
Results=[];
101+
102+
// Results = [GetRawResultItem()];
95103
}
96104

97105
public override string ToString()
@@ -149,7 +157,7 @@ private bool SetField<T>(ref T field, T value, [CallerMemberName] string propert
149157
return true;
150158
}
151159

152-
public SearchResultItem AsRawResultItem()
160+
public SearchResultItem GetRawResultItem()
153161
{
154162
return new SearchResultItem(this)
155163
{

SmartImage.Lib 3/SearchClient.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,26 +181,19 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, bool reload
181181
//todo
182182

183183
try {
184-
184+
185185
var ordered = results.Select(x => x.GetBestResult())
186186
.Where(x => x != null)
187187
.OrderByDescending(x => x.Similarity);
188188

189-
var item = ordered.First();
190-
191-
if (Config.OpenRaw) {
192-
OpenResult(item.Root.AsRawResultItem());
193-
}
189+
var item = ordered.FirstOrDefault();
194190

195191
OpenResult(item);
196192
}
197193
catch (Exception e) {
198194
Debug.WriteLine($"{e.Message}");
199195

200-
SearchResult result = results.FirstOrDefault(f => f.Status.IsSuccessful()) ?? results.First();
201-
var item = result.GetBestResult();
202-
203-
OpenResult(item);
196+
Debugger.Break();
204197
}
205198

206199
/*try {
@@ -241,19 +234,24 @@ private void ProcessResult(SearchResult result)
241234
}
242235
}
243236

244-
private static void OpenResult(Url url1)
237+
private static void OpenResult([MN] Url url1)
245238
{
246239
// #if DEBUG && !TEST
247240
/*
248241
#pragma warning disable CA1822
249242
250-
// ReSharper disable once MemberCanBeMadeStatic.Local
243+
// ReSharper disable once MemberCanBeMadeStatic.Local
251244
Logger.LogDebug("Not opening {url}", url1);
252245
return;
253246
254247
#pragma warning restore CA1822
255248
*/
256249
// #else
250+
251+
if (url1 == null) {
252+
return;
253+
}
254+
257255
Logger.LogInformation("Opening {Url}", url1);
258256

259257
var b = FileSystem.Open(url1, out var proc);
@@ -275,21 +273,31 @@ private static void OpenResult(Url url1)
275273

276274
}
277275

278-
private void OpenResult(SearchResultItem result)
276+
private void OpenResult([MN] SearchResultItem result)
279277
{
280278
// #if DEBUG && !TEST
281279
/*#pragma warning disable CA1822
282280
283-
// ReSharper disable once MemberCanBeMadeStatic.Local
281+
// ReSharper disable once MemberCanBeMadeStatic.Local
284282
Logger.LogDebug("Not opening result {result}", result);
285283
return;
286284
287285
#pragma warning restore CA1822*/
288286
// #else
289-
287+
290288
OnOpen?.Invoke(this, result);
291289

292-
OpenResult(result.Url);
290+
if (result != null) {
291+
292+
if (Config.OpenRaw) {
293+
OpenResult(result.Root.GetRawResultItem().Url);
294+
}
295+
else {
296+
OpenResult(result.Url);
297+
// OpenResult(result);
298+
299+
}
300+
}
293301
// #endif
294302

295303
}

SmartImage.Rdx/IntegrationCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace SmartImage.Rdx;
88

9-
public class IntegrationCommand : Command<IntegrationCommandSettings>
9+
internal class IntegrationCommand : Command<IntegrationCommandSettings>
1010
{
1111

1212
public override int Execute(CommandContext context, IntegrationCommandSettings settings)

SmartImage.Rdx/IntegrationCommandSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace SmartImage.Rdx;
77

8-
public class IntegrationCommandSettings : CommandSettings
8+
internal class IntegrationCommandSettings : CommandSettings
99
{
1010

1111
[CommandOption("--ctx-menu")]

0 commit comments

Comments
 (0)