1+ using GeneralUpdate . AspNetCore . DTO ;
2+ using GeneralUpdate . AspNetCore . Hubs ;
3+ using GeneralUpdate . AspNetCore . Services ;
4+ using GeneralUpdate . Core . Domain . DTO ;
5+ using GeneralUpdate . Core . Domain . Enum ;
6+ using Microsoft . AspNetCore . SignalR ;
7+ using Newtonsoft . Json ;
8+
19var builder = WebApplication . CreateBuilder ( args ) ;
10+ builder . Services . AddSingleton < IUpdateService , GeneralUpdateService > ( ) ;
11+ builder . Services . AddSignalR ( ) ;
212var app = builder . Build ( ) ;
313
4- app . MapGet ( "/" , ( ) => "Hello World!" ) ;
14+ /**
15+ * Push the latest version information in real time.
16+ */
17+ app . MapHub < VersionHub > ( "/versionhub" ) ;
18+
19+ app . MapPost ( "/push" , async Task < string > ( HttpContext context ) =>
20+ {
21+ try
22+ {
23+ var hubContext = context . RequestServices . GetRequiredService < IHubContext < VersionHub > > ( ) ;
24+ await hubContext . SendMessage ( "TESTNAME" , "123" ) ;
25+ }
26+ catch ( Exception ex )
27+ {
28+ return ex . Message ;
29+ }
30+ return "OK" ;
31+ } ) ;
32+
33+ /**
34+ * Check if an update is required.
35+ */
36+ app . MapGet ( "/versions/{clientType}/{clientVersion}/{clientAppKey}" , ( int clientType , string clientVersion , string clientAppKey , IUpdateService updateService ) =>
37+ {
38+ var versions = new List < VersionDTO > ( ) ;
39+ var md5 = "b03d52c279faf003965c46041f2037f9" ;
40+ var pubTime = new DateTimeOffset ( DateTime . UtcNow ) . ToUnixTimeSeconds ( ) ;
41+ string version = null ;
42+ if ( clientType == AppType . ClientApp )
43+ {
44+ //client
45+ //version = "0.0.0.0";
46+ version = "9.9.9.9" ;
47+ }
48+ else if ( clientType == AppType . UpgradeApp )
49+ {
50+ //upgrad
51+ //version = "0.0.0.0";
52+ version = "9.9.9.9" ;
53+ }
54+ var url = $ "http://192.168.50.203/testpacket.zip";
55+ var name = "testpacket" ;
56+ versions . Add ( new VersionDTO ( md5 , pubTime , version , url , name ) ) ;
57+ return updateService . Update ( clientType , clientVersion , version , clientAppKey , GetAppSecretKey ( ) , false , versions ) ;
58+ } ) ;
59+
60+ /**
61+ * Upload update package.
62+ */
63+ app . MapPost ( "/upload" , async Task < string > ( HttpContext context , HttpRequest request ) =>
64+ {
65+ var uploadReapDTO = new UploadReapDTO ( ) ;
66+ try
67+ {
68+ var contextReq = context . Request ;
69+ int . TryParse ( contextReq . Form [ "clientType" ] , out int clientType ) ;
70+ var version = contextReq . Form [ "clientType" ] . ToString ( ) ;
71+ var clientAppKey = contextReq . Form [ "clientAppKey" ] . ToString ( ) ;
72+ var md5 = contextReq . Form [ "md5" ] . ToString ( ) ;
73+
74+ if ( ! request . HasFormContentType ) throw new Exception ( "ContentType was not included in the request !" ) ;
75+ var form = await request . ReadFormAsync ( ) ;
76+
77+ var formFile = form . Files [ "file" ] ;
78+ if ( formFile is null || formFile . Length == 0 ) throw new ArgumentNullException ( "Uploaded update package file not found !" ) ;
79+ await using var stream = formFile . OpenReadStream ( ) ;
80+ byte [ ] buffer = new byte [ stream . Length ] ;
81+ stream . Read ( buffer , 0 , buffer . Length ) ;
82+ //TODO:save to file server.
83+ string localPath = $ "E:\\ { formFile . FileName } ";
84+ await using var fileStream = new FileStream ( localPath , FileMode . CreateNew , FileAccess . Write ) ;
85+ fileStream . Write ( buffer , 0 , buffer . Length ) ;
86+
87+ //TODO: data persistence.To mysql , sqlserver....
88+
89+ uploadReapDTO . Code = HttpStatus . OK ;
90+ uploadReapDTO . Body = "Published successfully." ;
91+ uploadReapDTO . Message = RespMessage . RequestSucceeded ;
92+ return JsonConvert . SerializeObject ( uploadReapDTO ) ;
93+ }
94+ catch ( Exception ex )
95+ {
96+ uploadReapDTO . Code = HttpStatus . BAD_REQUEST ;
97+ uploadReapDTO . Body = $ "Failed to publish ! Because : { ex . Message } ";
98+ uploadReapDTO . Message = RespMessage . RequestFailed ;
99+ return JsonConvert . SerializeObject ( uploadReapDTO ) ;
100+ }
101+ } ) ;
5102
6103app . Run ( ) ;
104+
105+ string GetAppSecretKey ( )
106+ {
107+ return "B8A7FADD-386C-46B0-B283-C9F963420C7C" ;
108+ }
0 commit comments