Skip to content

Commit ec08f24

Browse files
committed
Downloads; fixes; etc
1 parent c22c942 commit ec08f24

6 files changed

Lines changed: 59 additions & 39 deletions

File tree

SmartImage.Lib/Images/Uni/UniImage.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
using SixLabors.ImageSharp.Formats;
1515
using SixLabors.ImageSharp.Formats.Png;
1616
using SixLabors.ImageSharp.PixelFormats;
17-
using SixLabors.ImageSharp.Processing;
1817
using System.Diagnostics;
1918
using System.Runtime.InteropServices;
2019
using CoenM.ImageHash;
2120
using CommunityToolkit.HighPerformance;
21+
using Kantan.Net.Utilities;
2222
using Microsoft.IO;
2323
using SmartImage.Lib.Model;
2424
using SmartImage.Lib.Utilities;
@@ -45,7 +45,6 @@ public enum UniImageType
4545

4646
}
4747

48-
4948
/// <summary>
5049
/// <seealso cref="UniSource"/>
5150
/// </summary>
@@ -67,6 +66,8 @@ static UniImage()
6766

6867
public virtual long? Length => Bytes.Length;
6968

69+
// public virtual string Name {get; protected set;}
70+
7071
[JI]
7172
public string Value { get; }
7273

@@ -286,24 +287,26 @@ public bool TryDeleteFile()
286287
}
287288

288289
[MURV]
289-
public virtual string WriteImageToFile([CBN] string fn = null, [CBN] Action<IImageProcessingContext> operation = null)
290+
public virtual string WriteImageToFile([CBN] string fn = null)
290291
{
291292
if (!HasImage) {
292293
throw new InvalidOperationException();
293294
}
294295

295-
fn ??= Path.GetTempFileName();
296+
fn ??= this switch
297+
{
298+
UniImageUri uri => uri.Url.GetFileName(),
299+
UniImageFile file => file.LocalFileInfo.Name,
300+
_ => Path.GetRandomFileName()
301+
};
296302

297-
var encoder = new PngEncoder();
298-
operation ??= static _ => { };
303+
fn = Path.ChangeExtension(fn, ImageFormat.FileExtensions.First());
299304

300-
using var image = Image.Clone(operation);
305+
var path = Path.Combine(Path.GetTempPath(), fn);
301306

302-
// Image.Save(fn);
303-
image.Mutate(operation);
304-
image.Save(fn, encoder);
307+
Image.Save(path);
305308

306-
return fn;
309+
return path;
307310
}
308311

309312

SmartImage.Lib/Images/Uni/UniImageFile.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using Microsoft;
55
using SixLabors.ImageSharp.PixelFormats;
6-
using SixLabors.ImageSharp.Processing;
76
using System.IO.MemoryMappedFiles;
87
using JetBrains.Annotations;
98
using Microsoft.Extensions.Logging;
@@ -21,7 +20,7 @@ internal UniImageFile(FileInfo fi) : base(fi.FullName, UniImageType.File)
2120

2221
public FileInfo LocalFileInfo { get; }
2322

24-
public override string WriteImageToFile(string fn = null, Action<IImageProcessingContext> operation = null)
23+
public override string WriteImageToFile(string fn = null)
2524
{
2625
if (!HasFilePath) {
2726
throw new FileNotFoundException(Value);

SmartImage.Lib/Images/Uni/UniImageUri.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ public class UniImageUri : UniImage
2222
internal UniImageUri(Url url) : base(url?.ToString(), UniImageType.Uri)
2323
{
2424
Url = url;
25+
2526
}
2627

28+
// public override string Name => Url?.GetFileName();
29+
2730

2831
protected override async Task<bool> AllocAsync(CancellationToken ct = default)
2932
{

SmartImage.Rdx/Commands/SearchCommand.UI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public sealed partial class SearchCommand
6363

6464
private static readonly ConcurrentDictionary<SearchResultItem, int> tbl = new();
6565

66-
private static Selection GetResultItemIndexes(SearchResult res)
66+
private static Selection GetSelectionChoice(SearchResult res)
6767
{
6868
Selection ret;
6969

SmartImage.Rdx/Commands/SearchCommand.cs

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
using System.Collections.Concurrent;
5050
using System.Collections.Specialized;
5151
using System.Diagnostics;
52+
using System.IO.MemoryMappedFiles;
5253
using System.Linq;
5354
using System.Reflection;
5455
using System.Runtime.Caching;
@@ -299,14 +300,17 @@ private async Task RunInteractiveAsync(CancellationToken ct = default)
299300

300301
cmd = AnsiConsole.Prompt(ConsoleElements.Prm_Command);
301302

303+
if (cmd == R2.Chc_Exit) {
304+
return;
305+
}
302306
if (cmd == R2.Chc_Back) {
303307
break;
304308
}
305309

306-
var sel = GetResultItemIndexes(sr);
310+
var sel = GetSelectionChoice(sr);
307311
var sri = sel.Item;
308312

309-
s_logger.LogInformation("Selected {Item} {Scn} | {Idx1}, {Idx2}", sel.Item, sel.IsScannedItem, sel.ItemIdx, sel.ScanIdx);
313+
s_logger.LogDebug("Selected {Item} {Scn} | {Idx1}, {Idx2}", sel.Item, sel.IsScannedItem, sel.ItemIdx, sel.ScanIdx);
310314

311315
// s_logger.LogTrace("Interactive: {ResItem}", sri);
312316

@@ -322,12 +326,12 @@ await AnsiConsole.Live(srTable).StartAsync(async (f) =>
322326
s_logger.LogTrace("Scanning {Item}", sri);
323327
bool scannedOk = false;
324328
scannedOk = await sri.ScanAsync2(m_ctsRun.Token);
325-
329+
326330

327331
if (!scannedOk) {
328332
return;
329333
}
330-
334+
331335
/*if (!tbl.TryGetValue(sel.Item, out var idx)) {
332336
for (int j = 0; j < sel.Item.ScannedItems.Count; j++) {
333337
tbl[sel.Item.ScannedItems[j]] = j;
@@ -337,11 +341,10 @@ await AnsiConsole.Live(srTable).StartAsync(async (f) =>
337341
var idx = tbl[sel.Item.Parent];
338342

339343
int row;
340-
344+
341345
row = sr.Results.IndexOf(sri);
342346

343347
// row = idx;
344-
345348
// row = GetRowForItem(sri);
346349
// row = sr.Index(sel.Item, sel.Scanned);
347350

@@ -371,7 +374,6 @@ await AnsiConsole.Live(srTable).StartAsync(async (f) =>
371374
}
372375

373376

374-
375377
/*if (sr.ScannedResults.TryGetValue(sri, out SearchResultItem[] scanned)) {
376378
int i = 0;
377379
@@ -403,20 +405,8 @@ await AnsiConsole.Live(srTable).StartAsync(async (f) =>
403405

404406
AnsiConsole.Live(srTable).Start(f =>
405407
{
406-
// var row = GetRowForItem(sri);
407-
// var row = SearchResultItem.FindSumIndex(sri);
408-
// var row = sr.Index(sri);
409-
410-
// var row = sr.Index(sel.Item, sel.Scanned);
411-
412-
// var row = sel.Index();
413-
414-
var row = tbl[sel.Item];
415-
416-
// var row = (sel.ScanIdx == -1 ? 0 : sel.ScanIdx) + sel.ItemIdx;
417-
418-
// var row2 = sriPak;
419-
// var row = row2.RootIdx + (row2.ScnIdx == -1 ? 0 : (row2.ScnIdx + 1));
408+
//todo
409+
var row = sel.ItemIdx;
420410

421411
sri.CalculateSimilarity(Query.Source);
422412

@@ -441,7 +431,33 @@ await AnsiConsole.Live(srTable).StartAsync(async (f) =>
441431
}
442432

443433
if (cmd == R2.Chc_Download) {
444-
//todo
434+
435+
var ok = sri.TryWriteToFile();
436+
437+
if (ok) {
438+
AC.AlternateScreen(() =>
439+
{
440+
var gr = new Grid();
441+
gr.AddColumns(new GridColumn[] { new(), new() });
442+
gr.AddRow(new IRenderable[] { new Text("File", ConsoleElements.Sty_Name), new Text(sri.LocalFilePath) });
443+
AnsiConsole.Write(gr);
444+
var prompt = new ConfirmationPrompt("Open?") { };
445+
var choice = AnsiConsole.Prompt(prompt);
446+
447+
if (choice) {
448+
var proc = Process.Start(new ProcessStartInfo()
449+
{
450+
FileName = sri.LocalFilePath,
451+
WorkingDirectory = "",
452+
UseShellExecute = true
453+
});
454+
455+
proc?.WaitForExit();
456+
proc.Dispose();
457+
}
458+
});
459+
}
460+
445461
}
446462

447463
cont:
@@ -496,7 +512,7 @@ private CanvasImage GetPreview(SearchResultItem sri)
496512
var key = sri.Url;
497513
var val = m_prevCache.Get(key);
498514

499-
CanvasImage ci = val as CanvasImage;
515+
var ci = val as CanvasImage;
500516

501517
if (ci is null) {
502518
str = sri.GetStream();
@@ -536,7 +552,6 @@ private void ShowPreview(CanvasImage ci, SearchResultItem ui)
536552
ldc.Refresh();
537553
var cki = AnsiConsole.Console.Input.ReadKey(true);
538554

539-
540555
if (cki.HasValue) {
541556

542557
switch (cki.Value.Key) {

SmartImage.Rdx/Shell/ConsoleElements.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public static IEnumerable<TableColumn> GetColumns(params string[] names)
262262
AllowEmpty = false,
263263
Choices =
264264
{
265-
R2.Chc_Open, R2.Chc_Scan, R2.Chc_Preview, R2.Chc_Calc, R2.Chc_Exit, R2.Chc_Back
265+
R2.Chc_Open, R2.Chc_Scan, R2.Chc_Preview, R2.Chc_Calc, R2.Chc_Download, R2.Chc_Back, R2.Chc_Exit
266266
}
267267
};
268268

0 commit comments

Comments
 (0)