diff --git a/docs/研发/使用分布式文件系统分析.xmind b/docs/研发/使用分布式文件系统分析.xmind index 44d1a01b..d399d958 100644 Binary files a/docs/研发/使用分布式文件系统分析.xmind and b/docs/研发/使用分布式文件系统分析.xmind differ diff --git a/docs/研发/技术选型.xmind b/docs/研发/技术选型.xmind index 9296cffe..afea30ba 100644 Binary files a/docs/研发/技术选型.xmind and b/docs/研发/技术选型.xmind differ diff --git a/projects/Infrastructure/Web/Mvc/FileController.cs b/projects/Infrastructure/Web/Mvc/FileController.cs index b31e614b..416336a3 100644 --- a/projects/Infrastructure/Web/Mvc/FileController.cs +++ b/projects/Infrastructure/Web/Mvc/FileController.cs @@ -1,22 +1,29 @@ +using Infrastructure.Extensions; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; using System; using System.Globalization; using System.IO; using System.IO.Compression; +using System.Net.Http; using System.Security.Cryptography; -using Infrastructure.Extensions; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; namespace Infrastructure.Web.Mvc { public class FileController : Controller { private readonly IWebHostEnvironment _host; + private readonly IConfiguration _cfg; + private readonly IHttpClientFactory _httpClientFactory; - public FileController(IWebHostEnvironment host) + public FileController(IWebHostEnvironment host, IConfiguration cfg, IHttpClientFactory httpClientFactory) { this._host = host; + this._cfg = cfg; + this._httpClientFactory = httpClientFactory; } public IActionResult Upload() @@ -42,30 +49,50 @@ namespace Infrastructure.Web.Mvc { throw new ArgumentNullException(nameof(file)); } + var uploadUrl = this._cfg.GetSection("AppSettings").GetValue("upload"); using (Stream stream = file.OpenReadStream()) { var ext = GetExtension(file.FileName); var md5 = GetFileNameHash(stream); - var phicyPath = Path.Combine(this._host.WebRootPath, "upload"); - Directory.CreateDirectory(phicyPath); - var name = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", md5, ext); - var fullName = Path.Combine(phicyPath, name); - if (!System.IO.File.Exists(fullName)) + + if (string.IsNullOrEmpty(uploadUrl)) { - using (FileStream fs = System.IO.File.Create(fullName)) - { - file.CopyTo(fs); - } - if (ext == "zip") + var phicyPath = Path.Combine(this._host.WebRootPath, "upload"); + Directory.CreateDirectory(phicyPath); + var name = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", md5, ext); + var fullName = Path.Combine(phicyPath, name); + if (!System.IO.File.Exists(fullName)) { - var zipDirectory = Path.Combine(phicyPath, md5); - Directory.CreateDirectory(Path.Combine(phicyPath, md5)); - ZipFile.ExtractToDirectory(fullName, zipDirectory); + using (FileStream fs = System.IO.File.Create(fullName)) + { + file.CopyTo(fs); + } + if (ext == "zip") + { + var zipDirectory = Path.Combine(phicyPath, md5); + Directory.CreateDirectory(Path.Combine(phicyPath, md5)); + ZipFile.ExtractToDirectory(fullName, zipDirectory); + } } - } - return $"/upload/{name}"; + return $"/upload/{name}"; + } + else + { + var hc = this._httpClientFactory.CreateClient(); + var form = new MultipartFormDataContent(); + stream.Seek(0, SeekOrigin.Begin); + var bytes = new byte[stream.Length]; + stream.Read(bytes, 0, bytes.Length); + form.Add(new ByteArrayContent(bytes), "file", file.FileName); + form.Add(new StringContent("dfs"), "path"); + form.Add(new StringContent("json"), "output"); + var response = hc.PostAsync(uploadUrl, form).Result; + var result = response.Content.ReadAsStringAsync().Result; + var json = JsonConvert.DeserializeAnonymousType(result, new { url = "", md5 = "", path = "", domain = "", scene = "", size = 0, mtime = 0l, scenes = "", retmsg = "", retcode = -1, src = "" }); + return $"/dfs{json.src}"; + } } } diff --git a/projects/IoTCenter/appsettings.json b/projects/IoTCenter/appsettings.json index 491fc343..7b5cb9fc 100644 --- a/projects/IoTCenter/appsettings.json +++ b/projects/IoTCenter/appsettings.json @@ -13,6 +13,7 @@ }, "AppSettings": { "database": "postgresql", - "UseCookieSessionStore": false + "UseCookieSessionStore": false, + "upload": "http://10.10.24.104:8180/group1/upload" } } \ No newline at end of file diff --git a/tools/go-fastdfs/.gitignore b/tools/go-fastdfs/.gitignore new file mode 100644 index 00000000..541a934f --- /dev/null +++ b/tools/go-fastdfs/.gitignore @@ -0,0 +1,7 @@ +static +logs +log +files +data +conf +go-fastdfs-web \ No newline at end of file diff --git a/tools/fileserver.exe b/tools/go-fastdfs/fileserver.exe similarity index 100% rename from tools/fileserver.exe rename to tools/go-fastdfs/fileserver.exe diff --git a/tools/nginx-1.16.0/conf/nginx.conf b/tools/nginx-1.16.0/conf/nginx.conf index d958ee3d..973961b1 100644 --- a/tools/nginx-1.16.0/conf/nginx.conf +++ b/tools/nginx-1.16.0/conf/nginx.conf @@ -22,6 +22,11 @@ http { keepalive_timeout 65; + upstream go-fastdfs { + server 10.10.24.104:8180; + ip_hash; + } + server { listen 0.0.0.0:80; server_name localhost; @@ -30,6 +35,13 @@ http { proxy_pass http://localhost:8030/; } + location ^~ /dfs/ { + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_pass http://go-fastdfs/; + } + location ^~ /live/ { proxy_pass http://localhost:8080/live/; }