|
|
|
@ -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,11 +49,15 @@ 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);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(uploadUrl))
|
|
|
|
|
{
|
|
|
|
|
var phicyPath = Path.Combine(this._host.WebRootPath, "upload");
|
|
|
|
|
Directory.CreateDirectory(phicyPath);
|
|
|
|
|
var name = string.Format(CultureInfo.CurrentCulture, "{0}.{1}", md5, ext);
|
|
|
|
@ -67,6 +78,22 @@ namespace Infrastructure.Web.Mvc
|
|
|
|
|
|
|
|
|
|
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}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region private
|
|
|
|
|