Skip to content

Commit c9b3e9f

Browse files
committed
Work on #39; bug fixes; add additional upload engine
1 parent 10a4a03 commit c9b3e9f

File tree

8 files changed

+109
-136
lines changed

8 files changed

+109
-136
lines changed

SmartImage 3/Mode/Shell/Assets/UI.cs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal static void SetLabelStatus(Label lbl, bool? b)
6363
}
6464
}
6565

66-
public static void FromEnum2<TEnum>(this ListView lv, TEnum e) where TEnum : struct, Enum
66+
public static void FromEnum<TEnum>(this ListView lv, TEnum e) where TEnum : struct, Enum
6767
{
6868
var list = lv.Source.ToList<TEnum>().ToArray();
6969

@@ -83,7 +83,7 @@ public static void FromEnum2<TEnum>(this ListView lv, TEnum e) where TEnum : str
8383
}
8484
}
8585

86-
public static TEnum GetEnum2<TEnum>(this IListDataSource lv, TEnum t = default) where TEnum : struct, Enum
86+
public static TEnum GetEnum<TEnum>(this IListDataSource lv, TEnum t = default) where TEnum : struct, Enum
8787
{
8888
var m = lv.GetItems<TEnum>();
8989

@@ -111,23 +111,8 @@ public static TEnum GetEnum2<TEnum>(this IListDataSource lv, TEnum t = default)
111111

112112
return t2;
113113
}
114-
115-
static IList<T> GetItems<T>(IListDataSource src)
116-
{
117-
var ls = new List<T>();
118-
var cpy = src.ToList();
119-
120-
for (int i = 0; i < src.Length; i++) {
121-
if (src.IsMarked(i)) {
122-
// ls.RemoveAt(i);
123-
ls.Add((T) (object) cpy[i]);
124-
}
125-
}
126-
127-
return ls;
128-
}
129-
130-
internal static void OnEngineSelected_(ListView lv, ref SearchEngineOptions e)
114+
115+
internal static void OnEngineSelected(ListView lv, ref SearchEngineOptions e)
131116
{
132117
var l = lv.Source.ToList<SearchEngineOptions>().ToArray();
133118

@@ -147,12 +132,12 @@ internal static void OnEngineSelected_(ListView lv, ref SearchEngineOptions e)
147132
}
148133

149134
ret:
150-
lv.FromEnum2(e);
135+
lv.FromEnum(e);
151136

152137
Debug.WriteLine($"{e}");
153138
}
154139

155-
internal static void OnEngineSelected(ListViewItemEventArgs args, ref SearchEngineOptions e, ListView lv)
140+
/*internal static void OnEngineSelected(ListViewItemEventArgs args, ref SearchEngineOptions e, ListView lv)
156141
{
157142
var val = (SearchEngineOptions) args.Value;
158143
@@ -184,5 +169,5 @@ internal static void OnEngineSelected(ListViewItemEventArgs args, ref SearchEngi
184169
185170
lv.SetNeedsDisplay();
186171
Debug.WriteLine($"{val} {args.Item} -> {e} {isMarked}", nameof(OnEngineSelected));
187-
}
172+
}*/
188173
}

SmartImage 3/Mode/Shell/ShellMode.Dialog.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void ReloadDialog()
145145
{
146146
SearchEngineOptions e = Config.SearchEngines;
147147
// UI.OnEngineSelected(args1, ref e, lvSearchEngines);
148-
UI.OnEngineSelected_(lvSearchEngines, ref e);
148+
UI.OnEngineSelected(lvSearchEngines, ref e);
149149
Debug.WriteLine($"Setting {e}");
150150
Config.SearchEngines = e;
151151
ReloadDialog();
@@ -155,7 +155,7 @@ void ReloadDialog()
155155
{
156156
SearchEngineOptions e = Config.PriorityEngines;
157157
// UI.OnEngineSelected(args1, ref e, lvPriorityEngines);
158-
UI.OnEngineSelected_(lvPriorityEngines, ref e);
158+
UI.OnEngineSelected(lvPriorityEngines, ref e);
159159
Debug.WriteLine($"Setting {e}");
160160
Config.PriorityEngines = e;
161161
ReloadDialog();

SmartImage 3/Mode/Shell/ShellMode.Handlers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ private async void Input_TextChanging(TextChangingEventArgs tc)
5151
/// <summary>
5252
/// <see cref="Btn_Restart"/>
5353
/// </summary>
54-
private void Restart_Clicked()
54+
private void Restart_Clicked(bool force = false)
5555
{
56-
if (!Client.IsComplete) {
56+
if (!Client.IsComplete && !force) {
5757
return;
5858
}
5959

SmartImage 3/Mode/Shell/ShellMode.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using SmartImage.Mode.Shell.Assets;
2020
using SmartImage.Utilities;
2121
using Terminal.Gui;
22+
using Clipboard = Novus.Win32.Clipboard;
2223
using Window = Terminal.Gui.Window;
2324

2425
// ReSharper disable IdentifierTypo
@@ -305,7 +306,7 @@ public ShellMode(string[] args)
305306
m_cbCallbackTok = Application.MainLoop.AddTimeout(TimeoutTimeSpan, ClipboardCallback);
306307

307308
m_clipboard = new List<ustring>();
308-
309+
309310
Mb_Menu.Menus = new MenuBarItem[]
310311
{
311312
new("_About", null, AboutDialog),
@@ -364,7 +365,7 @@ public ShellMode(string[] args)
364365

365366
Tv_Results.CellActivated += Result_CellActivated;
366367
Btn_Run.Clicked += Run_Clicked;
367-
Btn_Restart.Clicked += Restart_Clicked;
368+
Btn_Restart.Clicked += () => Restart_Clicked(false);
368369
Btn_Clear.Clicked += Clear_Clicked;
369370
Btn_Config.Clicked += ConfigDialog;
370371
Btn_Cancel.Clicked += Cancel_Clicked;
@@ -377,32 +378,18 @@ public ShellMode(string[] args)
377378
HttpUtilities.TryOpenUrl(Query.Upload);
378379
};
379380

380-
Btn_Delete.Clicked += () =>
381-
{
382-
var file = Tf_Input.Text.ToString();
383-
384-
if (!string.IsNullOrWhiteSpace(file)) {
385-
Query.Dispose();
386-
Debug.WriteLine($"{IsQueryReady()}");
387-
FileSystem.DeleteFile(file, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
388-
Debug.WriteLine($"deleted {file}");
389-
Clear();
390-
}
381+
Btn_Delete.Clicked += On_Delete;
391382

392-
};
393-
394383
Cb_Queue.Toggled += b =>
395384
{
396385
Btn_Queue.Enabled = !b;
397386
};
398387

399388
Btn_Queue.Clicked += () =>
400389
{
401-
if (IsQueryReady()) {
402-
403-
}
390+
if (IsQueryReady()) { }
404391
};
405-
392+
406393
Btn_Queue.Enabled = false;
407394

408395
Win.Add(Lbl_Input, Tf_Input, Btn_Run, Lbl_InputOk,
@@ -425,6 +412,23 @@ public ShellMode(string[] args)
425412
}
426413
}
427414

415+
private void On_Delete()
416+
{
417+
Clipboard.Close();
418+
Restart_Clicked(true);
419+
420+
var file = Tf_Input.Text.ToString();
421+
422+
if (!string.IsNullOrWhiteSpace(file)) {
423+
Query.Dispose();
424+
Debug.WriteLine($"{IsQueryReady()}");
425+
FileSystem.DeleteFile(file, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
426+
Debug.WriteLine($"deleted {file}");
427+
Clear();
428+
}
429+
430+
}
431+
428432
public Task<object?> RunAsync(object? sender = null)
429433
{
430434
Application.Run();
@@ -454,7 +458,8 @@ private void OnResult(object o, SearchResult result)
454458

455459
Application.MainLoop.Invoke(() =>
456460
{
457-
Dt_Results.Rows.Add($"{result.Engine.Name} (Raw)", result.RawUrl, 0, 0, null, $"{result.Status}",
461+
Dt_Results.Rows.Add($"{result.Engine.Name} (Raw)",
462+
result.RawUrl, 0, 0, null, $"{result.Status}",
458463
null, null, null, null, null, null);
459464

460465
for (int i = 0; i < result.Results.Count; i++) {
@@ -618,6 +623,7 @@ private async Task<bool> SetQuery(ustring text)
618623
Lbl_QueryUpload.Text = ustring.Empty;
619624
Pbr_Status.Fraction = 0;
620625
Lbl_Status2.Text = ustring.Empty;
626+
Btn_Delete.Enabled = false;
621627

622628
return false;
623629
}
@@ -637,7 +643,8 @@ private async Task<bool> SetQuery(ustring text)
637643
Pbr_Status.Fraction = 0;
638644
// Btn_Delete.Enabled = true;
639645

640-
Tf_Input.ReadOnly = true;
646+
Tf_Input.ReadOnly = true;
647+
Btn_Delete.Enabled = true;
641648

642649
return true;
643650
}
Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,81 @@
1-
using Novus.OS;
1+
using Flurl.Http;
2+
using Novus.OS;
23
using Novus.Utilities;
34

45
namespace SmartImage.Lib.Engines.Impl.Upload;
56

67
public abstract class BaseUploadEngine
78
{
8-
/// <summary>
9-
/// Max file size, in bytes
10-
/// </summary>
11-
public abstract int MaxSize { get; }
9+
/// <summary>
10+
/// Max file size, in bytes
11+
/// </summary>
12+
public abstract int MaxSize { get; }
1213

13-
public abstract string Name { get; }
14+
public abstract string Name { get; }
1415

15-
protected string EndpointUrl { get; }
16+
protected string EndpointUrl { get; }
1617

17-
protected BaseUploadEngine(string s)
18-
{
19-
EndpointUrl = s;
20-
}
18+
protected BaseUploadEngine(string s)
19+
{
20+
EndpointUrl = s;
21+
}
2122

22-
public static BaseUploadEngine Default { get; } = new CatboxEngine();
23+
public static BaseUploadEngine Default { get; } = new CatboxEngine();
2324

24-
public abstract Task<Url> UploadFileAsync(string file);
25+
public abstract Task<Url> UploadFileAsync(string file);
2526

26-
public long Size { get; set; }
27+
public long Size { get; set; }
2728

28-
private protected bool IsFileSizeValid(string file)
29-
{
30-
Size = FileSystem.GetFileSize(file);
31-
var b = Size > MaxSize;
29+
private protected bool IsFileSizeValid(string file)
30+
{
31+
Size = FileSystem.GetFileSize(file);
32+
var b = Size > MaxSize;
3233

33-
return !b;
34-
}
34+
return !b;
35+
}
3536

36-
protected void Verify(string file)
37-
{
38-
if (string.IsNullOrWhiteSpace(file))
39-
{
40-
throw new ArgumentNullException(nameof(file));
41-
}
37+
protected void Verify(string file)
38+
{
39+
if (string.IsNullOrWhiteSpace(file))
40+
{
41+
throw new ArgumentNullException(nameof(file));
42+
}
4243

43-
if (!IsFileSizeValid(file))
44-
{
45-
throw new ArgumentException($"File {file} is too large (max {MaxSize}) for {Name}");
46-
}
47-
}
44+
if (!IsFileSizeValid(file))
45+
{
46+
throw new ArgumentException($"File {file} is too large (max {MaxSize}) for {Name}");
47+
}
48+
}
4849

49-
public static readonly BaseUploadEngine[] All =
50-
ReflectionHelper.CreateAllInAssembly<BaseUploadEngine>(TypeProperties.Subclass).ToArray();
50+
public static readonly BaseUploadEngine[] All =
51+
ReflectionHelper.CreateAllInAssembly<BaseUploadEngine>(TypeProperties.Subclass).ToArray();
52+
}
53+
54+
public abstract class BaseCatboxEngine : BaseUploadEngine
55+
{
56+
57+
public override async Task<Url> UploadFileAsync(string file)
58+
{
59+
Verify(file);
60+
61+
using var response = await EndpointUrl
62+
.PostMultipartAsync(mp =>
63+
mp.AddFile("fileToUpload", file)
64+
.AddString("reqtype", "fileupload")
65+
.AddString("time", "1h")
66+
);
67+
68+
var responseMessage = response.ResponseMessage;
69+
70+
var content = await responseMessage.Content.ReadAsStringAsync();
71+
72+
/*if (!responseMessage.IsSuccessStatusCode) {
73+
74+
return null;
75+
}*/
76+
77+
return new(content);
78+
}
79+
80+
protected BaseCatboxEngine(string s) : base(s) { }
5181
}
Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Flurl.Http;
7-
using SmartImage.Lib.Engines.Impl.Upload;
1+
using Flurl.Http;
82

9-
public sealed class CatboxEngine : BaseUploadEngine
3+
namespace SmartImage.Lib.Engines.Impl.Upload;
4+
5+
public sealed class CatboxEngine : BaseCatboxEngine
106
{
117
public override string Name => "Catbox";
128

139
public override int MaxSize => 1 * 1000 * 1000 * 200;
1410

1511
public CatboxEngine() : base("https://catbox.moe/user/api.php") { }
16-
17-
public override async Task<Url> UploadFileAsync(string file)
18-
{
19-
Verify(file);
20-
21-
using var response = await EndpointUrl
22-
.PostMultipartAsync(mp =>
23-
mp.AddFile("fileToUpload", file)
24-
.AddString("reqtype", "fileupload")
25-
.AddString("time", "1h")
26-
);
27-
28-
var responseMessage = response.ResponseMessage;
29-
30-
var content = await responseMessage.Content.ReadAsStringAsync();
31-
32-
/*if (!responseMessage.IsSuccessStatusCode) {
33-
34-
return null;
35-
}*/
36-
37-
return new(content);
38-
}
3912
}

0 commit comments

Comments
 (0)