|
@@ -5,122 +5,108 @@ using System.Threading.Tasks;
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
using Admin.Core.Model.Output;
|
|
|
using Admin.Core.Attributes;
|
|
|
+using Admin.Core.Common.Helpers;
|
|
|
+using Admin.Core.Common.Configs;
|
|
|
+using Admin.Core.Common.Auth;
|
|
|
|
|
|
namespace Admin.Core.Controllers.Admin
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 图片管理
|
|
|
/// </summary>
|
|
|
- [Area("Admin")]
|
|
|
- [Route("api/[area]/[controller]/[action]")]
|
|
|
- [ApiController]
|
|
|
- [NoOprationLog]
|
|
|
- public class ImgController : ControllerBase
|
|
|
+ public class ImgController : AreaController
|
|
|
{
|
|
|
- /// <summary>
|
|
|
- /// 获取头像
|
|
|
- /// </summary>
|
|
|
- /// <param name="environment"></param>
|
|
|
- /// <param name="fileName"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [HttpGet]
|
|
|
- [Route("{fileName}")]
|
|
|
- public FileStreamResult Avatar([FromServices]IWebHostEnvironment environment, string fileName = "")
|
|
|
+ private readonly IUser _user;
|
|
|
+ private readonly UploadConfig _uploadConfig;
|
|
|
+
|
|
|
+ public ImgController(IUser user, IOptionsMonitor<UploadConfig> uploadConfig)
|
|
|
{
|
|
|
- string filepath = Path.Combine(environment.WebRootPath,"avatar", fileName);
|
|
|
- var stream = System.IO.File.OpenRead(filepath);
|
|
|
- string fileExt = Path.GetExtension(filepath);
|
|
|
- var contentTypeProvider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
|
|
|
- var contentType = contentTypeProvider.Mappings[fileExt];
|
|
|
- var fileDownloadName = Path.GetFileName(filepath);
|
|
|
-
|
|
|
- return File(stream, contentType, fileDownloadName);
|
|
|
+ _user = user;
|
|
|
+ _uploadConfig = uploadConfig.CurrentValue;
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
/// <summary>
|
|
|
- /// 下载图片
|
|
|
+ /// 获取头像
|
|
|
/// </summary>
|
|
|
/// <param name="environment"></param>
|
|
|
/// <param name="fileName"></param>
|
|
|
/// <returns></returns>
|
|
|
[HttpGet]
|
|
|
[Route("{fileName}")]
|
|
|
- public FileStreamResult Download([FromServices]IWebHostEnvironment environment,string fileName = "")
|
|
|
+ [NoOprationLog]
|
|
|
+ [AllowAnonymous]
|
|
|
+ public FileStreamResult Avatar([FromServices]IWebHostEnvironment environment, string fileName = "")
|
|
|
{
|
|
|
- string filepath = Path.Combine(environment.WebRootPath, "images", fileName);
|
|
|
- var stream = System.IO.File.OpenRead(filepath);
|
|
|
- string fileExt = Path.GetExtension(filepath);
|
|
|
+ string filePath = Path.Combine(environment.WebRootPath,"avatar", fileName);
|
|
|
+ var stream = System.IO.File.OpenRead(filePath);
|
|
|
+ string fileExt = Path.GetExtension(filePath);
|
|
|
var contentTypeProvider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
|
|
|
var contentType = contentTypeProvider.Mappings[fileExt];
|
|
|
- var fileDownloadName = Path.GetFileName(filepath);
|
|
|
+ var fileDownloadName = Path.GetFileName(filePath);
|
|
|
|
|
|
return File(stream, contentType, fileDownloadName);
|
|
|
}
|
|
|
+ */
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 上传图片
|
|
|
- /// 支持多图片上传
|
|
|
+ /// 上传头像
|
|
|
/// </summary>
|
|
|
/// <param name="environment"></param>
|
|
|
+ /// <param name="file"></param>
|
|
|
/// <returns></returns>
|
|
|
[HttpPost]
|
|
|
- public async Task<IResponseOutput> Upload([FromServices]IWebHostEnvironment environment)
|
|
|
+ [Login]
|
|
|
+ public async Task<IResponseOutput> AvatarUpload([FromServices]IWebHostEnvironment environment, IFormFile file)
|
|
|
{
|
|
|
- string path = string.Empty;
|
|
|
- string foldername = "images";
|
|
|
- IFormFileCollection files = null;
|
|
|
-
|
|
|
- try
|
|
|
+ if(file == null || file.Length < 1)
|
|
|
{
|
|
|
- files = Request.Form.Files;
|
|
|
- }
|
|
|
- catch (Exception)
|
|
|
- {
|
|
|
- files = null;
|
|
|
+ if (Request.Form.Files != null && Request.Form.Files.Any())
|
|
|
+ {
|
|
|
+ file = Request.Form.Files[0];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if (files == null || !files.Any())
|
|
|
+ if (file == null || file.Length < 1)
|
|
|
{
|
|
|
- return ResponseOutput.NotOk("请选择上传的文件。");
|
|
|
+ return ResponseOutput.NotOk("请上传头像!");
|
|
|
}
|
|
|
|
|
|
+ var avatar = _uploadConfig.Avatar;
|
|
|
+
|
|
|
//格式限制
|
|
|
- var allowType = new string[] { "image/jpg", "image/png", "image/jpeg" };
|
|
|
+ if (!avatar.ContentType.Contains(file.ContentType))
|
|
|
+ {
|
|
|
+ return ResponseOutput.NotOk("图片格式错误");
|
|
|
+ }
|
|
|
|
|
|
- string folderpath = Path.Combine(environment.WebRootPath, foldername);
|
|
|
- if (!Directory.Exists(folderpath))
|
|
|
+ //大小限制
|
|
|
+ if (!(file.Length <= avatar.Size))
|
|
|
{
|
|
|
- Directory.CreateDirectory(folderpath);
|
|
|
+ return ResponseOutput.NotOk("图片过大");
|
|
|
}
|
|
|
|
|
|
- if (files.Any(c => allowType.Contains(c.ContentType)))
|
|
|
+ var dateTimeFormat = avatar.DateTimeFormat.NotNull() ? DateTime.Now.ToString(avatar.DateTimeFormat) : "";
|
|
|
+ var format = avatar.Format.NotNull() ? string.Format(avatar.Format,_user.Id) : "";
|
|
|
+ var savePath = Path.Combine(dateTimeFormat, format);
|
|
|
+ var fullDirectory = Path.Combine(avatar.Path, savePath);
|
|
|
+ if (!Directory.Exists(fullDirectory))
|
|
|
{
|
|
|
- if (files.Sum(c => c.Length) <= 1024 * 1024 * 4)
|
|
|
- {
|
|
|
- //foreach (var file in files)
|
|
|
- var file = files.FirstOrDefault();
|
|
|
- string strpath = Path.Combine(foldername, DateTime.Now.ToString("MMddHHmmss") + file.FileName);
|
|
|
- path = Path.Combine(environment.WebRootPath, strpath);
|
|
|
+ Directory.CreateDirectory(fullDirectory);
|
|
|
+ }
|
|
|
|
|
|
- using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
|
|
|
- {
|
|
|
- await file.CopyToAsync(stream);
|
|
|
- }
|
|
|
+ var saveFileName = $"{new Snowfake(0).nextId()}{Path.GetExtension(file.FileName)}";
|
|
|
+ var fullPath = Path.Combine(fullDirectory, saveFileName);
|
|
|
|
|
|
- return ResponseOutput.Ok(strpath);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return ResponseOutput.NotOk("图片过大");
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
+ using (var stream = new FileStream(fullPath, FileMode.Create))
|
|
|
{
|
|
|
- return ResponseOutput.NotOk("图片格式错误");
|
|
|
+ await file.CopyToAsync(stream);
|
|
|
}
|
|
|
+
|
|
|
+ return ResponseOutput.Ok(Path.Combine(savePath, saveFileName));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|