优化头像上传

This commit is contained in:
不做码农 2022-03-29 16:45:11 +08:00
parent de43e4964e
commit dd22af8c4f

View File

@ -15,6 +15,8 @@ using ZR.Common;
using ZR.Model.System.Dto; using ZR.Model.System.Dto;
using ZR.Model.System; using ZR.Model.System;
using ZR.Service.System.IService; using ZR.Service.System.IService;
using Infrastructure.Extensions;
using System.Threading.Tasks;
namespace ZR.Admin.WebApi.Controllers.System namespace ZR.Admin.WebApi.Controllers.System
{ {
@ -27,6 +29,7 @@ namespace ZR.Admin.WebApi.Controllers.System
private readonly ISysRoleService RoleService; private readonly ISysRoleService RoleService;
private readonly ISysUserPostService UserPostService; private readonly ISysUserPostService UserPostService;
private readonly ISysDeptService DeptService; private readonly ISysDeptService DeptService;
private readonly ISysFileService FileService;
private OptionsSetting OptionsSetting; private OptionsSetting OptionsSetting;
private IWebHostEnvironment hostEnvironment; private IWebHostEnvironment hostEnvironment;
@ -35,6 +38,7 @@ namespace ZR.Admin.WebApi.Controllers.System
ISysRoleService roleService, ISysRoleService roleService,
ISysUserPostService postService, ISysUserPostService postService,
ISysDeptService deptService, ISysDeptService deptService,
ISysFileService sysFileService,
IOptions<OptionsSetting> options, IOptions<OptionsSetting> options,
IWebHostEnvironment hostEnvironment) IWebHostEnvironment hostEnvironment)
{ {
@ -42,6 +46,7 @@ namespace ZR.Admin.WebApi.Controllers.System
RoleService = roleService; RoleService = roleService;
UserPostService = postService; UserPostService = postService;
DeptService = deptService; DeptService = deptService;
FileService = sysFileService;
OptionsSetting = options.Value; OptionsSetting = options.Value;
this.hostEnvironment = hostEnvironment; this.hostEnvironment = hostEnvironment;
} }
@ -124,28 +129,17 @@ namespace ZR.Admin.WebApi.Controllers.System
[HttpPost("Avatar")] [HttpPost("Avatar")]
[ActionPermissionFilter(Permission = "common")] [ActionPermissionFilter(Permission = "common")]
[Log(Title = "修改头像", BusinessType = BusinessType.UPDATE, IsSaveRequestData = false)] [Log(Title = "修改头像", BusinessType = BusinessType.UPDATE, IsSaveRequestData = false)]
public IActionResult Avatar([FromForm(Name = "picture")] IFormFile formFile) public async Task<IActionResult> Avatar([FromForm(Name = "picture")] IFormFile formFile)
{ {
LoginUser loginUser = Framework.JwtUtil.GetLoginUser(HttpContext); LoginUser loginUser = Framework.JwtUtil.GetLoginUser(HttpContext);
if (formFile == null) throw new CustomException("请选择文件"); if (formFile == null) throw new CustomException("请选择文件");
string fileExt = Path.GetExtension(formFile.FileName); string fileExt = Path.GetExtension(formFile.FileName);
string savePath = Path.Combine(hostEnvironment.WebRootPath, FileUtil.GetdirPath("uploads")); string fileName = FileUtil.HashFileName() + (fileExt.IsEmpty() ? ".png" : fileExt);
SysFile file = await FileService.SaveFileToLocal(hostEnvironment.WebRootPath, fileName, "", HttpContext.GetName(), formFile);
if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } UserService.UpdatePhoto(new SysUser() { Avatar = file.AccessUrl, UserId = loginUser.UserId });
return SUCCESS(new { imgUrl = file.AccessUrl });
string fileName = FileUtil.HashFileName() + fileExt;
string finalFilePath = savePath + fileName;
using (var stream = new FileStream(finalFilePath, FileMode.Create))
{
formFile.CopyTo(stream);
}
string accessUrl = $"{OptionsSetting.Upload.UploadUrl}/{FileUtil.GetdirPath("uploads")}{fileName}";
UserService.UpdatePhoto(new SysUser() { Avatar = accessUrl, UserId = loginUser.UserId });
return SUCCESS(new { imgUrl = accessUrl });
} }
} }
} }