新增加文件存储
This commit is contained in:
@@ -9,8 +9,10 @@ using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Common;
|
||||
using ZR.Model.System;
|
||||
using ZR.Service.System.IService;
|
||||
|
||||
namespace ZR.Admin.WebApi.Controllers
|
||||
@@ -82,17 +84,20 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
/// 存储文件
|
||||
/// </summary>
|
||||
/// <param name="formFile"></param>
|
||||
/// <param name="fileDir">存储目录</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost()]
|
||||
[Verify]
|
||||
[ActionPermissionFilter(Permission = "common")]
|
||||
public IActionResult UploadFile([FromForm(Name = "file")] IFormFile formFile)
|
||||
public IActionResult UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileDir = "uploads")
|
||||
{
|
||||
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
||||
string fileExt = Path.GetExtension(formFile.FileName);
|
||||
string fileName = FileUtil.HashFileName(Guid.NewGuid().ToString()).ToLower() + fileExt;
|
||||
string finalFilePath = Path.Combine(WebHostEnvironment.WebRootPath, FileUtil.GetdirPath("uploads"), fileName);
|
||||
string filePath = FileUtil.GetdirPath(fileDir);
|
||||
string finalFilePath = Path.Combine(WebHostEnvironment.WebRootPath, filePath, fileName);
|
||||
finalFilePath = finalFilePath.Replace("\\", "/").Replace("//", "/");
|
||||
double fileSize = formFile.Length / 1024;
|
||||
|
||||
if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
|
||||
{
|
||||
@@ -104,11 +109,24 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
formFile.CopyTo(stream);
|
||||
}
|
||||
|
||||
string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{FileUtil.GetdirPath("uploads").Replace("\\", " /")}{fileName}";
|
||||
string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{filePath.Replace("\\", " /")}{fileName}";
|
||||
SysFile file = new()
|
||||
{
|
||||
AccessUrl = accessPath,
|
||||
Create_by = HttpContext.GetName(),
|
||||
FileExt = fileExt,
|
||||
FileName = fileName,
|
||||
FileSize = fileSize + "kb",
|
||||
StoreType = 1,
|
||||
FileUrl = finalFilePath,
|
||||
Create_time = DateTime.Now
|
||||
};
|
||||
long fileId = SysFileService.InsertFile(file);
|
||||
return ToResponse(ResultCode.SUCCESS, new
|
||||
{
|
||||
url = accessPath,
|
||||
fileName
|
||||
fileName,
|
||||
fileId
|
||||
});
|
||||
}
|
||||
|
||||
@@ -127,7 +145,7 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
string fileExt = Path.GetExtension(formFile.FileName);
|
||||
string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".jpeg", ".webp", ".svga", ".xls" };
|
||||
int MaxContentLength = 1024 * 1024 * 5;
|
||||
|
||||
double fileSize = formFile.Length / 1024;
|
||||
if (!AllowedFileExtensions.Contains(fileExt))
|
||||
{
|
||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型");
|
||||
@@ -138,11 +156,21 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + (MaxContentLength / 1024).ToString() + " MB");
|
||||
}
|
||||
(bool, string, string) result = SysFileService.SaveFile(fileDir, formFile);
|
||||
|
||||
long fileId = SysFileService.InsertFile(new SysFile()
|
||||
{
|
||||
AccessUrl = result.Item2,
|
||||
Create_by = HttpContext.GetName(),
|
||||
FileExt = fileExt,
|
||||
FileName = result.Item3,
|
||||
FileSize = fileSize + "kb",
|
||||
StoreType = 2,
|
||||
StorePath = fileDir
|
||||
});
|
||||
return ToResponse(ResultCode.SUCCESS, new
|
||||
{
|
||||
url = result.Item2,
|
||||
fileName = result.Item3
|
||||
fileName = result.Item3,
|
||||
fileId
|
||||
});
|
||||
}
|
||||
#endregion
|
||||
|
||||
163
ZR.Admin.WebApi/Controllers/System/SysFileController.cs
Normal file
163
ZR.Admin.WebApi/Controllers/System/SysFileController.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlSugar;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Attribute;
|
||||
using Infrastructure.Enums;
|
||||
using Infrastructure.Model;
|
||||
using Mapster;
|
||||
using ZR.Admin.WebApi.Extensions;
|
||||
using ZR.Admin.WebApi.Filters;
|
||||
using ZR.Common;
|
||||
using ZR.Model.System;
|
||||
using ZR.Service.System.IService;
|
||||
using ZR.Model.System.Dto;
|
||||
|
||||
namespace ZR.Admin.WebApi.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件存储Controller
|
||||
/// </summary>
|
||||
[Verify]
|
||||
[Route("tool/file")]
|
||||
public class SysFileController : BaseController
|
||||
{
|
||||
/// <summary>
|
||||
/// 文件存储接口
|
||||
/// </summary>
|
||||
private readonly ISysFileService _SysFileService;
|
||||
|
||||
public SysFileController(ISysFileService SysFileService)
|
||||
{
|
||||
_SysFileService = SysFileService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询文件存储列表
|
||||
/// </summary>
|
||||
/// <param name="parm"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("list")]
|
||||
[ActionPermissionFilter(Permission = "tool:file:list")]
|
||||
public IActionResult QuerySysFile([FromQuery] SysFileQueryDto parm)
|
||||
{
|
||||
//开始拼装查询条件
|
||||
var predicate = Expressionable.Create<SysFile>();
|
||||
//搜索条件查询语法参考Sqlsugar
|
||||
predicate = predicate.AndIF(parm.BeginCreate_time != null, it => it.Create_time >= parm.BeginCreate_time);
|
||||
predicate = predicate.AndIF(parm.EndCreate_time != null, it => it.Create_time <= parm.EndCreate_time);
|
||||
predicate = predicate.AndIF(parm.StoreType != null, m => m.StoreType == parm.StoreType);
|
||||
|
||||
//搜索条件查询语法参考Sqlsugar
|
||||
var response = _SysFileService.GetPages(predicate.ToExpression(), parm);
|
||||
return SUCCESS(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询文件存储详情
|
||||
/// </summary>
|
||||
/// <param name="Id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{Id}")]
|
||||
[ActionPermissionFilter(Permission = "tool:file:query")]
|
||||
public IActionResult GetSysFile(int 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>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("{ids}")]
|
||||
[ActionPermissionFilter(Permission = "tool:file:delete")]
|
||||
[Log(Title = "文件存储", BusinessType = BusinessType.DELETE)]
|
||||
public IActionResult DeleteSysFile(string ids)
|
||||
{
|
||||
int[] idsArr = Tools.SpitIntArrary(ids);
|
||||
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
|
||||
|
||||
var response = _SysFileService.Delete(idsArr);
|
||||
//TODO 删除本地资源
|
||||
|
||||
return ToResponse(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文件存储导出
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Log(BusinessType = BusinessType.EXPORT, IsSaveResponseData = false, Title = "文件存储")]
|
||||
[HttpGet("export")]
|
||||
[ActionPermissionFilter(Permission = "tool:file:export")]
|
||||
public IActionResult Export()
|
||||
{
|
||||
var list = _SysFileService.GetAll();
|
||||
|
||||
string sFileName = ExportExcel(list, "SysFile", "文件存储");
|
||||
return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,10 +55,9 @@
|
||||
<!--<logger name="System.*" writeTo="blackhole" final="true" />-->
|
||||
<!-- Quartz -->
|
||||
<logger name="Quartz*" minlevel="Trace" maxlevel="Info" final="true" />
|
||||
<logger name="*" minLevel="Debug" writeTo="console"/>
|
||||
<logger name="ZR.Admin.WebApi.Startup" final="true" writeTo="sqlfile"/>
|
||||
|
||||
<logger name="*" minLevel="Debug" writeTo="console"/>
|
||||
<!--所有日志都写入到控制台-->
|
||||
<logger name="*" minLevel="Trace" writeTo="allfile" />
|
||||
<!--Skip non-critical Microsoft logs and so log only own logs-->
|
||||
<logger name="Microsoft.*,Quartz.Core.QuartzSchedulerThread" maxlevel="Info" final="true" />
|
||||
|
||||
@@ -42,6 +42,7 @@ namespace ${options.ApiControllerNamespace}.Controllers
|
||||
/// <summary>
|
||||
/// 查询${genTable.FunctionName}列表
|
||||
/// </summary>
|
||||
/// <param name="parm"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("list")]
|
||||
[ActionPermissionFilter(Permission = "${replaceDto.PermissionPrefix}:list")]
|
||||
|
||||
Reference in New Issue
Block a user