Skip to content

Commit 966a713

Browse files
committed
updates
1 parent 9f0ffac commit 966a713

File tree

14 files changed

+1016
-1029
lines changed

14 files changed

+1016
-1029
lines changed

SmartImage.Lib/SearchClient.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using SmartImage.Lib.Upload;
1616
using static SimpleCore.Diagnostics.LogCategories;
1717

18+
// ReSharper disable LoopCanBeConvertedToQuery
19+
1820
// ReSharper disable UnusedMember.Global
1921

2022
namespace SmartImage.Lib
@@ -114,7 +116,7 @@ public async Task RunSearchAsync()
114116
// / \
115117
// true false
116118
// / \
117-
// IsNonPrimitive [FilteredResults]
119+
// IsNonPrimitive [Results]
118120
// / \
119121
// true false
120122
// / \
@@ -144,8 +146,6 @@ public async Task RunSearchAsync()
144146
IsPriority = isPriority
145147
});
146148

147-
//!(Config.Filter && !value.IsNonPrimitive)
148-
149149

150150
IsComplete = !tasks.Any();
151151
}
@@ -178,7 +178,7 @@ public async Task RefineSearchAsync()
178178
throw new SmartImageException(ERR_NO_BEST_RESULT);
179179
}
180180

181-
var uri = best.Url;
181+
var uri = best.Direct;
182182

183183
Debug.WriteLine($"Refining by {uri}");
184184

@@ -211,26 +211,33 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
211211

212212
public ImageResult[] FindDirectResults()
213213
{
214+
214215
var best = FindBestResults();
215216

217+
const int FRAG_SIZE = 10;
218+
219+
var frag = best.Chunk(FRAG_SIZE).ToList();
216220

217-
best.AsParallel().ForAll(delegate(ImageResult f)
218-
{
219-
f.FindDirectImages();
220-
});
221+
var tasks = new List<Task>();
221222

222-
//foreach (var result in best) {
223-
// result.FindDirectImagesAsync();
224-
//}
223+
for (int i = 0; i < frag.Count; i++) {
224+
int iCopy = i;
225225

226+
tasks.Add(Task.Factory.StartNew(() =>
227+
{
228+
foreach (var result in frag[iCopy]) {
229+
result.FindDirectImages();
230+
}
231+
}));
232+
}
226233

227-
best = best.Where(f => f.Direct != null).ToArray();
228234

229-
//best = best
230-
// .AsParallel()
231-
// .Where(r => ImageHelper.IsDirect(r.Url.ToString()))
232-
// .Take(n).ToArray();
235+
Task.WaitAll(tasks.ToArray());
233236

237+
best = best.Where(x => x.Direct != null)
238+
.OrderByDescending(r => r.Similarity)
239+
.ThenByDescending(i => i.PixelResolution)
240+
.ToArray();
234241

235242
return best;
236243
}

SmartImage.Lib/Searching/ImageQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public ImageQuery([NotNull] string value, [CanBeNull] BaseUploadEngine engine =
8080
public static (bool IsUri, bool IsFile) IsUriOrFile(string x)
8181
{
8282
x = x.CleanString();
83-
return (ImageHelper.IsDirect(x), File.Exists(x));
83+
return (ImageHelper.IsDirect(x, DirectImageType.Binary), File.Exists(x));
8484
}
8585

8686

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ public float? MegapixelResolution
117117
/// </summary>
118118
public Dictionary<string, object> OtherMetadata { get; }
119119

120+
public Image? Image { get; set; }
121+
120122
public ImageResult()
121123
{
122124
OtherMetadata = new Dictionary<string, object>();
@@ -190,8 +192,6 @@ public void UpdateFrom(ImageResult result)
190192

191193
}
192194

193-
public Image? Image { get; set; }
194-
195195
public void FindDirectImages()
196196
{
197197
if (Url is not null && Direct == null) {
@@ -202,32 +202,25 @@ public void FindDirectImages()
202202
}
203203
else {
204204
try {
205+
206+
var directImages = ImageHelper.FindDirectImages(Url.ToString(), out var images);
205207

206-
//string[]? directImages = await ImageHelper.FindDirectImagesAsync(Url.ToString());
207-
var directImages = ImageHelper.FindDirectImages(Url.ToString(), out var im);
208+
string? direct = directImages?.FirstOrDefault();
208209

209-
Image = im.First();
210-
string? images = directImages?.FirstOrDefault();
210+
if (direct != null) {
211+
Direct = new Uri(direct);
212+
Image = images.First();
213+
//Debug.WriteLine($"{Url} -> {Direct}");
211214

212-
if (images != null) {
213-
var uri = new Uri(images);
214-
Direct = uri;
215-
Debug.WriteLine($"{Url} -> {Direct}");
216215
}
217216
}
218217
catch (Exception e) {
219218
Debug.WriteLine(e);
220-
221219
}
222220
}
223221

224222

225-
try {
226-
UpdateImageData();
227-
}
228-
catch (Exception) {
229-
// ignored
230-
}
223+
UpdateImageData();
231224

232225
}
233226
}
@@ -239,6 +232,7 @@ public void UpdateImageData()
239232

240233
Width = Image.Width;
241234
Height = Image.Height;
235+
242236
//
243237
// OtherMetadata.Add("Size", MathHelper.ConvertToUnit(rg.Length));
244238
// OtherMetadata.Add("Mime", MediaTypes.ResolveFromData(rg));

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ public static DisplayResolutionType GetDisplayResolution(int w, int h)
8686
/// Determines whether <paramref name="url"/> is a direct image link
8787
/// </summary>
8888
/// <remarks>A direct image link is a link which points to a binary image file</remarks>
89-
public static bool IsDirect(string url, DirectType directType = DirectType.Regex)
89+
public static bool IsDirect(string url, DirectImageType directType = DirectImageType.Regex)
9090
{
9191
return directType switch
9292
{
93-
DirectType.Binary => MediaTypes.IsDirect(url, MimeType.Image),
94-
DirectType.Regex =>
93+
DirectImageType.Binary => MediaTypes.IsDirect(url, MimeType.Image),
94+
DirectImageType.Regex =>
9595
/*
9696
* https://github.com/PactInteractive/image-downloader
9797
*/
@@ -107,7 +107,7 @@ public static bool IsDirect(string url, DirectType directType = DirectType.Regex
107107
public static List<string> FindDirectImages(string url) => FindDirectImages(url, out _);
108108

109109
public static List<string> FindDirectImages(string url, out List<Image> images) =>
110-
FindDirectImages(url, out images, DirectType.Regex, 5, 10, 1, true, null);
110+
FindDirectImages(url, out images, DirectImageType.Regex, 5, 10, 1, true, null);
111111

112112
/// <summary>
113113
/// Scans for direct images within a webpage.
@@ -120,10 +120,12 @@ public static List<string> FindDirectImages(string url, out List<Image> images)
120120
/// <param name="pingTimeSec"></param>
121121
/// <param name="readImage">Whether to read image metadata</param>
122122
/// <param name="imageFilter">Filter criteria for images (applicable iff <paramref name="readImage"/> is <c>true</c>)</param>
123-
public static List<string> FindDirectImages(string url, out List<Image> images, DirectType directType,
123+
public static List<string> FindDirectImages(string url, out List<Image> images, DirectImageType directType,
124124
int count, int fragmentSize, double pingTimeSec,
125125
bool readImage, Predicate<Image> imageFilter)
126126
{
127+
128+
127129
imageFilter ??= (x) => true;
128130

129131
var pingTime = TimeSpan.FromSeconds(pingTimeSec);
@@ -164,7 +166,7 @@ public static List<string> FindDirectImages(string url, out List<Image> images,
164166

165167
var tasks = new List<Task>();
166168

167-
count = Math.Clamp(count, count, flat.Count);
169+
//count = Math.Clamp(count, count, flat.Count);
168170

169171
for (int i = 0; i < fragments.Length; i++) {
170172

@@ -216,7 +218,7 @@ public static List<string> FindDirectImages(string url, out List<Image> images,
216218

217219
if (isValid) {
218220
directImages.Add(currentUrl);
219-
Debug.WriteLine($">>> {currentUrl}");
221+
//Debug.WriteLine($">>> {currentUrl}");
220222

221223
}
222224
}
@@ -237,7 +239,7 @@ public static List<string> FindDirectImages(string url, out List<Image> images,
237239
}
238240
}
239241

240-
public enum DirectType
242+
public enum DirectImageType
241243
{
242244
Binary,
243245
Regex

SmartImage/Core/Info.cs renamed to SmartImage/Core/AppInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace SmartImage.Core
2828
/// <summary>
2929
/// Program runtime information
3030
/// </summary>
31-
public static class Info
31+
public static class AppInfo
3232
{
3333
/// <summary>
3434
/// Name in ASCII art
@@ -69,7 +69,7 @@ public static class Info
6969

7070
public static string AppFolder => Path.GetDirectoryName(ExeLocation);
7171

72-
public static Version Version => typeof(Info).Assembly.GetName().Version!;
72+
public static Version Version => typeof(AppInfo).Assembly.GetName().Version!;
7373

7474
public static bool IsExeInAppFolder => File.Exists(Path.Combine(AppFolder, NAME_EXE));
7575

@@ -100,8 +100,8 @@ public static void Setup()
100100
throw new NotSupportedException();
101101
}
102102

103-
if (!Info.IsAppFolderInPath) {
104-
OSIntegration.HandlePath(IntegrationOption.Add);
103+
if (!AppInfo.IsAppFolderInPath) {
104+
AppIntegration.HandlePath(IntegrationOption.Add);
105105
}
106106

107107
var languages = Windows.System.UserProfile.GlobalizationPreferences.Languages;

SmartImage/Core/OSIntegration.cs renamed to SmartImage/Core/AppIntegration.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal enum IntegrationOption
2727
/// <summary>
2828
/// Program OS integrations
2929
/// </summary>
30-
internal static class OSIntegration
30+
internal static class AppIntegration
3131
{
3232
/*
3333
* HKEY_CLASSES_ROOT is an alias, a merging, of two other locations:
@@ -49,11 +49,11 @@ internal static bool HandleContextMenu(IntegrationOption option)
4949
RegistryKey regMenu = null;
5050
RegistryKey regCmd = null;
5151

52-
string fullPath = Info.ExeLocation;
52+
string fullPath = AppInfo.ExeLocation;
5353

5454
try {
5555
regMenu = Registry.CurrentUser.CreateSubKey(REG_SHELL);
56-
regMenu?.SetValue(String.Empty, Info.NAME);
56+
regMenu?.SetValue(String.Empty, AppInfo.NAME);
5757
regMenu?.SetValue("Icon", $"\"{fullPath}\"");
5858

5959
regCmd = Registry.CurrentUser.CreateSubKey(REG_SHELL_CMD);
@@ -109,9 +109,9 @@ internal static void HandlePath(IntegrationOption option)
109109
case IntegrationOption.Add:
110110
{
111111
string oldValue = FileSystem.EnvironmentPath;
112-
string appFolder = Info.AppFolder;
112+
string appFolder = AppInfo.AppFolder;
113113

114-
if (Info.IsAppFolderInPath) {
114+
if (AppInfo.IsAppFolderInPath) {
115115
return;
116116
}
117117

@@ -120,7 +120,7 @@ internal static void HandlePath(IntegrationOption option)
120120
.Any(p => p == appFolder);
121121

122122
string cd = Environment.CurrentDirectory;
123-
string exe = Path.Combine(cd, Info.NAME_EXE);
123+
string exe = Path.Combine(cd, AppInfo.NAME_EXE);
124124

125125
if (!appFolderInPath) {
126126
string newValue = oldValue + FileSystem.PATH_DELIM + cd;
@@ -130,7 +130,7 @@ internal static void HandlePath(IntegrationOption option)
130130
break;
131131
}
132132
case IntegrationOption.Remove:
133-
FileSystem.RemoveFromPath(Info.AppFolder);
133+
FileSystem.RemoveFromPath(AppInfo.AppFolder);
134134
break;
135135
default:
136136
throw new ArgumentOutOfRangeException(nameof(option), option, null);
@@ -156,7 +156,7 @@ internal static void Uninstall()
156156

157157
// self destruct
158158

159-
string exeFileName = Info.ExeLocation;
159+
string exeFileName = AppInfo.ExeLocation;
160160

161161
const string DEL_BAT_NAME = "SmartImage_Delete.bat";
162162

0 commit comments

Comments
 (0)