Skip to content

Commit 4f4a107

Browse files
committed
Regex image link scanning
1 parent 42ff19e commit 4f4a107

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,26 @@ public static DisplayResolutionType GetDisplayResolution(int w, int h)
9191
/// Determines whether <paramref name="url"/> is a direct image link
9292
/// </summary>
9393
/// <remarks>A direct image link is a link which points to a binary image file</remarks>
94-
public static bool IsDirect(string url) => MediaTypes.IsDirect(url, MimeType.Image);
94+
public static bool IsDirect(string url)
95+
{
96+
return MediaTypes.IsDirect(url, MimeType.Image);
97+
}
98+
99+
100+
public static bool IsDirect2(string url)
101+
{
102+
// todo
103+
104+
105+
/*
106+
* https://github.com/PactInteractive/image-downloader
107+
*/
108+
109+
return Regex.IsMatch(
110+
url,
111+
@"(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*\.(?:bmp|gif|ico|jfif|jpe?g|png|svg|tiff?|webp))(?:\?([^#]*))?(?:#(.*))?",
112+
RegexOptions.IgnoreCase);
113+
}
95114

96115
/// <summary>
97116
/// Scans for direct image links in <paramref name="url"/>
@@ -142,7 +161,7 @@ public static string[] FindDirectImages(string url)
142161
// todo: is running PLINQ within a task thread-safe?
143162

144163
results = rg.AsParallel()
145-
.Where(e => Network.IsUri(e, out var u) && IsDirect(u == null ? e : u.ToString()))
164+
.Where(e => Network.IsUri(e, out var u) && IsDirect2(u == null ? e : u.ToString()))
146165
.ToArray();
147166

148167
//Debug.WriteLine($"{nameof(FindDirectImages)}: {rg.Count} -> {results.Length}", C_DEBUG);

SmartImage/Core/DialogBridge.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal static NConsoleOption CreateOption(SearchResult result)
2727
{
2828
var option = new NConsoleOption
2929
{
30-
Function = CreateFunction(result.PrimaryResult),
30+
Function = CreateMainFunction(result.PrimaryResult),
3131
AltFunction = () =>
3232
{
3333
if (result.OtherResults.Any()) {
@@ -43,20 +43,8 @@ internal static NConsoleOption CreateOption(SearchResult result)
4343

4444
return null;
4545
},
46+
ComboFunction = CreateComboFunction(result.PrimaryResult),
4647

47-
ComboFunction = () =>
48-
{
49-
var direct = result.PrimaryResult.Direct;
50-
51-
var ok = direct != null;
52-
53-
if (ok) {
54-
var p = WebUtilities.Download(direct!.ToString());
55-
FileSystem.ExploreFile(p);
56-
}
57-
58-
return null;
59-
},
6048
Name = result.Engine.Name.AddColor(EngineNameColorMap[result.Engine.EngineOption]),
6149
Data = result.ToString(false)
6250
};
@@ -67,9 +55,9 @@ internal static NConsoleOption CreateOption(SearchResult result)
6755

6856
NConsoleProgress.Queue(cts);
6957

70-
7158
result.OtherResults.AsParallel().ForAll(x => x.FindDirectImages());
7259

60+
7361
result.PrimaryResult.UpdateFrom(result.OtherResults.First());
7462

7563
cts.Cancel();
@@ -87,16 +75,17 @@ private static NConsoleOption CreateOption(ImageResult r)
8775
{
8876
var option = new NConsoleOption
8977
{
90-
Function = CreateFunction(r),
91-
Name = $"Other result\n\b",
78+
Function = CreateMainFunction(r),
79+
ComboFunction = CreateComboFunction(r),
80+
Name = $"Other result\n\b",
9281
//Data = r.ToString().Replace("\n", "\n\t"),
9382
Data = r.ToString(true)
9483
};
9584

9685
return option;
9786
}
9887

99-
private static NConsoleFunction CreateFunction(ImageResult? primaryResult)
88+
private static NConsoleFunction CreateMainFunction(ImageResult? primaryResult)
10089
{
10190
return () =>
10291
{
@@ -112,6 +101,23 @@ private static NConsoleFunction CreateFunction(ImageResult? primaryResult)
112101
};
113102
}
114103

104+
private static NConsoleFunction CreateComboFunction(ImageResult result)
105+
{
106+
return () =>
107+
{
108+
var direct = result.Direct;
109+
110+
var ok = direct != null;
111+
112+
if (ok) {
113+
var p = WebUtilities.Download(direct!.ToString());
114+
FileSystem.ExploreFile(p);
115+
}
116+
117+
return null;
118+
};
119+
}
120+
115121
private static readonly Dictionary<SearchEngineOptions, Color> EngineNameColorMap = new()
116122
{
117123
{SearchEngineOptions.Iqdb, Color.SandyBrown},

Test/Program.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,22 @@ public static async Task Main(string[] args)
9191
//Console.WriteLine($">> {r2}");
9292
//Console.WriteLine($"{sw.Elapsed.TotalSeconds}");
9393

94-
9594

9695

97-
//var sw = Stopwatch.StartNew();
9896

99-
//var v = ImageHelper.FindDirectImages("https://iqdb.org/?url=https://litter.catbox.moe/151mk9.jpg");
97+
var sw = Stopwatch.StartNew();
10098

101-
//foreach (string s in v) {
102-
// Console.WriteLine(s);
103-
//}
104-
////
105-
//sw.Stop();
106-
//Debug.WriteLine($"{sw.Elapsed.TotalSeconds}");
99+
var v = ImageHelper.FindDirectImages("https://iqdb.org/?url=https://litter.catbox.moe/hm0muq.jpg");
100+
101+
foreach (string s in v)
102+
{
103+
Console.WriteLine(s);
104+
}
105+
106+
107+
//
108+
sw.Stop();
109+
Debug.WriteLine($"{sw.Elapsed.TotalSeconds}");
107110

108111

109112

0 commit comments

Comments
 (0)