文件上传加密及增加根据配置文件存储类型自动选择上传方式

This commit is contained in:
2023-11-22 10:29:07 +08:00
parent 43c9d3564b
commit bebcf559a1
8 changed files with 379 additions and 64 deletions
@@ -1,15 +1,10 @@
using System.Net;
using System.Net.Sockets;
using Infrastructure.Helper;
using Infrastructure.Helper;
using JinianNet.JNTemplate;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using ZR.Admin.WebApi.Filters;
using ZR.Model.System;
using ZR.Service.IService;
using ZR.Service.System;
using ZR.Service.System.IService;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Services.IService;
@@ -135,15 +130,6 @@ namespace ZR.Admin.WebApi.Controllers
SysFile file = new();
string fileExt = Path.GetExtension(formFile.FileName);//文件后缀
double fileSize = Math.Round(formFile.Length / 1024.0, 2);//文件大小KB
var scheme = HttpContext.Request.Scheme + "://";
var serverIP = HttpContext.Request.Host.Value;
if (WebHostEnvironment.IsProduction())
{
var host = await Dns.GetHostEntryAsync(Dns.GetHostName());
var ip = host.AddressList
.FirstOrDefault(it => it.AddressFamily == AddressFamily.InterNetwork);
serverIP = ip + ":" + Request.HttpContext.Connection.LocalPort;//获取服务器IP
}
if (OptionsSetting.Upload.NotAllowedExt.Contains(fileExt))
{
@@ -166,7 +152,7 @@ namespace ZR.Admin.WebApi.Controllers
uploadDto.FileDir = OptionsSetting.Upload.LocalSavePath;
}
file = await SysFileService.SaveFileToLocal(savePath, uploadDto.FileName, uploadDto.FileDir,
HttpContext.GetUId(), HttpContext.GetName(), formFile, scheme + serverIP);
HttpContext.GetUId(), HttpContext.GetNickName(), formFile, OptionsSetting.Upload.UploadUrl);
break;
case StoreType.REMOTE:
break;
@@ -181,7 +167,7 @@ namespace ZR.Admin.WebApi.Controllers
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + AlimaxContentLength + " MB");
}
file = new(formFile.FileName, uploadDto.FileName, fileExt, fileSize + "kb",
uploadDto.FileDir, HttpContext.GetUId(), HttpContext.GetName())
uploadDto.FileDir, HttpContext.GetUId(), HttpContext.GetNickName())
{
StoreType = (int)StoreType.ALIYUN,
FileType = formFile.ContentType
@@ -1,8 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using ZR.Admin.WebApi.Filters;
using ZR.Model.System;
using ZR.Service.System.IService;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Model.Dto;
using ZR.ServiceCore.Services.IService;
@@ -20,11 +17,11 @@ namespace ZR.Admin.WebApi.Controllers
/// <summary>
/// 文件存储接口
/// </summary>
private readonly ISysFileService _SysFileService;
private readonly ISysFileService _sysFileService;
public SysFileController(ISysFileService SysFileService)
public SysFileController(ISysFileService sysFileService)
{
_SysFileService = SysFileService;
_sysFileService = sysFileService;
}
/// <summary>
@@ -43,7 +40,7 @@ namespace ZR.Admin.WebApi.Controllers
predicate = predicate.AndIF(parm.StoreType != null, m => m.StoreType == parm.StoreType);
predicate = predicate.AndIF(parm.FileId != null, m => m.Id == parm.FileId);
var response = _SysFileService.GetPages(predicate.ToExpression(), parm, x => x.Id, OrderByType.Desc);
var response = _sysFileService.GetPages(predicate.ToExpression(), parm, x => x.Id, OrderByType.Desc);
return SUCCESS(response);
}
@@ -56,7 +53,7 @@ namespace ZR.Admin.WebApi.Controllers
[ActionPermissionFilter(Permission = "tool:file:query")]
public IActionResult GetSysFile(long Id)
{
var response = _SysFileService.GetFirst(x => x.Id == Id);
var response = _sysFileService.GetFirst(x => x.Id == Id);
return SUCCESS(response);
}
@@ -68,13 +65,12 @@ namespace ZR.Admin.WebApi.Controllers
[HttpDelete("{ids}")]
[ActionPermissionFilter(Permission = "tool:file:delete")]
[Log(Title = "文件存储", BusinessType = BusinessType.DELETE)]
public IActionResult DeleteSysFile(string ids)
public async Task<IActionResult> DeleteSysFile(string ids)
{
long[] idsArr = Tools.SpitLongArrary(ids);
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
var response = _SysFileService.Delete(idsArr);
//TODO 删除本地资源
var response = await _sysFileService.DeleteSysFileAsync(idsArr);
return ToResponse(response);
}
@@ -88,10 +84,50 @@ namespace ZR.Admin.WebApi.Controllers
[ActionPermissionFilter(Permission = "tool:file:export")]
public IActionResult Export()
{
var list = _SysFileService.GetAll();
var list = _sysFileService.GetAll();
string sFileName = ExportExcel(list, "SysFile", "文件存储");
return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName });
}
/// <summary>
/// 通过文件ID下载文件
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
[HttpPost("download")]
public IActionResult Download([FromBody] SysFile file)
{
var sysFile = _sysFileService.Queryable().Where(it => it.Id == file.Id).First();
if (sysFile == null)
{
return NoContent();
}
Stream? stream = null;
switch ((StoreType)Enum.Parse(typeof(StoreType), sysFile.StoreType.ToString() ?? string.Empty))
{
case StoreType.LOCAL:
stream = sysFile.IsEncrypted == "1" ? _sysFileService.DecryptSysFileStream(sysFile.FileUrl)
: new FileStream(sysFile.FileUrl, FileMode.Open);
break;
case StoreType.ALIYUN:
stream = AliyunOssHelper.DownloadFile(sysFile.FileUrl, "", sysFile.IsEncrypted == "1");
break;
default:
throw new CustomException("不支持的存储类型");
}
if (stream != null)
{
return new FileStreamResult(stream, "application/octet-stream")
{
FileDownloadName = sysFile.RealName
};
}
else
{
return NoContent();
}
}
}
}
@@ -1,8 +1,5 @@
using System.Net;
using System.Net.Sockets;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using ZR.Admin.WebApi.Filters;
using ZR.Model.System;
using ZR.Service.System.IService;
using ZR.ServiceCore.Model;
using ZR.ServiceCore.Model.Dto;
@@ -24,6 +21,7 @@ namespace ZR.Admin.WebApi.Controllers.System
private readonly ISysDeptService DeptService;
private readonly ISysFileService FileService;
private IWebHostEnvironment hostEnvironment;
private readonly OptionsSetting _optionsSetting;
public SysProfileController(
ISysUserService userService,
@@ -31,7 +29,8 @@ namespace ZR.Admin.WebApi.Controllers.System
ISysUserPostService postService,
ISysDeptService deptService,
ISysFileService sysFileService,
IWebHostEnvironment hostEnvironment)
IWebHostEnvironment hostEnvironment,
IOptions<OptionsSetting> options)
{
UserService = userService;
RoleService = roleService;
@@ -39,6 +38,7 @@ namespace ZR.Admin.WebApi.Controllers.System
DeptService = deptService;
FileService = sysFileService;
this.hostEnvironment = hostEnvironment;
_optionsSetting = options.Value;
}
/// <summary>
@@ -122,19 +122,10 @@ namespace ZR.Admin.WebApi.Controllers.System
{
long userId = HttpContext.GetUId();
if (formFile == null) throw new CustomException("请选择文件");
var scheme = HttpContext.Request.Scheme + "://";
var serverIP = HttpContext.Request.Host.Value;
if (hostEnvironment.IsProduction())
{
var host = await Dns.GetHostEntryAsync(Dns.GetHostName());
var ip = host.AddressList
.FirstOrDefault(it => it.AddressFamily == AddressFamily.InterNetwork);
serverIP = ip + ":" + Request.HttpContext.Connection.LocalPort;//获取服务器IP
}
var file = await FileService.SaveFileToLocal(hostEnvironment.WebRootPath, "", "avatar",
HttpContext.GetUId(), HttpContext.GetName(), formFile, serverIP);
HttpContext.GetUId(), HttpContext.GetName(), formFile, _optionsSetting.Upload.UploadUrl);
UserService.UpdatePhoto(new SysUser() { Avatar = file.AccessUrl, UserId = userId });
return SUCCESS(new { imgUrl = file.AccessUrl });