Former-commit-id: d18b4291b7c290cec9edd2600cc985fed8aa17dc
TangShanKaiPing
wanggang 5 years ago
parent 38f20ccb37
commit 4353abdb5d

Binary file not shown.

@ -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<string>("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}";
}
}
}

@ -13,6 +13,7 @@
},
"AppSettings": {
"database": "postgresql",
"UseCookieSessionStore": false
"UseCookieSessionStore": false,
"upload": "http://10.10.24.104:8180/group1/upload"
}
}

@ -0,0 +1,7 @@
static
logs
log
files
data
conf
go-fastdfs-web

@ -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/;
}

Loading…
Cancel
Save