Skip to content

Commit 58259f1

Browse files
committed
...
1 parent dfdb8d9 commit 58259f1

15 files changed

Lines changed: 307 additions & 385 deletions

File tree

SmartImage.Lib/Engines/OllamaModel.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ public OllamaRequest() { }
6464

6565
public Task<IFlurlResponse> CreateRequestAsync(SearchQuery query, CancellationToken ct = default)
6666
{
67-
using Stream stream = query.Source.Image.ToStream();
68-
var rg = ((MemoryStream) stream).ToArray();
69-
70-
var b64 = Convert.ToBase64String(rg);
67+
var bytes = query.Source.Image.ToBytes();
68+
var b64 = Convert.ToBase64String(bytes);
7169

7270

7371
var ollamaRequest = new OllamaRequest()

SmartImage.Lib/Engines/Results/SearchResultItem.cs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public record SearchResultItem : IDisposable, IComparable<SearchResultItem>, ICo
2121
[JI]
2222
public SearchResult Root { get; }
2323

24-
[CBN]
2524
[JI]
25+
[CBN]
2626
public SearchResultItem Parent { get; internal set; }
2727

2828
[MN]
@@ -170,8 +170,7 @@ public SearchResultItem[] CreateChildren(string[] rg)
170170
{
171171
var rg2 = new SearchResultItem[rg.Length];
172172

173-
for (int i = 0; i < rg.Length; i++)
174-
{
173+
for (int i = 0; i < rg.Length; i++) {
175174

176175
rg2[i] = new SearchResultItem(this)
177176
{
@@ -213,13 +212,11 @@ public SearchResultItem[] CreateChildren(string[] rg)
213212

214213
public async ValueTask<bool> LoadThumbnail(CancellationToken ct = default)
215214
{
216-
if (Url.IsValid(Thumbnail) && !(HasUni && Uni.Any(u => u.ValueString == Thumbnail)))
217-
{
215+
if (Url.IsValid(Thumbnail) && !(HasUni && Uni.Any(u => u.ValueString == Thumbnail))) {
218216

219217
var uni = await UniImage.TryCreateAsync(Thumbnail, ct: ct);
220218

221-
if (uni == null)
222-
{
219+
if (uni == null) {
223220
return false;
224221
}
225222

@@ -234,13 +231,11 @@ public async Task<bool> ScanAsync(CancellationToken ct = default)
234231
// TODO: USE CHANNELS
235232
// TODO: REFACTOR TO USE THIS FUNCTION
236233

237-
if (HasUni)
238-
{
234+
if (HasUni) {
239235
return true;
240236
}
241237

242-
if (Url == null)
243-
{
238+
if (Url == null) {
244239
return false;
245240
}
246241

@@ -251,17 +246,14 @@ public async Task<bool> ScanAsync(CancellationToken ct = default)
251246

252247
var tasks = ImageScanner.ScanImagesAsync(Url, ch.Writer, ct: ct);
253248

254-
while (await ch.Reader.WaitToReadAsync(ct))
255-
{
249+
while (await ch.Reader.WaitToReadAsync(ct)) {
256250
var v = await ch.Reader.ReadAsync(ct);
257251

258-
if (v != UniImage.Null && v.HasImageFormat)
259-
{
252+
if (v != UniImage.Null && v.HasImageFormat) {
260253
buf.Add(v);
261254
}
262255

263-
if (ct.IsCancellationRequested)
264-
{
256+
if (ct.IsCancellationRequested) {
265257
break;
266258
}
267259
}
@@ -303,10 +295,8 @@ public void Dispose()
303295
{
304296
Debug.WriteLine($"Disposing {Url} of {Root.Engine.Name}", LogCategories.C_VERBOSE);
305297

306-
if (Uni != null && Uni.Any())
307-
{
308-
foreach (var us in Uni)
309-
{
298+
if (Uni != null && Uni.Any()) {
299+
foreach (var us in Uni) {
310300

311301
us?.Dispose();
312302
}

SmartImage.Lib/Engines/Search/GoogleLensEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
189189
.GetAsync(cancellationToken: token);
190190
191191
var resData = await res2.GetStreamAsync();*/
192-
var str = await res.GetStringAsync().ConfigureAwait(false);
192+
// var str = await res.GetStringAsync().ConfigureAwait(false);
193193

194194
var resData = await res.GetStreamAsync().ConfigureAwait(false);
195195

SmartImage.Lib/Engines/Search/IqdbEngine.cs

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
6262

6363
IDocument document = null;
6464

65-
try
66-
{
65+
try {
6766
var response = await Client.Request(Endpoint)
6867
.OnError(r =>
6968
{
@@ -96,26 +95,7 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
9695
return;
9796
}, cancellationToken: token);
9897

99-
/*
100-
var response = await Client.Request(URL_QUERY)
101-
.OnError(r =>
102-
{
103-
// Debug.WriteLine($"{r.Exception}", Name);
104-
// r.ExceptionHandled = true;
105-
Logger.LogError(r.Exception, $"{Name}");
106-
Debugger.Break();
107-
#if !DEBUG
108-
r.ExceptionHandled = true;
109-
#endif
110-
}
111-
)
112-
.SetQueryParam("url", query.Upload)
113-
.WithTimeout(Timeout)
114-
.GetAsync(cancellationToken: ct);
115-
*/
116-
117-
if (response != null)
118-
{
98+
if (response != null) {
11999
var s = await response.GetStringAsync().ConfigureAwait(false);
120100

121101
var parser = new HtmlParser();
@@ -124,18 +104,17 @@ protected override async Task<IDocument> GetDocumentAsync(SearchResult sr, Searc
124104
// goto ret;
125105

126106
}
127-
else
128-
{
107+
else {
129108
// Debugger.Break();
130109
}
131110

132111
response?.Dispose();
133112

134113
goto ret;
135114
}
136-
catch (Exception e)
137-
{
115+
catch (Exception e) {
138116
Debug.WriteLine($"{e.Message}!");
117+
Logger.LogError(e, "{Func}: {Name}", nameof(GetDocumentAsync), Name);
139118
goto ret;
140119
}
141120

@@ -166,8 +145,7 @@ protected override ValueTask<IEnumerable<IqdbItem>> GetItems(IEnumerable<IHtmlCo
166145
{
167146
var buf = new List<IqdbItem>();
168147

169-
foreach (var c in source)
170-
{
148+
foreach (var c in source) {
171149
var iq = IqdbItem.ParseResultItem(c, r);
172150
buf.Add(iq);
173151
}
@@ -182,22 +160,19 @@ protected override bool Validate(IDocument doc, SearchResult sr)
182160
if (!b)
183161
goto ret;
184162

185-
if (doc is { Body: not null } bod)
186-
{
163+
if (doc is { Body: not null } bod) {
187164

188-
if (doc.GetElementsByClassName("err") is { Length: > 0 } err)
189-
{
165+
if (doc.GetElementsByClassName("err") is { Length: > 0 } err) {
190166
var fe = err[0];
191167
sr.Status = SearchResultStatus.UnknownError;
192168
sr.ErrorMessage = $"{fe.TextContent}";
193169
b = false;
194170
goto ret;
195171
}
196172

197-
if (bod.QuerySelector(Serialization.S_Iqdb_NoMatches) != null)
198-
{
173+
if (bod.QuerySelector(Serialization.S_Iqdb_NoMatches) != null) {
199174
sr.Flags |= SearchResultFlags.NoResults;
200-
b = false;
175+
b = false;
201176
}
202177
}
203178

@@ -237,24 +212,21 @@ public static IqdbItem ParseResultItem(IHtmlCollection<IElement> tr, SearchResul
237212

238213
//img.ChildNodes[0].ChildNodes[0].TryGetAttribute("href")
239214

240-
try
241-
{
215+
try {
242216
//url = src.FirstChild.ChildNodes[2].ChildNodes[0].TryGetAttribute("href");
243217

244218
url = img.ChildNodes[0].ChildNodes[0].TryGetAttribute(Serialization.Atr_href);
245219

246220
// Links must begin with http:// in order to work with "start"
247221

248222
}
249-
catch
250-
{
223+
catch {
251224
// ignored
252225
}
253226

254227
int w = 0, h = 0;
255228

256-
if (tr.Length >= 4)
257-
{
229+
if (tr.Length >= 4) {
258230
var res = tr[3];
259231

260232
string[] wh = res.TextContent.Split(Strings.Constants.MUL_SIGN);
@@ -270,35 +242,30 @@ public static IqdbItem ParseResultItem(IHtmlCollection<IElement> tr, SearchResul
270242

271243
double? sim;
272244

273-
if (tr.Length >= 5)
274-
{
245+
if (tr.Length >= 5) {
275246
var simNode = tr[4];
276247
string simStr = simNode.TextContent.Split('%')[0];
277248
sim = double.Parse(simStr);
278249
sim = Math.Round(sim.Value, 2);
279250
}
280-
else
281-
{
251+
else {
282252
sim = null;
283253
}
284254

285255
Url uri;
286256

287-
if (url != null)
288-
{
257+
if (url != null) {
289258
// Url u = url;
290259

291-
if (url.StartsWith("//"))
292-
{
260+
if (url.StartsWith("//")) {
293261
url = "https:" + url;
294262

295263
// url = url[2..];
296264
}
297265

298266
uri = url;
299267
}
300-
else
301-
{
268+
else {
302269
uri = null;
303270
}
304271

0 commit comments

Comments
 (0)