上传新增加jwt验证
This commit is contained in:
parent
1de4685ee5
commit
77eddb3bc1
@ -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<OptionsSetting> options)
|
||||
private IWebHostEnvironment WebHostEnvironment;
|
||||
private ISysFileService SysFileService;
|
||||
public CommonController(IOptions<OptionsSetting> 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 上传
|
||||
|
||||
/// <summary>
|
||||
/// 存储文件
|
||||
/// </summary>
|
||||
/// <param name="formFile"></param>
|
||||
/// <returns></returns>
|
||||
[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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 存储文件到阿里云
|
||||
/// </summary>
|
||||
/// <param name="formFile"></param>
|
||||
/// <returns></returns>
|
||||
[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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user