Selaa lähdekoodia

完成代理服务-文件/图片协议

linhong 3 vuotta sitten
vanhempi
commit
f5f1744a08

+ 5 - 29
Controllers/L1SvrController.cs

@@ -13,6 +13,7 @@ using Newtonsoft.Json;
 using System.IO;
 using System.Text;
 using Newtonsoft.Json.Linq;
+using LJProxy.Services;
 
 namespace LJProxy.Controllers
 {
@@ -22,34 +23,11 @@ namespace LJProxy.Controllers
     {
         private static object _syncRoot = new object();
         public static AppSettings _appSettingModel;
-        public L1SvrController(IOptions<AppSettings> appSettingModel)
+        private LJClientPoolService _ljClient;
+        public L1SvrController(LJClientPoolService ljClient)
         {
-            if (_appSettingModel == null)
-            {
-                _appSettingModel = appSettingModel.Value;
-            }
+            _ljClient = ljClient;
         }
-        private static ILJClient _pool { get; set; }
-        private static ILJClient Pool
-        {
-            get
-            {
-                if (_pool == null)
-                {
-                    lock (_syncRoot)
-                    {
-                        var url = _appSettingModel.L1SvrUrl;
-                        var urlArr = url.Split(':');
-                        var ip = urlArr[0];
-                        var port = urlArr[1];
-                        var creator = new DirectP1ClientCreator(ip, Convert.ToInt32(port));
-                        _pool = new LJClientPool(creator, _appSettingModel.ThreadSize);
-                    }
-                }
-                return _pool;
-            }
-        }
-
 
         [Route("svr/{apiName}")]
         [HttpPost]
@@ -61,9 +39,7 @@ namespace LJProxy.Controllers
             {
                 requestBody = await reader.ReadToEndAsync();
             }
-            //var excuteResult = GlobalVar.Excute(apiName, requestBody, Request);
-            //if (excuteResult.Item1) return excuteResult.Item2;
-            var rslt = Pool.DoExcute(apiName, requestBody);
+            var rslt = _ljClient.Pool.DoExcute(apiName, requestBody);
             return Content(rslt, "application/json");
         }
     }

+ 18 - 0
Models/GetFile.cs

@@ -0,0 +1,18 @@
+using LJLib.Net.SPI.Com;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace LJProxy.Models
+{
+    public class GetFileRequest : LJRequest
+    {
+        public string Filepath { get; set; }
+    }
+
+    public class GetFileResponse : LJResponse
+    {
+        public byte[] FileData { get; set; }
+    }
+}

+ 1 - 1
Properties/PublishProfiles/FolderProfile.pubxml.user

@@ -5,6 +5,6 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <_PublishTargetUrl>F:\projects\LJProxy\bin\Release\netcoreapp3.1\publish\</_PublishTargetUrl>
-    <History>True|2021-09-06T06:54:37.4829251Z;True|2021-09-06T14:46:46.4478494+08:00;True|2021-09-05T10:02:29.7646709+08:00;True|2021-09-04T22:32:42.5817800+08:00;True|2021-08-16T14:51:54.8412175+08:00;True|2021-08-16T11:30:00.7278175+08:00;</History>
+    <History>True|2021-09-08T06:20:29.8634886Z;True|2021-09-08T14:16:10.8813557+08:00;True|2021-09-08T14:08:52.1069174+08:00;True|2021-09-06T14:54:37.4829251+08:00;True|2021-09-06T14:46:46.4478494+08:00;True|2021-09-05T10:02:29.7646709+08:00;True|2021-09-04T22:32:42.5817800+08:00;True|2021-08-16T14:51:54.8412175+08:00;True|2021-08-16T11:30:00.7278175+08:00;</History>
   </PropertyGroup>
 </Project>

+ 87 - 0
Providers/L1SvrFileInfo.cs

@@ -0,0 +1,87 @@
+using LJLib.Net.SPI.Client;
+using LJProxy.Models;
+using Microsoft.Extensions.FileProviders;
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace LJProxy.Providers
+{
+    public class L1SvrFileInfo : IFileInfo
+    {
+        private string _fullFilepath;
+        private string _subpath;
+        private FileInfo _file;
+        private Stream _stream;
+        private ILJClient _ljPool;
+        private static object _syncRoot = new object();
+        public L1SvrFileInfo(string fullFilepath, string subpath, ILJClient ljPool)
+        {
+            _fullFilepath = fullFilepath;
+            _subpath = subpath;
+            _ljPool = ljPool;
+            if (File.Exists(_fullFilepath))
+            {
+                _file = new FileInfo(_fullFilepath);
+                _stream = _file.OpenRead();
+            }
+            else
+            {
+                lock (_syncRoot)
+                {
+                    if (File.Exists(_fullFilepath))
+                    {
+                        _file = new FileInfo(_fullFilepath);
+                        _stream = _file.OpenRead();
+                    }
+                    else
+                    {
+                        GetRemoteFile();
+                        if (File.Exists(_fullFilepath))
+                        {
+                            _file = new FileInfo(_fullFilepath);
+                            _stream = _file.OpenRead();
+                        }
+                    }
+
+                }
+            }
+        }
+        public bool Exists => _file!=null;
+
+        public bool IsDirectory => false;
+
+        public DateTimeOffset LastModified => _file.LastWriteTime;
+
+        public long Length => _stream.Length;
+
+        public string Name => _file.Name;
+
+        public string PhysicalPath => _file.FullName;
+
+        public Stream CreateReadStream()
+        {
+            return _stream;
+        }
+
+        public void GetRemoteFile()
+        {
+            var getFileReq = new GetFileRequest() {
+                Filepath = _subpath
+            };
+            var responseStr = _ljPool.DoExcute(getFileReq.GetApiName(),JsonConvert.SerializeObject(getFileReq));
+            var response = JsonConvert.DeserializeObject<GetFileResponse>(responseStr);
+            if (string.IsNullOrEmpty(response.ErrMsg))
+            {
+                var fileDir = Path.GetDirectoryName(_fullFilepath);
+                if (!Directory.Exists(fileDir)){
+                    Directory.CreateDirectory(fileDir);
+                }
+                File.WriteAllBytes(_fullFilepath, response.FileData);
+            }
+        }
+    }
+}

+ 40 - 0
Providers/L1SvrFileProvider.cs

@@ -0,0 +1,40 @@
+using LJProxy.Services;
+using Microsoft.Extensions.FileProviders;
+using Microsoft.Extensions.Primitives;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace LJProxy.Providers
+{
+    public class L1SvrFileProvider : IFileProvider
+    {
+        private string _contentRoot;
+        private LJClientPoolService _ljClient;
+        public L1SvrFileProvider(string contentRoot, LJClientPoolService ljClient)
+        {
+            _contentRoot = contentRoot;
+            _ljClient = ljClient;
+        }
+
+        public IDirectoryContents GetDirectoryContents(string subpath)
+        {
+            throw new NotImplementedException();
+        }
+
+        public IFileInfo GetFileInfo(string subpath)
+        {
+            string physicSubpath = subpath.Replace("/","\\");
+            string directory = @$"{_contentRoot}\wwwroot";
+            var fullFilepath = @$"{directory}{physicSubpath}";
+            var result = new L1SvrFileInfo(fullFilepath, physicSubpath,_ljClient.Pool);
+            return result;
+        }
+
+        public IChangeToken Watch(string filter)
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

+ 33 - 0
Services/LJClientPoolService.cs

@@ -0,0 +1,33 @@
+using LJLib.Client;
+using LJLib.Net.SPI.Client;
+using LJProxy.Settings;
+using Microsoft.Extensions.Options;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace LJProxy.Services
+{
+    public class LJClientPoolService
+    {
+        private AppSettings _appSettingModel;
+        private ILJClient _pool;
+        public LJClientPoolService(IOptions<AppSettings> appSettingModel)
+        {
+            _appSettingModel = appSettingModel.Value;
+            var url = _appSettingModel.L1SvrUrl;
+            var urlArr = url.Split(':');
+            var ip = urlArr[0];
+            var port = urlArr[1];
+            var creator = new DirectP1ClientCreator(ip, Convert.ToInt32(port));
+            _pool = new LJClientPool(creator, _appSettingModel.ThreadSize);
+        }
+
+        public ILJClient Pool
+        {
+            get { return _pool; }
+        }
+
+    }
+}

+ 5 - 2
Startup.cs

@@ -1,3 +1,4 @@
+using LJProxy.Providers;
 using LJProxy.Services;
 using LJProxy.Settings;
 using Microsoft.AspNetCore.Builder;
@@ -31,6 +32,7 @@ namespace LJProxy
                 //opt.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
             });
             services.Configure<AppSettings>(Configuration.GetSection("Appsettings"));
+            services.AddSingleton<LJClientPoolService>();
             services.AddScoped<VersionService>();
             GlobalVar.InitSetting(Configuration.GetSection("Appsettings"));
         }
@@ -55,8 +57,9 @@ namespace LJProxy
             });
 
             app.UseDefaultFiles();
-
-            app.UseStaticFiles();
+            app.UseStaticFiles(new StaticFileOptions { 
+                FileProvider = new L1SvrFileProvider(env.ContentRootPath,app.ApplicationServices.GetService<LJClientPoolService>()),
+            });
         }
     }
 }