Skip to content

Commit a60ad17

Browse files
committed
Pipe input support
1 parent e79c8ec commit a60ad17

File tree

4 files changed

+57
-57
lines changed

4 files changed

+57
-57
lines changed

SmartImage.Lib 3/UniImage.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public static async Task<UniImage> TryCreateAsync(object o, CancellationToken t
116116
}
117117

118118
}, str, t);
119+
119120
str.TrySeek();
120121

121122
var query = new UniImage(o, str, qt)
@@ -181,7 +182,7 @@ public static bool IsFileType(object o, out FileInfo f)
181182
return f != null;
182183
}
183184

184-
public static bool IsValidSourceType(object str)
185+
public static bool IsValidSourceType(object str, bool checkExt = true)
185186
{
186187
// UniSourceType v = UniHandler.GetUniType(str, out object o2);
187188
/*bool isFile = UniSourceFile.IsType(str, out var f);
@@ -192,7 +193,8 @@ public static bool IsValidSourceType(object str)
192193
bool isStream = IsStreamType(str, out var f3);
193194
bool ok = isFile || isUri || isStream;
194195

195-
if (isFile) {
196+
if (isFile && checkExt) {
197+
//todo
196198
string ext = Path.GetExtension(str.ToString())?[1..];
197199
return FileType.Image.Any(x => x.Subtype == ext);
198200
}

SmartImage.Rdx/Program.cs

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using System.Collections.Concurrent;
1+
using System.Buffers;
2+
using System.Collections.Concurrent;
23
using System.Diagnostics;
34
using System.Reflection;
5+
using System.Runtime.InteropServices;
46
using System.Text;
57
using Kantan.Text;
68
using Novus.Streams;
79
using SixLabors.ImageSharp;
810
using SixLabors.ImageSharp.Formats;
11+
using SmartImage.Lib;
912
using Spectre.Console;
1013
using Spectre.Console.Cli;
1114
using SmartImage.Rdx.Shell;
@@ -27,57 +30,63 @@ namespace SmartImage.Rdx;
2730
public static class Program
2831
{
2932

33+
private static readonly byte[] Utf8_Bom_Sig = new[]
34+
{
35+
(byte) 0xEF, (byte) 0xBB, (byte) 0xBF
36+
};
37+
38+
public static string ReadInputStream(out bool isFile)
39+
{
40+
41+
var path = Path.GetTempFileName();
42+
43+
using var fs = File.Open(path, FileMode.Truncate, FileAccess.Write);
44+
45+
using Stream stdin = Console.OpenStandardInput();
46+
47+
byte[] buffer = new byte[4096]; // Buffer to hold byte input
48+
int bytesRead;
49+
int iter = 0;
50+
51+
while ((bytesRead = stdin.Read(buffer, 0, buffer.Length)) > 0) {
52+
if (iter == 0) {
53+
54+
if (buffer[0] == Utf8_Bom_Sig[0]
55+
&& buffer[1] == Utf8_Bom_Sig[1]
56+
&& buffer[2] == Utf8_Bom_Sig[2]) {
57+
58+
buffer = buffer[3..];
59+
bytesRead -= Utf8_Bom_Sig.Length;
60+
}
61+
}
62+
63+
fs.Write(buffer, 0, bytesRead);
64+
65+
iter++;
66+
}
67+
68+
isFile = File.Exists(path);
69+
fs.Flush();
70+
fs.Dispose();
71+
72+
return path;
73+
}
74+
3075
public static async Task<int> Main(string[] args)
3176
{
3277
Debug.WriteLine(AConsole.Profile.Height);
3378
Debug.WriteLine(Console.BufferHeight);
3479

3580
if (Console.IsInputRedirected) {
36-
// using var str = Console.OpenStandardInput(500);
37-
Stream stdin = Console.OpenStandardInput(1000);
38-
/*byte[] inBuffer = new byte[1_000_000];
39-
40-
int outLen = stdin.Read(inBuffer, 0, inBuffer.Length);
41-
char[] chars = Encoding.ASCII.GetChars(inBuffer, 0, outLen);
42-
var cmd = new string(chars);
43-
if ((cmd[cmd.Length - 2] == '\r') && (cmd[cmd.Length - 1] == '\n'))
44-
{
45-
cmd = cmd.Substring(0, cmd.Length - 2);
46-
}*/
47-
var sr = new StreamReader(stdin);
48-
int n1 = 0, n2 = 0;
49-
var sb = new char[8_000_000];
50-
51-
while (!sr.EndOfStream) {
52-
/*if (sb[^2] == '\r' && sb[^1] == '\n') {
53-
break;
54-
}*/
55-
n1 += (n2 = sr.Read(sb, n1, sb.Length - n1));
56-
57-
if (n2 == 0) {
58-
break;
59-
}
81+
var pipeInput = ReadInputStream(out var isf);
6082

61-
}
62-
63-
sb = sb[..n1];
64-
65-
if (sb[^2] == '\r' && sb[^1] == '\n') {
66-
sb = sb[0..^2];
67-
}
83+
// AConsole.WriteLine($"[{pipeInput}] {isf}");
6884

69-
IImageFormat fmt;
70-
AConsole.WriteLine($"{sb.Length}");
85+
var newargs = new string[args.Length + 1];
86+
newargs[0] = pipeInput;
87+
args.CopyTo(newargs, 1);
7188

72-
/*try {
73-
fmt = await Image.DetectFormatAsync();
74-
}
75-
catch (Exception e) {
76-
AConsole.WriteLine($"{e.Message}");
77-
}
78-
finally {
79-
str.TrySeek();
80-
}*/
89+
args = newargs;
8190
}
8291

8392
var ff = CliFormat.LoadFigletFontFromResource(nameof(R2.Fg_larry3d), out var ms);

SmartImage.Rdx/SearchCommand.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@ private async Task SetupSearchAsync(ProgressContext ctx)
8888
var p = ctx.AddTask("Creating query");
8989
p.IsIndeterminate = true;
9090

91-
if (m_scs.Raw) {
92-
93-
}
94-
9591
Query = await SearchQuery.TryCreateAsync(m_scs.Query);
9692

9793
p.Increment(COMPLETE / 2);

SmartImage.Rdx/SearchCommandSettings.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,13 @@ internal sealed class SearchCommandSettings : CommandSettings
8383

8484
#endregion
8585

86-
public bool Raw { get; internal set; }
87-
public byte[] Raw2 { get; internal set; }
88-
8986
// public const string PROP_ARG_RESULTS = "$all_results";
9087

9188
public override ValidationResult Validate()
9289
{
9390
var result = base.Validate();
9491

95-
if (String.IsNullOrWhiteSpace(Query)) {
96-
97-
}
98-
99-
else if (!UniImage.IsValidSourceType(Query)) {
92+
if (!UniImage.IsValidSourceType(Query, false)) {
10093
return ValidationResult.Error("Invalid query");
10194
}
10295

0 commit comments

Comments
 (0)