Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
何阳
/
CaptureAgent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
55c4137f
authored
Dec 21, 2023
by
何阳
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
NEW: 1. 增加是否上传选项,是否匿名选项
2. 增加简单账号登陆 3. 增加上传日志统计
parent
10ff1887
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
84 additions
and
29 deletions
FtpAgent/Config.cs
FtpAgent/CustomMembershipProvider.cs
FtpAgent/FtpAgent.csproj
FtpAgent/OcrServer.cs
FtpAgent/Program.cs
FtpAgent/config.ini
FtpAgent/Config.cs
View file @
55c4137f
...
...
@@ -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"
]);
}
}
}
FtpAgent/CustomMembershipProvider.cs
0 → 100644
View file @
55c4137f
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
));
}
}
}
FtpAgent/FtpAgent.csproj
View file @
55c4137f
...
...
@@ -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>
...
...
FtpAgent/OcrServer.cs
View file @
55c4137f
...
...
@@ -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
);
}
}
...
...
FtpAgent/Program.cs
View file @
55c4137f
...
...
@@ -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
)
...
...
FtpAgent/config.ini
View file @
55c4137f
...
...
@@ -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
DIR
=
Image
\ No newline at end of file
Port
=
21
DIR
=
Image
Upload
=
True
Anonymous
=
False
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment