Skip to content

Commit 6af1d86

Browse files
更名
1 parent a99989e commit 6af1d86

13 files changed

Lines changed: 167 additions & 56 deletions

File tree

MarkdownImageAPI/Handlers/MarkdownRequestArgs.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.6.33815.320
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MarkdownImageAPI", "MarkdownImageAPI\MarkdownImageAPI.csproj", "{E5439AD8-3151-4DD2-9583-17A473C7658B}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniServer", "MiniServer\MiniServer.csproj", "{5D45D00D-FE0B-C360-5CD9-928DAB74D4FB}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
1111
Release|Any CPU = Release|Any CPU
1212
EndGlobalSection
1313
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14-
{E5439AD8-3151-4DD2-9583-17A473C7658B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15-
{E5439AD8-3151-4DD2-9583-17A473C7658B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16-
{E5439AD8-3151-4DD2-9583-17A473C7658B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17-
{E5439AD8-3151-4DD2-9583-17A473C7658B}.Release|Any CPU.Build.0 = Release|Any CPU
14+
{5D45D00D-FE0B-C360-5CD9-928DAB74D4FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5D45D00D-FE0B-C360-5CD9-928DAB74D4FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5D45D00D-FE0B-C360-5CD9-928DAB74D4FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5D45D00D-FE0B-C360-5CD9-928DAB74D4FB}.Release|Any CPU.Build.0 = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System.Diagnostics;
2+
using System.Net;
3+
using MiniServer.Interface;
4+
using Newtonsoft.Json;
5+
6+
namespace MiniServer.Handlers;
7+
8+
public class CommandHandler : IHttpRequestHandler
9+
{
10+
public string Path => "/command";
11+
public string Method => HttpMethod.Post.Method;
12+
public async Task InvokeAsync(HttpRequestArgs args, CancellationToken cancellationToken)
13+
{
14+
using var sr = new StreamReader(args.Context.Request.InputStream, args.Context.Request.ContentEncoding);
15+
var requestBody = await sr.ReadToEndAsync(cancellationToken);
16+
var param = JsonConvert.DeserializeObject<CommandRequestArgs>(requestBody) ?? throw new ArgumentNullException("请求参数不能为空");
17+
var result = Execute(param.Command, param.Arguments, param.WorkingDirectory);
18+
args.ReplyJson(result, HttpStatusCode.OK);
19+
}
20+
21+
public static CommandResult Execute(string command, string arguments = "", string? workingDirectory = null)
22+
{
23+
// 配置进程启动信息
24+
var startInfo = new ProcessStartInfo
25+
{
26+
FileName = command,
27+
Arguments = arguments,
28+
RedirectStandardOutput = true,
29+
RedirectStandardError = true,
30+
UseShellExecute = false,
31+
CreateNoWindow = true,
32+
WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory
33+
};
34+
35+
// 处理跨平台差异
36+
if (OperatingSystem.IsWindows())
37+
{
38+
// Windows 使用 cmd.exe 执行命令
39+
startInfo.FileName = "cmd.exe";
40+
startInfo.Arguments = $"/c {command} {arguments}";
41+
}
42+
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
43+
{
44+
// Linux/macOS 使用 bash 执行命令
45+
startInfo.FileName = "/bin/bash";
46+
startInfo.Arguments = $"-c \"{EscapeForBash(command)} {EscapeForBash(arguments)}\"";
47+
}
48+
49+
using var process = new Process { StartInfo = startInfo };
50+
51+
try
52+
{
53+
// 启动进程并等待完成
54+
process.Start();
55+
string output = process.StandardOutput.ReadToEnd();
56+
string error = process.StandardError.ReadToEnd();
57+
process.WaitForExit();
58+
59+
return new CommandResult()
60+
{
61+
ExitCode = process.ExitCode,
62+
Output = output.Trim(),
63+
Error = error.Trim()
64+
};
65+
}
66+
catch (Exception ex)
67+
{
68+
return new CommandResult()
69+
{
70+
ExitCode = -1,
71+
Output = string.Empty,
72+
Error = ex.ToString()
73+
};
74+
}
75+
}
76+
77+
// 处理 bash 特殊字符转义
78+
private static string EscapeForBash(string input)
79+
{
80+
if (string.IsNullOrEmpty(input)) return string.Empty;
81+
return input.Replace("\"", "\\\""); // 转义双引号
82+
}
83+
}
84+
85+
public class CommandRequestArgs
86+
{
87+
[JsonProperty("cmd")]
88+
public string Command { get; init; } = string.Empty;
89+
90+
[JsonProperty("args")]
91+
public string Arguments { get; init; } = string.Empty;
92+
93+
[JsonProperty("working_directory")]
94+
public string WorkingDirectory { get; init; } = string.Empty; // 工作目录,默认为当前目录
95+
}
96+
97+
/// <summary>
98+
/// 命令执行结果封装
99+
/// </summary>
100+
public class CommandResult
101+
{
102+
[JsonProperty("exit_code")]
103+
public int ExitCode { get; init; }
104+
105+
[JsonProperty("output")]
106+
public string Output { get; init; } = string.Empty;
107+
108+
[JsonProperty("Error")]
109+
public string Error { get; init; } = string.Empty;
110+
}

MarkdownImageAPI/Handlers/MarkdownHandler.cs renamed to MiniServer/Handlers/MarkdownHandler.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Net;
2-
using MarkdownImageAPI.Interface;
2+
using MiniServer.Interface;
33
using Newtonsoft.Json;
44

5-
namespace MarkdownImageAPI.Handlers;
5+
namespace MiniServer.Handlers;
66

77
public class MarkdownHandler : IHttpRequestHandler
88
{
@@ -19,3 +19,21 @@ public async Task InvokeAsync(HttpRequestArgs args, CancellationToken cancellati
1919
args.ReplyImage(buffer, HttpStatusCode.OK);
2020
}
2121
}
22+
23+
public class MarkdownRequestArgs
24+
{
25+
[JsonProperty("auto_width")]
26+
public bool AutoWidth { get; init; } = true;
27+
28+
[JsonProperty("auto_height")]
29+
public bool AutoHeight { get; init; } = true;
30+
31+
[JsonProperty("timeout")]
32+
public int TimeOut { get; init; } = 5000;
33+
34+
[JsonProperty("enable_dark")]
35+
public bool Dark { get; init; } = false;
36+
37+
[JsonProperty("content")]
38+
public string MarkdownContent { get; init; } = string.Empty;
39+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Text;
33
using Newtonsoft.Json;
44

5-
namespace MarkdownImageAPI;
5+
namespace MiniServer;
66

77
public class HttpRequestArgs(HttpListenerContext context)
88
{

MarkdownImageAPI/Interface/IHttpRequestHandler.cs renamed to MiniServer/Interface/IHttpRequestHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace MarkdownImageAPI.Interface;
1+
namespace MiniServer.Interface;
22

33
public interface IHttpRequestHandler
44
{
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using MarkdownImageAPI.Network;
2-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
32
using Microsoft.Extensions.Hosting;
3+
using MiniServer.Network;
44

5-
namespace MarkdownImageAPI;
5+
namespace MiniServer;
66

7-
public class MarkdownApp
7+
public class MiniServer
88
{
99
private static HostApplicationBuilder _hostApplicationBuilder = Host.CreateApplicationBuilder();
1010

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System.Net;
22
using System.Reflection;
3-
using MarkdownImageAPI.Interface;
43
using Microsoft.Extensions.Configuration;
54
using Microsoft.Extensions.Hosting;
65
using Microsoft.Extensions.Logging;
6+
using MiniServer.Interface;
77

8-
namespace MarkdownImageAPI.Network;
8+
namespace MiniServer.Network;
99

1010
public class HttpServer : IHostedService
1111
{
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System.Reflection;
2-
using MarkdownImageAPI;
2+
using MiniServer;
33

44
Utils.Kill();
55

6-
Console.Title = "MarkdownImageAPI";
6+
Console.Title = "MiniServer";
77
if (!File.Exists("appsettings.json"))
88
{
99
var assembly = Assembly.GetExecutingAssembly();
10-
using var stream = assembly.GetManifestResourceStream("MarkdownImageAPI.Resources.appsettings.json");
10+
using var stream = assembly.GetManifestResourceStream("MiniServer.Resources.appsettings.json");
1111
if (stream == null)
1212
{
1313
Console.WriteLine("appsettings.json not found in resources.");
@@ -18,4 +18,4 @@
1818
fs.Close();
1919
stream?.Close();
2020
}
21-
MarkdownApp.Start();
21+
MiniServer.MiniServer.Start();

0 commit comments

Comments
 (0)