Skip to content

Commit 8d6232a

Browse files
committed
Experimenting
1 parent 0f091d2 commit 8d6232a

File tree

7 files changed

+202
-13
lines changed

7 files changed

+202
-13
lines changed

SmartImage.Lib 3/HydrusClient.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ namespace SmartImage.Lib;
2323

2424
public class HydrusClient : IEndpoint, INotifyPropertyChanged
2525
{
26-
private const string HDR_HYDRUS_KEY = "Hydrus-Client-API-Access-Key";
26+
private const string HDR_HYDRUS_KEY = "Hydrus-Client-API-Access-Key";
27+
private const string GET_FILES_THUMBNAIL = "/get_files/thumbnail";
28+
private const string GET_FILES_FILE = "/get_files/file";
2729

2830
public FlurlClient Client { get; }
2931

@@ -97,6 +99,40 @@ public async Task<JsonValue> GetFileRelationshipsAsync(string hash)
9799
return j;
98100
}
99101

102+
public async Task<IFlurlResponse> GetFileAsync(string hash)
103+
{
104+
var res = await Client.Request(GET_FILES_FILE)
105+
.SetQueryParam("hash", hash)
106+
.GetAsync();
107+
108+
return res;
109+
}
110+
public async Task<IFlurlResponse> GetFileAsync(int id)
111+
{
112+
var res = await Client.Request(GET_FILES_FILE)
113+
.SetQueryParam("file_id", id)
114+
.GetAsync();
115+
116+
return res;
117+
}
118+
public async Task<IFlurlResponse> GetFileThumbnailAsync(string hash)
119+
{
120+
var res = await Client.Request(GET_FILES_THUMBNAIL)
121+
.SetQueryParam("hash", hash)
122+
.GetAsync();
123+
124+
return res;
125+
}
126+
127+
public async Task<IFlurlResponse> GetFileThumbnailAsync(int id)
128+
{
129+
var res = await Client.Request(GET_FILES_THUMBNAIL)
130+
.SetQueryParam("file_id", id)
131+
.GetAsync();
132+
133+
return res;
134+
}
135+
100136
private string m_key;
101137

102138
public string Key

SmartImage.UI/ControlsHelper.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,12 @@ public static string FormatSize(UniSource uni)
146146

147147
return bytes;
148148
}
149+
150+
public static (bool ctrl, bool alt, bool shift) GetModifiers()
151+
{
152+
var ctrl = Keyboard.Modifiers.HasFlag(ModifierKeys.Control);
153+
var alt = Keyboard.Modifiers.HasFlag(ModifierKeys.Alt);
154+
var shift = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift);
155+
return (ctrl, alt, shift);
156+
}
149157
}

SmartImage.UI/HydrusWindow.xaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Window x:Class="SmartImage.UI.HydrusWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:SmartImage.UI"
7+
xmlns:model="clr-namespace:SmartImage.UI.Model"
8+
mc:Ignorable="d"
9+
DataContext="{Binding RelativeSource={RelativeSource Self}}"
10+
11+
Title="HydrusWindow" Height="450" Width="800">
12+
<Window.Resources>
13+
<model:SharedInfo x:Key="SharedInfo" />
14+
</Window.Resources>
15+
<Grid>
16+
<TextBox x:Name="Tb_Info" HorizontalAlignment="Left" Margin="500,10,0,0" TextWrapping="Wrap" Text="{Binding Hash}"
17+
DataContext="{StaticResource SharedInfo }"
18+
VerticalAlignment="Top" Width="135" />
19+
20+
<Border x:Name="Br_Preview" Height="150" Margin="0,10,10,0" Width="150"
21+
Background="{DynamicResource Black2}"
22+
VerticalAlignment="Top" HorizontalAlignment="Right" Panel.ZIndex="1">
23+
24+
<Image x:Name="Img_Preview" Width="150" HorizontalAlignment="Right" VerticalAlignment="Top"
25+
Panel.ZIndex="0" />
26+
</Border>
27+
<Button x:Name="Btn_1" Content="Button" HorizontalAlignment="Left" Margin="595,33,0,0" VerticalAlignment="Top" Click="Btn_1_OnClick"/>
28+
</Grid>
29+
</Window>

SmartImage.UI/HydrusWindow.xaml.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Cryptography;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using System.Windows.Controls;
9+
using System.Windows.Data;
10+
using System.Windows.Documents;
11+
using System.Windows.Input;
12+
using System.Windows.Media;
13+
using System.Windows.Media.Imaging;
14+
using System.Windows.Shapes;
15+
using Kantan.Utilities;
16+
using Novus.Streams;
17+
using SmartImage.UI.Model;
18+
19+
namespace SmartImage.UI;
20+
21+
/// <summary>
22+
/// Interaction logic for HydrusWindow.xaml
23+
/// </summary>
24+
public partial class HydrusWindow : Window
25+
{
26+
public HydrusWindow(SharedInfo s)
27+
{
28+
DataContext = this;
29+
InitializeComponent();
30+
Shared = s;
31+
}
32+
33+
public SharedInfo Shared { get; set; }
34+
35+
private void Btn_1_OnClick(object sender, RoutedEventArgs e)
36+
{
37+
Tb_Info.Dispatcher.InvokeAsync(async () =>
38+
{
39+
Shared.Query.Uni.Stream.TrySeek();
40+
var data = SHA256.HashData(Shared.Query.Uni.Stream);
41+
var hash = HashHelper.Sha256.ToString(data);
42+
var t = await Shared.m_hydrus.GetFileAsync(hash);
43+
var d = await t.GetStreamAsync();
44+
var imageSource = new BitmapImage();
45+
imageSource.BeginInit();
46+
imageSource.StreamSource = d;
47+
imageSource.EndInit();
48+
imageSource.Freeze();
49+
Img_Preview.Source = imageSource;
50+
});
51+
e.Handled = true;
52+
}
53+
}

SmartImage.UI/MainWindow.Handlers.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ void CheckMedia()
344344

345345
private void Lv_Results_KeyDown(object sender, KeyEventArgs e)
346346
{
347-
var ctrl = Keyboard.Modifiers.HasFlag(ModifierKeys.Control);
348-
var alt = Keyboard.Modifiers.HasFlag(ModifierKeys.Alt);
349-
var shift = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift);
347+
(bool ctrl, bool alt, bool shift) = ControlsHelper.GetModifiers();
350348

351349
var key = e.Key;
352350

@@ -404,6 +402,11 @@ private void Lv_Results_KeyDown(object sender, KeyEventArgs e)
404402
case Key.R when ctrl && alt:
405403
Dispatcher.InvokeAsync(() => RetryEngineAsync(CurrentResultItem));
406404
break;
405+
case Key.H when ctrl:
406+
var w = new HydrusWindow(Shared);
407+
w.Show();
408+
break;
409+
407410
}
408411

409412
e.Handled = true;

SmartImage.UI/MainWindow.xaml.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ static MainWindow() { }
7171

7272
public MainWindow()
7373
{
74-
Client = new SearchClient(new SearchConfig());
74+
// Client = new SearchClient(new SearchConfig());
75+
Shared = new SharedInfo();
7576
m_queries = new ConcurrentDictionary<string, SearchQuery>();
7677

7778
InitializeComponent();
@@ -147,6 +148,7 @@ public MainWindow()
147148
PropertyChangedEventManager.AddHandler(this, OnCurrentQueueItemChanged, nameof(CurrentQueueItem));
148149
// m_hydrus = new HydrusClient()
149150
ParseArgs(Args);
151+
150152
}
151153

152154
#region
@@ -163,15 +165,14 @@ public MainWindow()
163165

164166
private readonly SemaphoreSlim m_us;
165167
private readonly List<string> m_pipeBuffer;
166-
private readonly HydrusClient m_hydrus;
167168

168169
#endregion
169170

170171
#region
171172

172-
public SearchClient Client { get; }
173+
public SearchClient Client => Shared.Client;
173174

174-
public SearchConfig Config => Client.Config;
175+
public SearchConfig Config => Shared.Config;
175176

176177
public ObservableCollection<ResultItem> Results { get; private set; }
177178

@@ -191,6 +192,8 @@ public bool InPath
191192

192193
#region
193194

195+
public SharedInfo Shared { get; set; }
196+
194197
private readonly ConcurrentDictionary<UniResultItem, string> m_uni;
195198

196199
private readonly ConcurrentDictionary<string, SearchQuery> m_queries;
@@ -210,7 +213,11 @@ public bool InPath
210213

211214
#region Queue/Query
212215

213-
public SearchQuery Query { get; internal set; }
216+
public SearchQuery Query
217+
{
218+
get => Shared.Query;
219+
internal set => Shared.Query = value;
220+
}
214221

215222
private string m_currentQueueItem;
216223

@@ -480,7 +487,7 @@ public bool UseClipboard
480487

481488
private static int _clipboardSequence;
482489

483-
// [DebuggerHidden]
490+
[DebuggerHidden]
484491
private void ClipboardListenAsync(object? s, EventArgs e)
485492
{
486493
/*if (IsInputReady() /*|| Query != SearchQuery.Null#1#) {
@@ -957,7 +964,7 @@ private async void RetryEngineAsync(ResultItem ri)
957964

958965
#endregion
959966

960-
#region
967+
#region
961968

962969
public static readonly string[] Args = Environment.GetCommandLineArgs();
963970

@@ -1032,7 +1039,7 @@ private void ChangeStatus2(ResultItem ri)
10321039
}
10331040
}
10341041

1035-
#region
1042+
#region
10361043

10371044
private void Log(LogEntry l)
10381045
{
@@ -1080,7 +1087,7 @@ private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
10801087
10811088
public event PreviewChangedCallback? PreviewChanged;*/
10821089

1083-
#region
1090+
#region
10841091

10851092
private void UpdatePreview(ResultItem ri)
10861093
{

SmartImage.UI/Model/SharedInfo.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Runtime.CompilerServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows.Shapes;
9+
using SmartImage.Lib;
10+
11+
namespace SmartImage.UI.Model
12+
{
13+
public class SharedInfo : INotifyPropertyChanged
14+
{
15+
public SharedInfo()
16+
{
17+
Client = new SearchClient(new SearchConfig());
18+
19+
m_hydrus = new HydrusClient(Config.HydrusEndpoint, Config.HydrusKey);
20+
}
21+
22+
public readonly HydrusClient m_hydrus;
23+
24+
public SearchClient Client { get; }
25+
26+
public SearchConfig Config => Client.Config;
27+
28+
private string _hash;
29+
30+
public string Hash
31+
{
32+
get { return _hash; }
33+
set
34+
{
35+
if (_hash != value) {
36+
_hash = value;
37+
OnPropertyChanged();
38+
}
39+
}
40+
}
41+
public SearchQuery Query
42+
{
43+
get;
44+
internal set;
45+
}
46+
public event PropertyChangedEventHandler? PropertyChanged;
47+
48+
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
49+
{
50+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)