Commit 55c4137f by 何阳

NEW: 1. 增加是否上传选项,是否匿名选项

2.  增加简单账号登陆
3. 增加上传日志统计
parent 10ff1887
......@@ -17,6 +17,8 @@ namespace FtpAgent
public string Ip { get; set; }
public int Port { get; set; }
public string Dir{ get; set; }
public bool Upload{ get; set; }
public bool Anonymous { get; private set; }
public Config(string filePath)
{
......@@ -37,6 +39,8 @@ namespace FtpAgent
Ip = config["OCR:Ip"];
Port = int.Parse(config["OCR:Port"]);
Dir = config["OCR:DIR"];
Upload = bool.Parse(config["OCR:Upload"]);
Anonymous = bool.Parse(config["OCR:Anonymous"]);
}
}
}
using FubarDev.FtpServer.AccountManagement;
using Serilog;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using FubarDev.FtpServer.FileSystem;
using FubarDev.FtpServer.AccountManagement.Anonymous;
namespace FtpAgent
{
public class TestMembershipProvider : IMembershipProvider
{
public Task<MemberValidationResult> ValidateUserAsync(string username, string password)
{
Log.Debug($"user:{username} passwd:{password}");
if (username == "admin" && password == "wj123456")
{
Log.Debug("登陆成功");
var identity = new ClaimsIdentity();
identity.AddClaim(new Claim(ClaimTypes.Name, username));
identity.AddClaim(new Claim(ClaimTypes.Role, "admin"));
return Task.FromResult(new MemberValidationResult(MemberValidationStatus.AuthenticatedUser, new ClaimsPrincipal(identity)));
}
Log.Debug("登陆失败");
return Task.FromResult(new MemberValidationResult(MemberValidationStatus.InvalidLogin));
}
}
}
......@@ -5,7 +5,7 @@
<TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<AssemblyVersion>1.0.0.1</AssemblyVersion>
</PropertyGroup>
......
......@@ -8,13 +8,15 @@ using CSharpFunctionalExtensions;
namespace FtpAgent
{
public partial class OcrServer
public class OcrServer
{
public static string ServerLog { get; set; } = "upload.log";
public static string OCR_SERVER_URL { get; private set; } = "https://hardware-test.guke.tech";
public static string OCR_API_URL { get; private set; } = "/api/service/ocr/v2/recognize";
[ErrorHandler<OcrResultData>("上传图片到OCR服务器失败!")]
public static async Task<Result<OcrResultData, ApiServerError>> UploadFile(OcrRequest result)
{
File.AppendAllText(ServerLog, "图片: " + result.ocrFile +"\n");
string url = $"{OCR_SERVER_URL}{OCR_API_URL}?ocrChannel=1&ocrType=4&remark=certificate test";
using (var httpclient = new HttpClient())
{
......@@ -37,8 +39,10 @@ namespace FtpAgent
Log.Error($"请求远程OCR API失败,失败code=${respond.code}, msg={respond.detail}");
return Result.Failure<OcrResultData, ApiServerError>(new ApiServerError() { Code = respond.code, Msg = respond.msg });
}
var test = respond?.data?.lines.Select(item => item.text).Aggregate((l, f) => l + f);
Log.Information($"返回结果文本长度:{test.Length}");
var text = respond?.data?.lines.Select(item => item.text).Aggregate((l, f) => l + " " + f);
Log.Information($"返回结果文本长度:{text.Length}");
Log.Information($"拼接后的识别结果:{text}");
File.AppendAllText(ServerLog, "内容: "+text +"\n");
return Result.Success<OcrResultData, ApiServerError>(respond.data);
}
}
......
......@@ -4,12 +4,12 @@
using CaptureAgent;
using FtpAgent;
using FubarDev.FtpServer;
using FubarDev.FtpServer.AccountManagement;
using FubarDev.FtpServer.FileSystem.DotNet;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using System.Reflection;
HashSet<string> FileChangeCnt = new();
Logging.InitLogging();
Log.Information($"FTP Agent 启动");
......@@ -21,21 +21,22 @@ Assembly assembly = Assembly.GetExecutingAssembly();
Version version = assembly.GetName().Version;
string informationalVersion = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
Log.Information($"程序版本:{informationalVersion}");
Log.Information($"Customer Name: {config.CustomerName}");
Log.Information($"Device No: {config.DeviceNo}");
Log.Information($"OCR Type: {config.OcrType}");
Log.Information($"Order No: {config.OrderNo}");
Log.Information($"Order Type: {config.OrderType}");
Log.Information($"Photo Mode: {config.PhotoMode}");
Log.Information($"客户名: {config.CustomerName}");
Log.Information($"设备号: {config.DeviceNo}");
Log.Information($"OCR 类型: {config.OcrType}");
Log.Information($"订单号: {config.OrderNo}");
Log.Information($"订单类型: {config.OrderType}");
Log.Information($"拍照模式: {config.PhotoMode}");
Log.Information($"Secret: {config.Secret}");
Log.Information($"Tenant Id: {config.TenantId}");
Log.Information($"OCR Server URL: {config.OcrServerUrl}");
Log.Information($"租户 Id: {config.TenantId}");
Log.Information($"OCR 服务器 URL: {config.OcrServerUrl}");
Log.Information($"OCR API URL: {config.OcrApiUrl}");
Log.Information($"IP: {config.Ip}");
Log.Information($"Port: {config.Port}");
Log.Information($"Dir: {config.Dir}");
Log.Information($"端口: {config.Port}");
Log.Information($"图片目录: {config.Dir}");
Log.Information($"是否上传: {config.Upload}");
Log.Information($"是否匿名: {config.Anonymous}");
// Setup dependency injection
......@@ -48,10 +49,21 @@ services.Configure<DotNetFileSystemOptions>(opt => opt
// Add FTP server services
// DotNetFileSystemProvider = Use the .NET file system functionality
// AnonymousMembershipProvider = allow only anonymous logins
services.AddFtpServer(builder => builder
.UseDotNetFileSystem() // Use the .NET file system functionality
.EnableAnonymousAuthentication()); // allow anonymous logins
// Add FTP server services
services.AddFtpServer(builder =>
{
builder.UseDotNetFileSystem(); // Use the .NET file system functionality
if (config.Anonymous)
{
// Enable anonymous logins
builder.EnableAnonymousAuthentication();
}
else
{
// Register your custom membership provider
services.AddSingleton<IMembershipProvider, TestMembershipProvider>();
}
});
......@@ -100,15 +112,14 @@ using (var serviceProvider = services.BuildServiceProvider())
async void Watcher_Changed(object sender, FileSystemEventArgs e)
{
// FTP文件被修改两次,才算完成上传
if (!FileChangeCnt.TryGetValue(e.Name, out string res))
Log.Information("{Name} 文件接收完成!",e.Name);
if (!config.Upload)
{
FileChangeCnt.Add(e.Name);
return;
}
Log.Information("{Name} 文件接收完成!",e.Name);
//如果被修改的是jpg文件,则转发到OCR服务器
if (Path.GetExtension(e.Name) == ".jpg")
var ext = Path.GetExtension(e.Name);
if (ext == ".jpg")
{
Log.Information("{Name} 文件转发到服务器", e.Name);
OcrRequest result = new OcrRequest(e.FullPath)
......@@ -124,7 +135,11 @@ async void Watcher_Changed(object sender, FileSystemEventArgs e)
};
await OcrServer.UploadFile(result);
}
FileChangeCnt.Remove(e.Name);
if (ext == ".txt")
{
Log.Information("{Name} 读码文件,数据为:", e.Name);
Log.Information(File.ReadAllText(e.FullPath));
}
}
void OnFileCreated(object sender, FileSystemEventArgs e)
......
......@@ -10,5 +10,7 @@ tenantId=0
OCR_SERVER_URL=https://hardware-test.guke.tech
OCR_API_URL=/api/service/ocr/v2/recognize
Ip=127.0.0.1
Port=9527
Port=21
DIR=Image
Upload=True
Anonymous=False
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment