diff --git a/ZR.Admin.WebApi/Controllers/CommonController.cs b/ZR.Admin.WebApi/Controllers/CommonController.cs index 39225ea..591d78f 100644 --- a/ZR.Admin.WebApi/Controllers/CommonController.cs +++ b/ZR.Admin.WebApi/Controllers/CommonController.cs @@ -1,12 +1,17 @@ using Infrastructure; using Infrastructure.Attribute; using Infrastructure.Model; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Newtonsoft.Json; using System; +using System.IO; +using System.Linq; using ZR.Admin.WebApi.Filters; using ZR.Common; +using ZR.Service.System.IService; namespace ZR.Admin.WebApi.Controllers { @@ -19,8 +24,12 @@ namespace ZR.Admin.WebApi.Controllers private OptionsSetting OptionsSetting; private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); - public CommonController(IOptions options) + private IWebHostEnvironment WebHostEnvironment; + private ISysFileService SysFileService; + public CommonController(IOptions options, IWebHostEnvironment webHostEnvironment, ISysFileService fileService) { + WebHostEnvironment = webHostEnvironment; + SysFileService = fileService; OptionsSetting = options.Value; } @@ -60,5 +69,67 @@ namespace ZR.Admin.WebApi.Controllers return SUCCESS(true); } + + #region 上传 + + /// + /// 存储文件 + /// + /// + /// + [HttpPost()] + [Verify] + [ActionPermissionFilter(Permission = "system")] + public IActionResult UploadFile([FromForm(Name = "file")] IFormFile formFile) + { + 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); + finalFilePath = finalFilePath.Replace("\\", "/").Replace("//", "/"); + + if (!Directory.Exists(Path.GetDirectoryName(finalFilePath))) + { + Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath)); + } + + using (var stream = new FileStream(finalFilePath, FileMode.Create)) + { + formFile.CopyTo(stream); + } + + string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{FileUtil.GetdirPath("uploads").Replace("\\", " /")}{fileName}"; + return ToResponse(ResultCode.SUCCESS, accessPath); + } + + /// + /// 存储文件到阿里云 + /// + /// + /// + [HttpPost] + [Verify] + [ActionPermissionFilter(Permission = "system")] + public IActionResult UploadFileAliyun([FromForm(Name = "file")] IFormFile formFile) + { + 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" }; + int MaxContentLength = 1024 * 1024 * 4; + + if (!AllowedFileExtensions.Contains(fileExt)) + { + return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型"); + } + + if (formFile.Length > MaxContentLength) + { + return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + (MaxContentLength / 1024).ToString() + " MB"); + } + (bool, string) result = SysFileService.SaveFile("", formFile); + + return ToResponse(ResultCode.SUCCESS, result.Item2); + } + #endregion } }