Skip to content

Commit cf65a5b

Browse files
committed
updates
1 parent 531cf9e commit cf65a5b

File tree

11 files changed

+81
-45
lines changed

11 files changed

+81
-45
lines changed

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ protected BaseSearchEngine(string baseUrl)
2020

2121
public abstract SearchEngineOptions EngineOption { get; }
2222

23+
2324
public virtual string Name => EngineOption.ToString();
2425

2526

@@ -62,7 +63,7 @@ public Uri GetRawResultUrl(ImageQuery query)
6263
{
6364
var uri = new Uri(BaseUrl + query.Uri);
6465

65-
bool ok = Network.IsUriAlive(uri);
66+
bool ok = Network.IsUriAlive(uri, TimeSpan.FromSeconds(5));
6667

6768
if (!ok) {
6869
Debug.WriteLine($"{uri.Host} is unavailable", C_WARN);

SmartImage.Lib/SearchClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public void Reload()
7474
.ToArray();
7575

7676
Trace.WriteLine($"Engines: {Config.SearchEngines} | {Engines.QuickJoin()}");
77+
7778
}
7879

7980
/// <summary>
@@ -113,7 +114,7 @@ public async Task RunSearchAsync()
113114
// / \
114115
// true false
115116
// / \
116-
// IsNonPrimitive [Filtered]
117+
// IsNonPrimitive [FilteredResults]
117118
// / \
118119
// true false
119120
// / \

SmartImage.Lib/SearchConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using JetBrains.Annotations;
8+
using SimpleCore.Model;
89
using SimpleCore.Utilities;
910
using SmartImage.Lib.Engines;
1011
using SmartImage.Lib.Searching;

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Drawing;
8+
using System.IO;
89
using System.Linq;
10+
using System.Net.Mime;
911
using System.Reflection;
1012
using Novus.Win32;
13+
using SimpleCore.Model;
1114
using SimpleCore.Net;
1215

1316
#nullable enable
@@ -180,34 +183,68 @@ public void UpdateFrom(ImageResult result)
180183
Description = result.Description;
181184
Date = result.Date;
182185

183-
if (result.Direct is { }) {
184-
var stream = WebUtilities.GetStream(result.Direct.ToString());
185-
var image = Image.FromStream(stream);
186-
187-
188-
Width = image.Width;
189-
Height = image.Height;
190-
191-
OtherMetadata.Add("Mime", MediaTypes.ResolveFromData(stream));
192-
}
193186

187+
ReadDirectImageData();
194188

195189
}
196190

197191
public async void FindDirectImagesAsync()
198192
{
199193
if (Url is not null) {
200-
var directImages = await ImageHelper.FindDirectImagesAsync(Url?.ToString());
201194

202-
if (directImages is { }) {
203-
string? images = directImages.FirstOrDefault();
195+
if (ImageHelper.IsDirect(Url.ToString())) {
196+
Direct = Url;
197+
198+
}
199+
else {
200+
try {
201+
202+
var directImages = await ImageHelper.FindDirectImagesAsync(Url.ToString());
203+
204+
if (directImages is { }) {
205+
string? images = directImages.FirstOrDefault();
206+
207+
if (images is { }) {
208+
var uri = new Uri(images);
209+
Direct = uri;
210+
Debug.WriteLine($"{Url} -> {Direct}");
211+
}
212+
}
213+
}
214+
catch (Exception e) {
215+
Debug.WriteLine(e);
204216

205-
if (images is { }) {
206-
var uri = new Uri(images);
207-
Direct = uri;
208-
Debug.WriteLine($"{Url} -> {Direct}");
209217
}
210218
}
219+
220+
221+
try {
222+
ReadDirectImageData();
223+
}
224+
catch (Exception) {
225+
// ignored
226+
}
227+
228+
}
229+
}
230+
231+
private void ReadDirectImageData()
232+
{
233+
if (Direct is { }) {
234+
var stream = WebUtilities.GetStream(Direct.ToString());
235+
var image = Image.FromStream(stream);
236+
237+
238+
Width = image.Width;
239+
Height = image.Height;
240+
241+
stream.Position = 0;
242+
using var ms = new MemoryStream();
243+
stream.CopyTo(ms);
244+
var rg = ms.ToArray();
245+
246+
OtherMetadata.Add("Size", MathHelper.ConvertToUnit(rg.Length));
247+
OtherMetadata.Add("Mime", MediaTypes.ResolveFromData(stream));
211248
}
212249
}
213250

SmartImage.Lib/Searching/SearchResult.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Reflection;
1010
using Novus.Utilities;
11+
using SimpleCore.Model;
1112

1213
#pragma warning disable IDE0066
1314

@@ -129,7 +130,7 @@ public string ToString(bool name)
129130
var sb = new ExtendedStringBuilder();
130131

131132
if (name) {
132-
sb.AppendLine($"[{Engine.Name}] :: ({Status}; {(!IsNonPrimitive ? RANK_P : RANK_S)})");
133+
sb.AppendLine($"[{Engine.Name}] :: ({Status}; {(!IsNonPrimitive ? "NP" : "P")})");
133134

134135
}
135136
else {
@@ -158,9 +159,5 @@ public string ToString(bool name)
158159

159160

160161
public override string ToString() => ToString(true);
161-
162-
internal const string RANK_P = "P";
163-
164-
internal const string RANK_S = "S";
165162
}
166163
}

SmartImage.Lib/SmartImage.Lib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
1616
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
1717
<PackageReference Include="RestSharp" Version="106.11.7" />
18-
<PackageReference Include="System.Drawing.Common" Version="5.0.1" />
18+
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
1919
<PackageReference Include="System.Json" Version="4.7.1" />
2020
<PackageReference Include="System.Windows.Extensions" Version="5.0.0" />
2121
</ItemGroup>

SmartImage.UI/Form.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public SmartImageForm()
6161
private SearchClient m_cl;
6262

6363
private static readonly Color[] SimilarityGradient =
64-
ColorUtilities.GetGradients(Color.Red, Color.ForestGreen, 100).ToArray();
64+
ColorHelper.GetGradients(Color.Red, Color.ForestGreen, 100).ToArray();
6565

6666
private int GetStep() => (int) Math.Ceiling(((double) 1 / m_cl.Engines.Length) * 100);
6767

SmartImage/Core/DialogBridge.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ internal static NConsoleOption CreateOption(SearchResult result)
4242

4343
return null;
4444
},
45+
4546
ComboFunction = CreateComboFunction(result.PrimaryResult),
47+
48+
Color = EngineNameColorMap[result.Engine.EngineOption],
4649

47-
Name = result.Engine.Name.AddColor(EngineNameColorMap[result.Engine.EngineOption]),
48-
Data = result.ToString(false)
50+
Name = result.Engine.Name,
51+
Data = result.ToString(false)
4952
};
5053

5154
option.CtrlFunction = () =>
@@ -56,12 +59,7 @@ internal static NConsoleOption CreateOption(SearchResult result)
5659

5760
result.OtherResults.AsParallel().ForAll(x =>
5861
{
59-
try {
60-
x.FindDirectImagesAsync();
61-
}
62-
catch (Exception) {
63-
// ignored
64-
}
62+
x.FindDirectImagesAsync();
6563
});
6664

6765
result.PrimaryResult.UpdateFrom(result.OtherResults.First());
@@ -83,9 +81,8 @@ private static NConsoleOption CreateOption(ImageResult r)
8381
{
8482
Function = CreateMainFunction(r),
8583
ComboFunction = CreateComboFunction(r),
86-
Name = $"Other result\n\b",
87-
//Data = r.ToString().Replace("\n", "\n\t"),
88-
Data = r.ToString(true)
84+
Name = "Other result\n\b",
85+
Data = r.ToString(true)
8986
};
9087

9188
return option;

SmartImage/Core/MainDialog.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public static class MainDialog
4343
{
4444
new()
4545
{
46-
Name = ">>> Run <<<".AddColor(ColorMain),
46+
Name = ">>> Run <<<",
47+
Color = ColorMain,
4748
Function = () =>
4849
{
4950
ImageQuery query = NConsole.ReadInput("Image file or direct URL", x =>
@@ -59,7 +60,8 @@ public static class MainDialog
5960

6061
new()
6162
{
62-
Name = "Engines".AddColor(ColorOther),
63+
Name = "Engines",
64+
Color = ColorOther,
6365
Function = () =>
6466
{
6567
Program.Config.SearchEngines = ReadEnum<SearchEngineOptions>();
@@ -73,7 +75,8 @@ public static class MainDialog
7375

7476
new()
7577
{
76-
Name = "Priority engines".AddColor(ColorOther),
78+
Name = "Priority engines",
79+
Color = ColorOther,
7780
Function = () =>
7881
{
7982
Program.Config.PriorityEngines = ReadEnum<SearchEngineOptions>();
@@ -90,7 +93,7 @@ public static class MainDialog
9093
Function = () =>
9194
{
9295
Program.Config.Filtering = !Program.Config.Filtering;
93-
96+
9497
MainMenuOptions[3].Name = GetFilterName(Program.Config.Filtering);
9598
SaveUpdateConfig();
9699
return null;
@@ -107,7 +110,7 @@ public static class MainDialog
107110

108111
added = OSIntegration.IsContextMenuAdded;
109112

110-
113+
111114
MainMenuOptions[4].Name = GetContextMenuName(added);
112115

113116
return null;
@@ -178,7 +181,6 @@ public static class MainDialog
178181
};
179182

180183

181-
182184
private static void SaveUpdateConfig()
183185
{
184186
Program.Client.Reload();

SmartImage/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private static async Task Main(string[] args)
108108
cts.Cancel();
109109
cts.Dispose();
110110
};
111-
111+
112112
NConsoleProgress.Queue(cts);
113113

114114
// Show results

0 commit comments

Comments
 (0)