Skip to content

Commit b4519f7

Browse files
committed
fixes, improvements
1 parent d026fb4 commit b4519f7

File tree

14 files changed

+217
-255
lines changed

14 files changed

+217
-255
lines changed

SmartImage.Lib/Engines/Search/IqdbEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ protected override SearchResult Process(object obj, SearchResult sr)
140140
// Don't select other results
141141
var query = (ImageQuery) obj;
142142
var doc = GetDocument(query);
143-
143+
doc.Wait();
144+
144145
var pages = doc.Result.Body.SelectSingleNode("//div[@id='pages']");
145146
var tables = ((IHtmlElement) pages).SelectNodes("div/table");
146147

SmartImage.Lib/SearchClient.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public SearchClient(SearchConfig config)
9999

100100
public List<Task<SearchResult>> Tasks { get; private set; }
101101

102-
private List<Task> ContinueTasks { get; }
102+
public List<Task> ContinueTasks { get; }
103103

104104
public WaitHandle DirectResultsWaitHandle { get; private set; }
105105

@@ -266,27 +266,25 @@ private void GetResultContinueCallback(Task<SearchResult> task, object state)
266266
if (!value.IsSuccessful || !value.IsNonPrimitive || value.Scanned) {
267267
return;
268268
}
269-
270-
// var task2 = value.FindDirectResultsAsync();
271-
// task2.Wait();
272-
// var result = task2.Result;
273-
274-
var result = value.FindDirectResultsAsync();
269+
270+
var result = value.ScanForImages();
275271

276272
if (result.Any()) {
277-
result = result /*.Where(x => x.Direct != null)*/
278-
.ToList();
279273

280274
DirectResults.AddRange(result);
281275

282276
value.Scanned = true;
283277
var autoResetEvent = ((AutoResetEvent)DirectResultsWaitHandle);
284278

279+
if (DirectResults.Count > 0 /*||
280+
!DirectResultsWaitHandle.SafeWaitHandle.IsClosed*/ /*|| ContinueTasks.Count==1*/) {
281+
282+
if (!DirectResultsWaitHandle.SafeWaitHandle.IsClosed) {
283+
Debug.WriteLine("wait handle set");
284+
autoResetEvent.Set();
285+
286+
}
285287

286-
if (DirectResults.Count > 0 &&
287-
!DirectResultsWaitHandle.SafeWaitHandle.IsClosed /*|| ContinueTasks.Count==1*/) {
288-
Debug.WriteLine("wait handle set");
289-
autoResetEvent.Set();
290288
}
291289

292290
DirectResultCompleted?.Invoke(null, EventArgs.Empty);
@@ -312,7 +310,7 @@ public async Task RefineSearchAsync()
312310
throw new SmartImageException("Could not find direct result");
313311
}
314312

315-
var direct = directResult.Direct.Url;
313+
var direct = directResult.DirectImage.Url;
316314

317315
Debug.WriteLine($"{nameof(SearchClient)}: Refining by {direct}", C_DEBUG);
318316

@@ -329,7 +327,8 @@ public static List<ImageResult> ApplyPredicateFilter(List<SearchResult> results,
329327
.SelectMany(r => r.AllResults)
330328
.OrderByDescending(r => r.Similarity)
331329
.ThenByDescending(r => r.PixelResolution)
332-
.ThenByDescending(r => r.DetailScore).ToList();
330+
.ThenByDescending(r => r.DetailScore)
331+
.ToList();
333332

334333
return query;
335334
}
@@ -400,7 +399,6 @@ public void Dispose()
400399

401400
if (!DirectResultsWaitHandle.SafeWaitHandle.IsClosed) {
402401
DirectResultsWaitHandle.Dispose();
403-
404402
}
405403
}
406404
}

SmartImage.Lib/Searching/ImageQuery.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,22 @@ public static (bool IsUri, bool IsFile) IsUriOrFile(string x)
105105
public ImageResult GetImageResult()
106106
{
107107

108+
109+
DirectImage directImage = new()
110+
{
111+
// Stream = Stream,
112+
Url = UploadUri
113+
};
114+
108115
var result = new ImageResult
109116
{
110-
Url = UploadUri,
117+
111118
// Image = Image.FromStream(Stream),
112-
Direct =
113-
{
114-
// Stream = Stream,
115-
Url = UploadUri
116-
}
119+
Url = UploadUri
117120
};
118121

122+
result.DirectImages.Add(directImage);
123+
119124
result.OtherMetadata.Add("Upload engine", UploadEngine.Name);
120125
result.OtherMetadata.Add("Input type", IsUri ? "URI" : "File");
121126
result.OtherMetadata.Add("Input value", Value);

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -197,51 +197,51 @@ private static List<FieldInfo> GetDetailFields()
197197

198198
public void UpdateFrom(ImageResult result)
199199
{
200-
Url = result.Url;
201-
Direct = result.Direct;
202-
Similarity = result.Similarity;
203-
Width = result.Width;
204-
Height = result.Height;
205-
Source = result.Source;
206-
Characters = result.Characters;
207-
Artist = result.Artist;
208-
Site = result.Site;
209-
Description = result.Description;
210-
Date = result.Date;
200+
Url = result.Url;
201+
DirectImages = result.DirectImages;
202+
Similarity = result.Similarity;
203+
Width = result.Width;
204+
Height = result.Height;
205+
Source = result.Source;
206+
Characters = result.Characters;
207+
Artist = result.Artist;
208+
Site = result.Site;
209+
Description = result.Description;
210+
Date = result.Date;
211211

212212
}
213213

214-
public DirectImage Direct { get; internal set; } = new();
214+
public List<DirectImage> DirectImages { get; internal set; } = new() { };
215215

216-
public async Task<bool> ScanForImagesAsync()
216+
public DirectImage DirectImage
217217
{
218-
if (Url==null) {
218+
get => DirectImages.FirstOrDefault();
219+
set => DirectImages[0] = value;
220+
}
221+
222+
public bool ScanForImages()
223+
{
224+
if (Url == null) {
219225
return false;
220226
}
221227

222-
if (Direct is {Url: {}} || IsAlreadyDirect()) {
228+
if (DirectImage is { Url: { } } || IsAlreadyDirect()) {
223229
return true;
224230
}
225231

226232
try {
227233

228-
var directImages = await ImageHelper.ScanForImages(Url.ToString());
234+
var directImages = ImageHelper.ScanForImages(Url.ToString())
235+
.Where(x => x is { Url: { } })
236+
.ToList();
229237

230238
if (directImages.Any()) {
231239
Debug.WriteLine($"{Url}: Found {directImages.Count} direct images");
232240

233-
var direct = directImages.First();
234-
235-
if (direct != null) {
236-
Direct = direct;
237241

238-
for (int i = 1; i < directImages.Count; i++) {
239-
directImages[i].Dispose();
240-
}
241-
242-
return true;
243-
}
242+
DirectImages.AddRange(directImages);
244243

244+
return true;
245245
}
246246
}
247247
catch {
@@ -263,7 +263,7 @@ private bool IsAlreadyDirect()
263263
var b = ImageHelper.IsImage(s, out var di);
264264

265265
if (b) {
266-
Direct = di;
266+
DirectImages.Add(di);
267267
}
268268
else {
269269
di.Dispose();
@@ -279,11 +279,11 @@ public Dictionary<string, object> Data
279279
var map = new Dictionary<string, object>
280280
{
281281
{ nameof(Url), Url },
282-
{ "Direct Url", Direct.Url }
282+
{ "Direct Url", DirectImage?.Url }
283283
};
284284

285285
if (Similarity.HasValue) {
286-
map.Add($"{nameof(Similarity)}", $"{Similarity.Value/100:P}");
286+
map.Add($"{nameof(Similarity)}", $"{Similarity.Value / 100:P}");
287287
}
288288

289289
if (HasImageDimensions) {
@@ -322,7 +322,10 @@ public Dictionary<string, object> Data
322322

323323
public void Dispose()
324324
{
325-
Direct?.Dispose();
325+
326+
for (int i = 0; i < DirectImages.Count; i++) {
327+
DirectImages[i].Dispose();
328+
}
326329
}
327330

328331
public ConsoleOption GetConsoleOption()
@@ -333,7 +336,7 @@ public ConsoleOption GetConsoleOption()
333336
Functions =
334337
{
335338
[ConsoleOption.NC_FN_MAIN] = IResult.CreateOpenFunction(Url),
336-
[ConsoleOption.NC_FN_COMBO] = IResult.CreateDownloadFunction(() => Direct.Url)
339+
[ConsoleOption.NC_FN_COMBO] = IResult.CreateDownloadFunction(() => DirectImage.Url)
337340
}
338341
};
339342

SmartImage.Lib/Searching/SearchResult.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public class SearchResult : IResult
8080
/// </summary>
8181
public List<ImageResult> AllResults => OtherResults.Union(new[] { PrimaryResult }).ToList();
8282

83+
84+
8385
/// <summary>
8486
/// Undifferentiated URI
8587
/// </summary>
@@ -143,38 +145,25 @@ public SearchResult(BaseSearchEngine engine)
143145

144146
public bool Scanned { get; internal set; }
145147

146-
public List<ImageResult> FindDirectResultsAsync()
148+
public List<ImageResult> ScanForImages()
147149
{
148150
Debug.WriteLine($"searching within {Engine.Name}");
149151

150152
var directResults = new List<ImageResult>();
151153

152-
var ll = Parallel.For(0, AllResults.Count, async (i, pls) =>
154+
var ll = Parallel.For(0, AllResults.Count, (i, pls) =>
153155
{
154156
var allResult = AllResults[i];
155157

156-
var task = allResult.ScanForImagesAsync();
157-
task.Wait();
158-
var b = task.Result;
158+
var b = allResult.ScanForImages();
159159

160-
if (b && !directResults.Contains(allResult) && allResult.Direct != null) {
161-
Debug.WriteLine($"{nameof(SearchResult)}: Found direct result {allResult.Direct.Url}");
160+
if (b && !directResults.Contains(allResult)) {
161+
Debug.WriteLine($"{nameof(SearchResult)}: Found direct result {allResult.DirectImage.Url}");
162162

163163
directResults.Add(allResult);
164-
PrimaryResult.Direct.Url ??= allResult.Direct.Url;
164+
165165
}
166166
});
167-
/*foreach (ImageResult ir in AllResults) {
168-
var b = await ir.ScanForImagesAsync();
169-
170-
if (b && !directResults.Contains(ir))
171-
{
172-
// Debug.WriteLine($"{nameof(SearchResult)}: Found direct result {ir.Direct.Url}");
173-
174-
directResults.Add(ir);
175-
PrimaryResult.Direct.Url ??= ir.Direct.Url;
176-
}
177-
}*/
178167

179168
Scanned = true;
180169

@@ -193,9 +182,8 @@ public ConsoleOption GetConsoleOption()
193182

194183
Functions = new()
195184
{
196-
[ConsoleOption.NC_FN_MAIN] = IResult.CreateOpenFunction(PrimaryResult is { Url: { } }
197-
? PrimaryResult.Url
198-
: RawUri),
185+
[ConsoleOption.NC_FN_MAIN] = IResult.CreateOpenFunction(
186+
PrimaryResult is { Url: { } } ? PrimaryResult.Url : RawUri),
199187

200188
[ConsoleOption.NC_FN_SHIFT] = IResult.CreateOpenFunction(RawUri),
201189

@@ -238,7 +226,7 @@ public ConsoleOption GetConsoleOption()
238226
};
239227

240228
option.Functions[ConsoleOption.NC_FN_COMBO] =
241-
IResult.CreateDownloadFunction(() => PrimaryResult.Direct.Url);
229+
IResult.CreateDownloadFunction(() => PrimaryResult.DirectImage.Url);
242230

243231
option.Functions[ConsoleOption.NC_FN_CTRL] = () =>
244232
{
@@ -248,7 +236,7 @@ public ConsoleOption GetConsoleOption()
248236
ConsoleProgressIndicator.Instance.Start(cts);
249237
}
250238

251-
_ = FindDirectResultsAsync();
239+
_ = ScanForImages();
252240

253241
cts.Cancel();
254242
cts.Dispose();

SmartImage.Lib/SmartImage.Lib.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net6.0</TargetFramework>
55
<PublishSingleFile>true</PublishSingleFile>
66
<PublishTrimmed>true</PublishTrimmed>
7-
<Configurations>Debug;Release</Configurations>
7+
<Configurations>Debug;Release;Test;Test</Configurations>
88
</PropertyGroup>
99

1010
<ItemGroup>
@@ -33,8 +33,8 @@
3333
<HintPath>..\..\..\VSProjects\Novus\Novus\bin\Release\net6.0\Novus.dll</HintPath>
3434
</Reference>
3535
</ItemGroup>
36-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='TestDebug|AnyCPU'">
37-
<DefineConstants>DEBUG;TRACE;JETBRAINS_ANNOTATIONS;TEST_DEBUG</DefineConstants>
36+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Test|AnyCPU'">
37+
<DefineConstants>DEBUG;TRACE;JETBRAINS_ANNOTATIONS;TEST</DefineConstants>
3838
<!-- <NoWarn>CA1416</NoWarn> -->
3939
</PropertyGroup>
4040
</Project>

0 commit comments

Comments
 (0)