Skip to content

Commit 6f92361

Browse files
committed
Fix ImageScanner; UI work
1 parent 2d61459 commit 6f92361

File tree

7 files changed

+94
-59
lines changed

7 files changed

+94
-59
lines changed

SmartImage.Lib/Clients/FlareSolverrClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ public bool Configure(string api)
3838
}
3939

4040
private FlareSolverrClient() { }
41+
4142
static FlareSolverrClient()
4243
{
4344

4445
}
46+
4547
public static FlareSolverrClient Value { get; private set; } = new();
4648

4749
public void Dispose()

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected virtual Url GetRawUrl(SearchQuery query)
152152
return u;
153153
}
154154

155-
public virtual async ValueTask<bool> VerifyQueryAsync(SearchQuery q)
155+
public virtual ValueTask<bool> VerifyQueryAsync(SearchQuery q)
156156
{
157157
/*if (q.Upload is not { }) {
158158
return false;
@@ -172,7 +172,7 @@ public virtual async ValueTask<bool> VerifyQueryAsync(SearchQuery q)
172172
b = q.Size <= MaxSize;
173173
}*/
174174

175-
return b;
175+
return ValueTask.FromResult(b);
176176
}
177177

178178

SmartImage.Lib/Images/ImageScanner.cs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using SmartImage.Lib.Engines;
3232
using SmartImage.Lib.Images.Uni;
3333
using SmartImage.Lib.Results;
34+
using SmartImage.Lib.Results.Data;
3435
using SmartImage.Lib.Utilities;
3536

3637
// ReSharper disable InconsistentNaming
@@ -162,6 +163,7 @@ public static async ValueTask<IFlurlRequest> BuildRequest(Url u, CancellationTok
162163
return req.WithCookies(Cookies);
163164
}
164165

166+
165167
public static async Task<List<UniSimilarity>> Analyze(List<Task<UniImage>> tasks, SearchQuery query,
166168
CancellationToken ct = default)
167169
{
@@ -170,21 +172,18 @@ public static async Task<List<UniSimilarity>> Analyze(List<Task<UniImage>> tasks
170172
query.Uni.Stream.TrySeek();
171173
var rg = new List<UniSimilarity>();
172174

173-
while (tasks.Count != 0)
174-
{
175+
while (tasks.Count != 0) {
175176
var task = await Task.WhenAny(tasks);
176177
tasks.Remove(task);
177178
var ux = await task;
178179

179-
if (ux != UniImage.Null && ux.HasImageFormat)
180-
{
180+
if (ux != UniImage.Null && ux.HasImageFormat) {
181181
var cmp = ph.Hash(ux.Stream);
182182
var sim = CompareHash.Similarity(orig, cmp);
183183
rg.Add(new UniSimilarity(ux, sim));
184184
ux.Stream.TrySeek();
185185
}
186-
else
187-
{
186+
else {
188187
ux.Dispose();
189188
ux = null;
190189
}
@@ -280,31 +279,32 @@ public static async Task<List<Task<UniImage>>> ScanImagesAsync(Url u, Cancellati
280279
autoDisposeOnError: false, ct: ct);
281280

282281

283-
if (uf != UniImage.Null)
284-
{
285-
if (uf.HasImageFormat)
286-
{
287-
tasks = [Task.FromResult(uf)];
282+
if (uf != UniImage.Null && uf.HasImageFormat) {
283+
tasks = [Task.FromResult(uf)];
284+
285+
goto ret;
288286

289-
goto ret;
290-
}
291287
}
292-
else
293-
{
294-
stream = uf.Stream;
295-
stream.Position = 0;
288+
else {
289+
// stream = uf.Stream;
290+
// stream.Position = 0;
291+
uf.Stream.TrySeek();
292+
293+
res = await Client.Request(u)
294+
.GetAsync(cancellationToken: ct);
295+
stream = await res.GetStreamAsync();
296296
}
297297

298-
if (!stream.CanRead)
299-
{
298+
if (!stream.CanRead) {
300299
stream.Dispose();
301300
goto ret;
302301
}
303302

304-
var sr = new StreamReader(stream, leaveOpen: true /*false*/);
303+
var sr = new StreamReader(stream, leaveOpen: /*true*/ false);
305304

306305
string html = await sr.ReadToEndAsync(ct);
307-
var urls = GetImageUrls(html, u);
306+
307+
var urls = GetImageUrls(html, u);
308308

309309
tasks = urls.Select(s =>
310310
{
@@ -361,6 +361,7 @@ await Parallel.ForEachAsync(urls, po, async (s, token) =>
361361
// stream.Dispose(); // todo?
362362
// res.Dispose();
363363

364+
sr.Dispose();
364365
ret:
365366
return tasks;
366367

@@ -376,18 +377,14 @@ public static IEnumerable<string> GetImageUrls(string html, Url url)
376377
Match baseMatch = r_imgHtml.Match(html);
377378
string baseUrl;
378379

379-
if (baseMatch.Success)
380-
{
380+
if (baseMatch.Success) {
381381
baseUrl = baseMatch.Groups["url"].Value.TrimEnd(URL_DELIM);
382382
}
383-
else
384-
{
385-
if (url.ToString().EndsWith(URL_DELIM))
386-
{
383+
else {
384+
if (url.ToString().EndsWith(URL_DELIM)) {
387385
baseUrl = url.ToString().TrimEnd(URL_DELIM);
388386
}
389-
else
390-
{
387+
else {
391388
baseUrl = Url.Parse(url); //todo
392389

393390
// or Path.GetDirectoryName?
@@ -445,8 +442,7 @@ await Parallel.ForEachAsync(s2, ct, async (s1, token) =>
445442
{
446443
var uni = await UniImage.TryCreateAsync(s1, ct: token);
447444

448-
if (uni != null)
449-
{
445+
if (uni != null) {
450446
rg.Add(uni);
451447
}
452448

SmartImage.Lib/Images/Uni/UniImageUri.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@ public static bool IsUriType(object o, out Url u)
3030
{
3131
u = o switch
3232
{
33-
Url u2 => u2,
34-
string s => s,
35-
_ => null
33+
Url u2 => u2,
34+
string s when Flurl.Url.IsValid(s) => s,
35+
_ => null
3636
};
3737

38+
if (u == null) {
39+
return false;
40+
}
41+
3842
var scheme = u.Scheme;
3943

40-
return Url.IsValid(u) && Schemes.All(s => scheme != s);
44+
45+
return Schemes.All(s => scheme != s);
4146
}
4247

4348
public override async ValueTask<bool> Alloc(CancellationToken ct = default)

SmartImage.Lib/SearchClient.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public async Task<SearchResult[]> RunSearchAsync(SearchQuery query,
180180
IsRunning = true;
181181

182182
if (!ConfigApplied) {
183-
await LoadEnginesAsync(); // todo
183+
await LoadEnginesAsync(token); // todo
184184

185185
}
186186
else {
@@ -359,6 +359,18 @@ public async ValueTask LoadEnginesAsync(CancellationToken token = default)
359359
if (!ok) {
360360
Debugger.Break();
361361
}
362+
else {
363+
// Ensure FlareSolverr
364+
365+
try {
366+
var idx = await FlareSolverrClient.Value.Clearance.Solverr.GetIndexAsync();
367+
}
368+
catch (Exception e) {
369+
Trace.WriteLine($"{nameof(FlareSolverrClient)}: {e.Message}");
370+
Config.FlareSolverr = false;
371+
FlareSolverrClient.Value.Dispose();
372+
}
373+
}
362374
}
363375

364376
foreach (BaseSearchEngine bse in Engines) {

SmartImage.UI/MainWindow.xaml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161

162162
</Style.Triggers>
163163
</Style>
164+
<SolidColorBrush x:Key="White1" Color="#FFE1E1E1"/>
164165

165166
</Window.Resources>
166167

@@ -672,7 +673,7 @@
672673
</DataTemplate>
673674
</GridViewColumn.CellTemplate>
674675
</GridViewColumn>
675-
676+
676677
<GridViewColumn Header="Name"
677678
Width="135" x:Name="Lvgvc_Name"
678679
DisplayMemberBinding="{Binding Name, Mode=OneTime}">
@@ -827,11 +828,11 @@
827828
<Grid Background="{DynamicResource Black1}">
828829
<ListBox x:Name="Lb_Engines" d:ItemsSource="{Binding Engines}" Margin="27,33,0,0"
829830
SelectionMode="Multiple" SelectionChanged="Lb_Engines_SelectionChanged" Width="175"
830-
VerticalAlignment="Top" HorizontalAlignment="Left" Height="400" />
831+
VerticalAlignment="Top" HorizontalAlignment="Left" Height="400" Background="{DynamicResource Black2}" Foreground="{DynamicResource White1}" />
831832

832833
<ListBox x:Name="Lb_Engines2" d:ItemsSource="{Binding Engines}" Margin="228,33,0,0"
833834
SelectionMode="Multiple" SelectionChanged="Lb_Engines2_SelectionChanged" Width="175"
834-
HorizontalAlignment="Left" VerticalAlignment="Top" Height="400" />
835+
HorizontalAlignment="Left" VerticalAlignment="Top" Height="400" Background="{DynamicResource Black2}" Foreground="{DynamicResource White1}" />
835836

836837
<CheckBox x:Name="Cb_Clipboard" Content="Clipboard"
837838
IsChecked="{Binding Path=UseClipboard, Mode=TwoWay}" HorizontalAlignment="Left"
@@ -867,19 +868,23 @@
867868
<Label x:Name="Lbl_PriorityEngines" Content="Priority Engines" HorizontalAlignment="Left"
868869
Margin="228,2,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" />
869870

870-
<ComboBox x:Name="Cm_UploadEngine" HorizontalAlignment="Center" Margin="0,34,0,0"
871-
VerticalAlignment="Top" Width="120"
871+
<ComboBox x:Name="Cm_UploadEngine" HorizontalAlignment="Left" Margin="600,34,0,0"
872+
VerticalAlignment="Top" Width="180"
872873
ItemsSource="{x:Static controls:ControlsHelper.UploadEngineNames}"
873874
SelectionChanged="Cm_UploadEngine_SelectionChanged" />
874875

875876
<Label x:Name="Lbl_Options" Content="Options" HorizontalAlignment="Left"
876-
Margin="426,3,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" />
877+
Margin="425,3,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" />
877878

878879
<Label x:Name="Lbl_UploadEngines" Content="Upload Engine" HorizontalAlignment="Left"
879880
Margin="600,3,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" />
880881

881-
<TextBox x:Name="Tb_FlareSolverrApi" HorizontalAlignment="Left" Margin="607,92,0,0" TextWrapping="Wrap"
882-
Text="{Binding Config.FlareSolverrApiUrl, Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>
882+
<TextBox x:Name="Tb_FlareSolverrApi" HorizontalAlignment="Left" Margin="600,92,0,0" TextWrapping="Wrap"
883+
Text="{Binding Config.FlareSolverrApiUrl, Mode=TwoWay}" VerticalAlignment="Top" Width="180"
884+
Background="{DynamicResource Black2}" Foreground="{DynamicResource White1}"/>
885+
886+
<Label x:Name="Lbl_FlareSolverrApi" Content="Flare Solverr API" HorizontalAlignment="Left"
887+
Margin="600,60,0,0" VerticalAlignment="Top" Foreground="White" FontWeight="Bold" />
883888

884889

885890

SmartImage.UI/MainWindow.xaml.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
using System.Buffers;
6868
using System.Reflection;
6969
using DynamicData;
70+
using SmartImage.Lib.Clients;
7071
using SmartImage.UI.Controls;
7172
using SmartImage.Lib.Images;
7273
using SmartImage.Lib.Images.Uni;
@@ -98,8 +99,16 @@ public MainWindow()
9899

99100
InitializeComponent();
100101

101-
m_us = new SemaphoreSlim(1, 1);
102-
m_us2 = new SemaphoreSlim(1, 1);
102+
/*Config.PropertyChanged += (sender, args) =>
103+
{
104+
if (args.PropertyName == nameof(Config.FlareSolverr)) {
105+
Tb_FlareSolverrApi.IsEnabled = Config.FlareSolverr;
106+
}
107+
};*/
108+
109+
m_us = new SemaphoreSlim(1, 1);
110+
m_us2 = new SemaphoreSlim(1, 1);
111+
103112
// DataContext = this;
104113
SearchStart = default;
105114

@@ -145,6 +154,7 @@ public MainWindow()
145154
{
146155
Interval = TimeSpan.FromSeconds(3)
147156
};
157+
148158
// m_trDispatch.Tick += IdleDispatchAsync; // TODO: disabled
149159

150160
m_uni = new();
@@ -185,6 +195,7 @@ public MainWindow()
185195
ParseArgs(Args);
186196
AddHandler(Validation.ErrorEvent, new RoutedEventHandler(OnValidationRaised));
187197

198+
188199
}
189200

190201
#region
@@ -251,7 +262,8 @@ public bool CanReload
251262
get => m_canReload;
252263
set
253264
{
254-
if (value == m_canReload) return;
265+
if (value == m_canReload)
266+
return;
255267

256268
m_canReload = value;
257269
OnPropertyChanged();
@@ -297,7 +309,9 @@ public string? Input
297309
set
298310
{
299311
value = value?.CleanString();
300-
if (value == m_input) return;
312+
313+
if (value == m_input)
314+
return;
301315

302316
m_input = value;
303317
OnPropertyChanged();
@@ -318,7 +332,8 @@ public QueryModel CurrentQuery
318332
{
319333

320334
if (Equals(value, m_currentQuery) /*|| Query?.ValueString == value*/
321-
/* || (String.IsNullOrWhiteSpace(value))*/) return;
335+
/* || (String.IsNullOrWhiteSpace(value))*/)
336+
return;
322337

323338
m_currentQuery = value;
324339
OnPropertyChanged();
@@ -515,10 +530,7 @@ private void HandleQueryAsync()
515530

516531
}
517532

518-
Tb_Status.Dispatcher.Invoke(() =>
519-
{
520-
Tb_Status.Text = tbs;
521-
});
533+
Tb_Status.Dispatcher.Invoke(() => { Tb_Status.Text = tbs; });
522534

523535
Btn_Run.IsEnabled = CurrentQuery.CanSearch;
524536

@@ -576,7 +588,8 @@ private void AdvanceQueue(int i = 1)
576588
if (Queue.Count == 0) {
577589
next = new QueryModel();
578590
}
579-
else next = Queue[(Queue.IndexOf(CurrentQuery) + i) % Queue.Count];
591+
else
592+
next = Queue[(Queue.IndexOf(CurrentQuery) + i) % Queue.Count];
580593

581594
CurrentQuery = next;
582595
}
@@ -886,8 +899,9 @@ private void ClearQueryControls()
886899

887900
Tb_Upload.Text = String.Empty;
888901
Pb_Preview.IsIndeterminate = false;
902+
889903
// Tb_Preview.Text = String.Empty;
890-
Lb_Queue.IsEnabled = true;
904+
Lb_Queue.IsEnabled = true;
891905
}
892906

893907
private void ClearResults(bool full = false)
@@ -918,8 +932,8 @@ private void Reset()
918932
{
919933
Restart(true);
920934
ClearQueryControls();
921-
922-
935+
936+
923937
Lb_Upload.Foreground = Brushes.White;
924938

925939
// SetQueue(null);
@@ -1494,4 +1508,5 @@ private void SetRenderMode()
14941508
}
14951509
}
14961510

1511+
14971512
}

0 commit comments

Comments
 (0)