优化文件存储
This commit is contained in:
@@ -7,9 +7,11 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using Snowflake.Core;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Common;
|
||||
@@ -158,25 +160,29 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
[HttpPost]
|
||||
[Verify]
|
||||
[ActionPermissionFilter(Permission = "common")]
|
||||
public IActionResult UploadFileAliyun([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "")
|
||||
public async Task<IActionResult> UploadFileAliyun([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "")
|
||||
{
|
||||
if (fileDir.IsEmpty()) fileDir = "uploads";
|
||||
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
||||
string fileExt = Path.GetExtension(formFile.FileName);
|
||||
string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".jpeg", ".webp", ".svga", ".xls", ".doc", ".zip", ".json", ".txt", ".bundle" };
|
||||
int MaxContentLength = 1024 * 1024 * 15;
|
||||
double fileSize = formFile.Length / 1024;
|
||||
if (!AllowedFileExtensions.Contains(fileExt))
|
||||
string fileExt = Path.GetExtension(formFile.FileName);//文件后缀
|
||||
double fileSize = formFile.Length / 1024.0;//文件大小KB
|
||||
string[] NotAllowedFileExtensions = new string[] { ".bat", ".exe", ".jar", ".js" };
|
||||
int MaxContentLength = 15;
|
||||
if (NotAllowedFileExtensions.Contains(fileExt))
|
||||
{
|
||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型");
|
||||
}
|
||||
|
||||
if (formFile.Length > MaxContentLength)
|
||||
if ((fileSize / 1024) > MaxContentLength)
|
||||
{
|
||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + (MaxContentLength / 1024).ToString() + " MB");
|
||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + MaxContentLength + " MB");
|
||||
}
|
||||
|
||||
(bool, string, string) result = SysFileService.SaveFile(fileDir, formFile, fileName);
|
||||
long fileId = SysFileService.InsertFile(new SysFile()
|
||||
(bool, string, string) result = new();
|
||||
await Task.Run(() =>
|
||||
{
|
||||
result = SysFileService.SaveFile(fileDir, formFile, fileName, "");
|
||||
});
|
||||
long id = SysFileService.InsertFile(new SysFile()
|
||||
{
|
||||
AccessUrl = result.Item2,
|
||||
Create_by = HttpContext.GetName(),
|
||||
@@ -193,7 +199,7 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
url = result.Item2,
|
||||
fileName = result.Item3,
|
||||
fileId
|
||||
fileId = id
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -57,74 +57,13 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
/// <returns></returns>
|
||||
[HttpGet("{Id}")]
|
||||
[ActionPermissionFilter(Permission = "tool:file:query")]
|
||||
public IActionResult GetSysFile(int Id)
|
||||
public IActionResult GetSysFile(long Id)
|
||||
{
|
||||
var response = _SysFileService.GetFirst(x => x.Id == Id);
|
||||
|
||||
return SUCCESS(response);
|
||||
}
|
||||
|
||||
///// <summary>
|
||||
///// 添加文件存储
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
//[HttpPost]
|
||||
//[ActionPermissionFilter(Permission = "tool:file:add")]
|
||||
//[Log(Title = "文件存储", BusinessType = BusinessType.INSERT)]
|
||||
//public IActionResult AddSysFile([FromBody] SysFileDto parm)
|
||||
//{
|
||||
// if (parm == null)
|
||||
// {
|
||||
// throw new CustomException("请求参数错误");
|
||||
// }
|
||||
// //从 Dto 映射到 实体
|
||||
// var model = parm.Adapt<SysFile>().ToCreate(HttpContext);
|
||||
|
||||
// var response = _SysFileService.Insert(model, it => new
|
||||
// {
|
||||
// it.FileName,
|
||||
// it.FileUrl,
|
||||
// it.StorePath,
|
||||
// it.FileSize,
|
||||
// it.FileExt,
|
||||
// it.Create_by,
|
||||
// it.Create_time,
|
||||
// it.StoreType,
|
||||
// it.AccessUrl,
|
||||
// });
|
||||
// return ToResponse(response);
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// 更新文件存储
|
||||
///// </summary>
|
||||
///// <returns></returns>
|
||||
//[HttpPut]
|
||||
//[ActionPermissionFilter(Permission = "tool:file:update")]
|
||||
//[Log(Title = "文件存储", BusinessType = BusinessType.UPDATE)]
|
||||
//public IActionResult UpdateSysFile([FromBody] SysFileDto parm)
|
||||
//{
|
||||
// if (parm == null)
|
||||
// {
|
||||
// throw new CustomException("请求实体不能为空");
|
||||
// }
|
||||
// //从 Dto 映射到 实体
|
||||
// var model = parm.Adapt<SysFile>().ToUpdate(HttpContext);
|
||||
|
||||
// var response = _SysFileService.Update(w => w.Id == model.Id, it => new SysFile()
|
||||
// {
|
||||
// //Update 字段映射
|
||||
// FileUrl = model.FileUrl,
|
||||
// StorePath = model.StorePath,
|
||||
// FileSize = model.FileSize,
|
||||
// FileExt = model.FileExt,
|
||||
// StoreType = model.StoreType,
|
||||
// AccessUrl = model.AccessUrl,
|
||||
// });
|
||||
|
||||
// return ToResponse(response);
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 删除文件存储
|
||||
/// </summary>
|
||||
@@ -134,7 +73,7 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
[Log(Title = "文件存储", BusinessType = BusinessType.DELETE)]
|
||||
public IActionResult DeleteSysFile(string ids)
|
||||
{
|
||||
int[] idsArr = Tools.SpitIntArrary(ids);
|
||||
long[] idsArr = Tools.SpitLongArrary(ids);
|
||||
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
|
||||
|
||||
var response = _SysFileService.Delete(idsArr);
|
||||
|
||||
Reference in New Issue
Block a user