Skip to content

Commit 8ce9dc0

Browse files
committed
Updates & improvements
1 parent 47e78ae commit 8ce9dc0

File tree

9 files changed

+276
-15
lines changed

9 files changed

+276
-15
lines changed

SmartImage.Lib/Searching/ImageQuery.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using JetBrains.Annotations;
55
using SimpleCore.Net;
6+
using SimpleCore.Utilities;
67
using SmartImage.Lib.Engines;
78
using SmartImage.Lib.Engines.Impl;
89
using SmartImage.Lib.Upload;
@@ -41,23 +42,24 @@ public sealed class ImageQuery
4142
/// </summary>
4243
public BaseUploadEngine UploadEngine { get; }
4344

44-
4545
public Stream Stream { get; }
4646

47+
48+
4749
public ImageQuery([NotNull] string value, [CanBeNull] BaseUploadEngine engine = null)
4850
{
4951
if (String.IsNullOrWhiteSpace(value)) {
5052
throw new ArgumentNullException(nameof(value));
5153
}
5254

53-
value = value.Trim('\"');
55+
value = value.CleanString();
5456

5557
Value = value;
5658

5759
(IsUri, IsFile) = IsUriOrFile(value);
5860

5961
if (!IsUri && !IsFile) {
60-
throw new ArgumentException($"{value} is neither file nor direct image link");
62+
throw new ArgumentException("Input was neither file nor direct image link", nameof(value));
6163
}
6264

6365

@@ -77,7 +79,7 @@ public ImageQuery([NotNull] string value, [CanBeNull] BaseUploadEngine engine =
7779

7880
public static (bool IsUri, bool IsFile) IsUriOrFile(string x)
7981
{
80-
x = x.Trim('\"');
82+
x = x.CleanString();
8183
return (ImageHelper.IsDirect(x), File.Exists(x));
8284
}
8385

SmartImage.Lib/Upload/CatBoxEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override Uri Upload(string file)
3333
var res = m_client.Execute(req);
3434

3535
if (!res.IsSuccessful) {
36-
throw new SmartImageException();
36+
return null;
3737
}
3838

3939
return new Uri(res.Content);

SmartImage.Lib/Upload/LitterboxEngine.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public override Uri Upload(string file)
2525
{
2626
Verify(file);
2727

28-
2928
var req = new RestRequest(Method.POST);
3029

3130
req.AddParameter("time", "1h");
@@ -36,7 +35,7 @@ public override Uri Upload(string file)
3635
var res = m_client.Execute(req);
3736

3837
if (!res.IsSuccessful) {
39-
throw new SmartImageException($"{res.ErrorMessage} {res.StatusCode} {res.ResponseStatus}");
38+
return null;
4039
}
4140

4241
return new Uri(res.Content);

SmartImage/Core/Info.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using SmartImage.Utilities;
77
using System;
88
using System.Diagnostics;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.IO;
1011
using System.Runtime.CompilerServices;
1112
using System.Text;
@@ -79,7 +80,7 @@ public static string AppFolder
7980
/// <summary>
8081
/// <c>Null</c> if executable is not in path.
8182
/// </summary>
82-
[CanBeNull]
83+
[MaybeNull]
8384
public static string ExeLocation => FileSystem.FindExecutableLocation(NAME_EXE)!;
8485

8586

SmartImage/Core/MainDialog.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
using SmartImage.Lib.Searching;
55
using System;
66
using System.Drawing;
7+
using System.IO;
78
using System.Linq;
89
using Novus.Utilities;
910
using SmartImage.Lib;
11+
using SmartImage.Utilities;
12+
// ReSharper disable PossibleNullReferenceException
1013

1114
namespace SmartImage.Core
1215
{
@@ -51,7 +54,7 @@ public static class MainDialog
5154
{
5255
(bool url, bool file) = ImageQuery.IsUriOrFile(x);
5356
return !(url || file);
54-
});
57+
},"Input must be file or direct image link");
5558

5659
Program.Config.Query = query;
5760
return true;
@@ -138,12 +141,21 @@ public static class MainDialog
138141
//Console.Clear();
139142

140143
Console.WriteLine($"Author: {Info.Author}");
141-
Console.WriteLine($"Version: {Info.Version}");
142144

143-
Console.WriteLine($"Executable location: {Info.ExeLocation}");
145+
Console.WriteLine($"Current version: {Info.Version} ({UpdateInfo.GetUpdateInfo().Status})");
146+
Console.WriteLine($"Latest version: {ReleaseInfo.GetLatestRelease()}");
147+
148+
Console.WriteLine();
149+
150+
var di = new DirectoryInfo(Info.ExeLocation);
151+
152+
Console.WriteLine($"Executable location: {di.Parent.Name}");
153+
144154
Console.WriteLine($"In path: {Info.IsAppFolderInPath}");
145155
Console.WriteLine($"Context menu added: {OSIntegration.IsContextMenuAdded}");
146156

157+
158+
Console.WriteLine();
147159
Console.WriteLine(Strings.Separator);
148160

149161
var dependencies = ReflectionHelper.DumpDependencies();
@@ -152,6 +164,17 @@ public static class MainDialog
152164
Console.WriteLine($"{name.Name} ({name.Version})");
153165
}
154166

167+
NConsole.WaitForInput();
168+
return null;
169+
}
170+
},
171+
new()
172+
{
173+
Name = "Update",
174+
Function = () =>
175+
{
176+
UpdateInfo.AutoUpdate();
177+
155178
NConsole.WaitForInput();
156179
return null;
157180
}

SmartImage/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using SmartImage.Lib.Searching;
2323
using SmartImage.Lib.Utilities;
2424
using SmartImage.Utilities;
25+
2526
// ReSharper disable ConvertSwitchStatementToSwitchExpression
2627

2728
// ReSharper disable UnusedParameter.Local
@@ -109,7 +110,7 @@ private static async Task Main(string[] args)
109110
cts.Cancel();
110111
cts.Dispose();
111112
};
112-
113+
113114
NConsoleProgress.Queue(cts);
114115

115116
// Show results
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Globalization;
3+
using Newtonsoft.Json.Linq;
4+
using RestSharp;
5+
using SimpleCore.Diagnostics;
6+
7+
// ReSharper disable PossibleNullReferenceException
8+
9+
namespace SmartImage.Utilities
10+
{
11+
public readonly struct ReleaseInfo
12+
{
13+
private const string GITHUB_API_ENDPOINT = "https://api.github.com/";
14+
15+
private const string GITHUB_API_SMARTIMAGE = "repos/Decimation/SmartImage/releases";
16+
17+
public ReleaseInfo(string tagName, string htmlUrl, string publishedAt, string asset)
18+
{
19+
TagName = tagName;
20+
21+
HtmlUrl = htmlUrl;
22+
23+
24+
var utc = DateTime.Parse(publishedAt, null, DateTimeStyles.AdjustToUniversal);
25+
26+
PublishedAt = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
27+
28+
29+
// Parse version
30+
31+
string[] versionStrSplit = tagName
32+
.Replace("v", String.Empty)
33+
.Split(".");
34+
35+
36+
int major = Int32.Parse(versionStrSplit[0]);
37+
int minor = Int32.Parse(versionStrSplit[1]);
38+
39+
int build = 0, rev = 0;
40+
41+
if (versionStrSplit.Length >= 3) {
42+
build = Int32.Parse(versionStrSplit[2]);
43+
}
44+
45+
if (versionStrSplit.Length >= 4) {
46+
rev = Int32.Parse(versionStrSplit[3]);
47+
}
48+
49+
50+
Version = new Version(major, minor, build, rev);
51+
52+
AssetUrl = asset;
53+
}
54+
55+
public string TagName { get; }
56+
57+
public string HtmlUrl { get; }
58+
59+
public DateTime PublishedAt { get; }
60+
61+
public Version Version { get; }
62+
63+
public string AssetUrl { get; }
64+
65+
public static ReleaseInfo GetLatestRelease()
66+
{
67+
var rc = new RestClient(GITHUB_API_ENDPOINT);
68+
var re = new RestRequest(GITHUB_API_SMARTIMAGE);
69+
var rs = rc.Execute(re);
70+
var ja = JArray.Parse(rs.Content);
71+
72+
var first = ja[0];
73+
74+
var tagName = first["tag_name"];
75+
var url = first["html_url"];
76+
var publish = first["published_at"];
77+
78+
79+
var assets = first["assets"];
80+
var dlUrl = assets[0]["browser_download_url"];
81+
82+
var r = new ReleaseInfo(tagName.ToString(), url.ToString(), publish.ToString(), dlUrl.ToString());
83+
84+
return r;
85+
}
86+
87+
public override string ToString()
88+
{
89+
return $"{TagName} ({Version}) @ {PublishedAt}";
90+
}
91+
}
92+
}

SmartImage/Utilities/UpdateInfo.cs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.IO;
4+
using System.Net;
5+
using Novus.Win32;
6+
using SimpleCore.Cli;
7+
using SmartImage.Core;
8+
9+
namespace SmartImage.Utilities
10+
{
11+
public enum VersionStatus
12+
{
13+
UpToDate,
14+
Available,
15+
Preview,
16+
}
17+
18+
public readonly struct UpdateInfo
19+
{
20+
public Version Current { get; }
21+
22+
public ReleaseInfo Latest { get; }
23+
24+
public VersionStatus Status { get; }
25+
26+
private UpdateInfo(Version current, ReleaseInfo info, VersionStatus status)
27+
{
28+
Current = current;
29+
Latest = info;
30+
Status = status;
31+
}
32+
33+
[DoesNotReturn]
34+
public static void Update(UpdateInfo ui)
35+
{
36+
const string NEW_EXE = "SmartImage-new.exe";
37+
const string UPDATE_BAT = "SmartImage_Updater.bat";
38+
39+
40+
var destNew = Path.Combine(Info.AppFolder, NEW_EXE);
41+
var wc = new WebClient();
42+
43+
Console.WriteLine("Downloading...");
44+
45+
wc.DownloadFile(ui.Latest.AssetUrl, destNew);
46+
47+
48+
string exeFileName = Info.ExeLocation;
49+
50+
//const string WAIT_4_SEC = "ping 127.0.0.1 > nul";
51+
52+
const string WAIT_4_SEC = "timeout /t 4 /nobreak >nul";
53+
54+
string[] commands =
55+
{
56+
"@echo off",
57+
58+
/* Wait approximately 4 seconds (so that the process is already terminated) */
59+
WAIT_4_SEC,
60+
61+
/* Delete executable */
62+
"echo y | del /F " + exeFileName,
63+
64+
/* Rename */
65+
$"move /Y \"{destNew}\" \"{exeFileName}\" > NUL",
66+
67+
/* Wait */
68+
WAIT_4_SEC,
69+
WAIT_4_SEC,
70+
71+
/* Open the new SmartImage version */
72+
$"start /d \"{Info.AppFolder}\" {Info.NAME_EXE}",
73+
74+
/* Delete this batch file */
75+
"echo y | del " + UPDATE_BAT,
76+
};
77+
78+
79+
// Runs in background
80+
Command.RunBatch(commands, false, UPDATE_BAT);
81+
}
82+
83+
84+
// NOTE: Does not return if a new update is found and the user updates
85+
public static void AutoUpdate()
86+
{
87+
var ui = GetUpdateInfo();
88+
89+
if (ui.Status == VersionStatus.Available) {
90+
Console.WriteLine($"Update found: {ui.Latest} ");
91+
92+
if (NConsole.ReadConfirmation("Update?")) {
93+
try {
94+
Update(ui);
95+
}
96+
catch (Exception e) {
97+
Console.WriteLine(e);
98+
return;
99+
}
100+
}
101+
}
102+
103+
Console.WriteLine($"Up to date: {ui.Current} [{ui.Latest}]");
104+
NConsole.WaitForSecond();
105+
}
106+
107+
public static UpdateInfo GetUpdateInfo()
108+
{
109+
var currentVersion = Info.Version;
110+
111+
var release = ReleaseInfo.GetLatestRelease();
112+
113+
int cmp = currentVersion.CompareTo(release.Version);
114+
115+
var status = cmp switch
116+
{
117+
< 0 => VersionStatus.Available,
118+
0 => VersionStatus.UpToDate,
119+
_ => VersionStatus.Preview
120+
};
121+
122+
return new UpdateInfo(currentVersion, release, status);
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)