Skip to content

Commit 36b83a9

Browse files
committed
rework
1 parent df61b3f commit 36b83a9

File tree

14 files changed

+121
-87
lines changed

14 files changed

+121
-87
lines changed

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Diagnostics;
55
using System.Threading.Tasks;
6+
using static SimpleCore.Diagnostics.LogCategories;
67

78
namespace SmartImage.Lib.Engines
89
{
@@ -42,14 +43,14 @@ public async Task<SearchResult> GetResultAsync(ImageQuery query)
4243

4344
var task = Task.Run(delegate
4445
{
45-
Debug.WriteLine($"[info] {Name}: getting result async");
46+
Debug.WriteLine($"{Name}: getting result async",C_INFO);
4647
var sw = Stopwatch.StartNew();
4748

4849
var res = GetResult(query);
4950

5051
sw.Stop();
5152

52-
Debug.WriteLine($"[success] {Name}: result done {sw.Elapsed.TotalSeconds}");
53+
Debug.WriteLine($"{Name}: result done {sw.Elapsed.TotalSeconds}",C_SUCCESS);
5354

5455
return res;
5556
});
@@ -70,7 +71,7 @@ public Uri GetRawResultUrl(ImageQuery query)
7071
bool ok = Network.IsUriAlive(uri);
7172

7273
if (!ok) {
73-
Debug.WriteLine($"[error] {uri.Host} is unavailable");
74+
Debug.WriteLine($"{uri.Host} is unavailable",C_WARN);
7475
return null;
7576
}
7677

SmartImage.Lib/Engines/IUploadEngine.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@ public interface IUploadEngine
1515

1616
public Uri Upload(string file);
1717

18-
public bool FileSizeValid(string file)
18+
19+
protected static void Verify(IUploadEngine e, string file)
20+
{
21+
//todo
22+
23+
if (String.IsNullOrWhiteSpace(file)) {
24+
throw new ArgumentNullException(nameof(file));
25+
}
26+
27+
if (!e.IsFileSizeValid(file)) {
28+
throw new ArgumentException($"File {file} is too large (max {e.MaxSize} MB) for {e.Name}");
29+
}
30+
}
31+
32+
protected bool IsFileSizeValid(string file)
1933
{
2034
double fileSizeMegabytes =
2135
MathHelper.ConvertToUnit(FileSystem.GetFileSize(file), MetricUnit.Mega);

SmartImage.Lib/Engines/Impl/CatBoxEngine.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ public CatBoxEngine()
2929

3030
public Uri Upload(string file)
3131
{
32-
if (String.IsNullOrWhiteSpace(file)) {
33-
throw new ArgumentNullException(nameof(file));
34-
}
35-
36-
if (!((IUploadEngine) this).FileSizeValid(file)) {
37-
throw new ArgumentException($"File {file} is too large (max {MaxSize} MB) for {Name}"); //todo
38-
}
32+
IUploadEngine.Verify(this, file);
3933

4034

4135
var req = new RestRequest(Method.POST);
@@ -47,7 +41,7 @@ public Uri Upload(string file)
4741
var res = m_client.Execute(req);
4842

4943
if (!res.IsSuccessful) {
50-
throw new SmartImageException(); //todo
44+
throw new SmartImageException();
5145
}
5246

5347
return new Uri(res.Content);

SmartImage.Lib/Engines/Impl/ImgOpsEngine.cs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,27 @@ namespace SmartImage.Lib.Engines.Impl
1010
{
1111
public sealed class ImgOpsEngine : BaseSearchEngine, IUploadEngine
1212
{
13+
public override SearchEngineOptions Engine => SearchEngineOptions.ImgOps;
14+
15+
public int MaxSize => 5;
16+
1317
public ImgOpsEngine() : base("http://imgops.com/") { }
1418

15-
public override SearchEngineOptions Engine => SearchEngineOptions.ImgOps;
19+
public Uri Upload(string img)
20+
{
21+
IUploadEngine.Verify(this, img);
22+
23+
Debug.WriteLine($"Uploading {img}");
1624

1725

26+
var imgOpsUrl = UploadInternal(img);
27+
28+
string? link = imgOpsUrl.ToString();
29+
link = "http://" + link.SubstringAfter(BaseUrl);
30+
31+
return new Uri(link);
32+
}
33+
1834
private Uri UploadInternal(string path)
1935
{
2036
//https://github.com/dogancelik/imgops
@@ -33,29 +49,5 @@ private Uri UploadInternal(string path)
3349

3450
return re.ResponseUri;
3551
}
36-
37-
38-
public int MaxSize => 5;
39-
40-
public Uri Upload(string img)
41-
{
42-
if (string.IsNullOrWhiteSpace(img)) {
43-
throw new ArgumentNullException(nameof(img));
44-
}
45-
46-
if (!((IUploadEngine) this).FileSizeValid(img)) {
47-
throw new ArgumentException($"File {img} is too large (max {MaxSize} MB) for {Name}"); //todo
48-
}
49-
50-
Debug.WriteLine($"Uploading {img}");
51-
52-
53-
var imgOpsUrl = UploadInternal(img);
54-
55-
string? link = imgOpsUrl.ToString();
56-
link = "http://" + link.SubstringAfter(BaseUrl);
57-
58-
return new Uri(link);
59-
}
6052
}
6153
}

SmartImage.Lib/Engines/Impl/LitterboxEngine.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using RestSharp;
77
using SmartImage.Lib.Utilities;
8+
// ReSharper disable StringLiteralTypo
89

910
// ReSharper disable UnusedMember.Global
1011

@@ -25,13 +26,7 @@ public LitterboxEngine()
2526

2627
public Uri Upload(string file)
2728
{
28-
if (String.IsNullOrWhiteSpace(file)) {
29-
throw new ArgumentNullException(nameof(file));
30-
}
31-
32-
if (!((IUploadEngine) this).FileSizeValid(file)) {
33-
throw new ArgumentException($"File {file} is too large (max {MaxSize} MB) for {Name}"); //todo
34-
}
29+
IUploadEngine.Verify(this,file);
3530

3631

3732
var req = new RestRequest(Method.POST);

SmartImage.Lib/Engines/Impl/SauceNaoEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Json;
1212
using System.Linq;
1313
using System.Net;
14+
using static SimpleCore.Diagnostics.LogCategories;
1415
using JsonArray = System.Json.JsonArray;
1516
using JsonObject = System.Json.JsonObject;
1617

@@ -104,7 +105,7 @@ private static IEnumerable<SauceNaoDataResult> ParseResults(string url)
104105
*/
105106

106107
if (html.Contains("Search Limit Exceeded")) {
107-
Trace.WriteLine("SauceNao on cooldown!");
108+
Trace.WriteLine("SauceNao on cooldown!",C_WARN);
108109
return null;
109110
}
110111

SmartImage.Lib/SearchClient.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Linq;
1010
using System.Threading.Tasks;
1111
using SimpleCore.Net;
12+
using static SimpleCore.Diagnostics.LogCategories;
1213

1314
// ReSharper disable UnusedMember.Global
1415

@@ -57,7 +58,7 @@ public ImageResult[] FindBestResults(int n)
5758
return x;
5859
});
5960

60-
Debug.WriteLine(best.Count());
61+
//==================================================================//
6162

6263

6364
best = best
@@ -137,7 +138,7 @@ public async Task RunSearchAsync()
137138
IsComplete = !tasks.Any();
138139
}
139140

140-
Trace.WriteLine($"[success] {nameof(SearchClient)}: Search complete");
141+
Trace.WriteLine($"{nameof(SearchClient)}: Search complete",C_SUCCESS);
141142
SearchCompleted?.Invoke(null, null);
142143

143144
return;

SmartImage.Lib/Searching/ImageQuery.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using SmartImage.Lib.Engines;
77
using SmartImage.Lib.Engines.Impl;
88
using SmartImage.Lib.Utilities;
9+
using static SimpleCore.Diagnostics.LogCategories;
910

1011
namespace SmartImage.Lib.Searching
1112
{
@@ -65,7 +66,7 @@ public ImageQuery([NotNull] string value, [CanBeNull] IUploadEngine engine = nul
6566

6667
Stream = IsFile ? File.OpenRead(value) : Network.GetStream(value);
6768

68-
Trace.WriteLine($"[success] {nameof(ImageQuery)}: {Uri}");
69+
Trace.WriteLine($"{nameof(ImageQuery)}: {Uri}", C_SUCCESS);
6970
}
7071

7172

SmartImage/Core/Info.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public static class Info
2828
/// Name in ASCII art
2929
/// </summary>
3030
public const string NAME_BANNER =
31-
" ____ _ ___\n" +
32-
" / ___| _ __ ___ __ _ _ __| |_|_ _|_ __ ___ __ _ __ _ ___\n" +
33-
@" \___ \| '_ ` _ \ / _` | '__| __|| || '_ ` _ \ / _` |/ _` |/ _ \" + "\n" +
31+
" ____ _ ___\n" +
32+
" / ___| _ __ ___ __ _ _ __| |_|_ _|_ __ ___ __ _ __ _ ___\n" +
33+
@" \___ \| '_ ` _ \ / _` | '__| __|| || '_ ` _ \ / _` |/ _` |/ _ \" + "\n" +
3434
" ___) | | | | | | (_| | | | |_ | || | | | | | (_| | (_| | __/\n" +
35-
@" |____/|_| |_| |_|\__,_|_| \__|___|_| |_| |_|\__,_|\__, |\___|" + "\n" +
35+
@" |____/|_| |_| |_|\__,_|_| \__|___|_| |_| |_|\__,_|\__, |\___|" + "\n" +
3636
" |___/\n";
3737

3838

@@ -47,7 +47,7 @@ public static class Info
4747
public const string NAME_EXE = "SmartImage.exe";
4848

4949
/// <summary>
50-
/// Config file name (<see cref="UserSearchConfig"/>)
50+
/// Config file name
5151
/// </summary>
5252
public const string NAME_CFG = "SmartImage.cfg";
5353

@@ -59,8 +59,6 @@ public static class Info
5959

6060
public const string Issue = "https://github.com/Decimation/SmartImage/issues/new";
6161

62-
63-
6462

6563
public static string AppFolder
6664
{
@@ -91,13 +89,11 @@ public static string AppFolder
9189
[ModuleInitializer]
9290
public static void Setup()
9391
{
94-
if (!OperatingSystem.IsWindows())
95-
{
92+
if (!OperatingSystem.IsWindows()) {
9693
throw new NotSupportedException();
9794
}
95+
9896
Integration.Setup();
9997
}
100-
101-
10298
}
10399
}

SmartImage/Core/Integration.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace SmartImage.Core
1414
{
15+
/*
16+
1517
/// <summary>
1618
/// Legacy integration features
1719
/// </summary>
@@ -101,7 +103,7 @@ internal static bool HandleContextMenu(IntegrationOption option)
101103
}
102104
catch (Exception e)
103105
{
104-
Trace.WriteLine("Context menu error: {0}", e.Message);
106+
Trace.WriteLine($"Context menu error: {e.Message}");
105107
NConsole.WaitForSecond();
106108
return false;
107109
}
@@ -142,7 +144,8 @@ internal static bool LegacyCleanup()
142144
143145
return true;
144146
}
145-
}
147+
}*/
148+
146149
// todo: move context menu integration to Novus for use in other projects?
147150

148151
internal enum IntegrationOption
@@ -156,15 +159,13 @@ internal enum IntegrationOption
156159
/// </summary>
157160
internal static class Integration
158161
{
159-
160162
/*
161163
* HKEY_CLASSES_ROOT is an alias, a merging, of two other locations:
162164
* HKEY_CURRENT_USER\Software\Classes
163165
* HKEY_LOCAL_MACHINE\Software\Classes
164166
*/
165167

166168

167-
168169
/// <returns><c>true</c> if operation succeeded; <c>false</c> otherwise</returns>
169170
internal static bool HandleContextMenu(IntegrationOption option)
170171
{
@@ -189,7 +190,7 @@ internal static bool HandleContextMenu(IntegrationOption option)
189190
regCmd?.SetValue(String.Empty, $"\"{fullPath}\" \"%1\"");
190191
}
191192
catch (Exception ex) {
192-
Trace.WriteLine("{0}", ex.Message);
193+
Trace.WriteLine($"{ex.Message}");
193194
NConsole.WaitForInput();
194195
return false;
195196
}
@@ -217,7 +218,7 @@ internal static bool HandleContextMenu(IntegrationOption option)
217218
}
218219
}
219220
catch (Exception ex) {
220-
Trace.WriteLine("{0}", ex.Message);
221+
Trace.WriteLine($"{ex.Message}");
221222
NConsole.WaitForInput();
222223
return false;
223224
}

0 commit comments

Comments
 (0)