ImgController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Options;
  9. using Admin.Core.Model.Output;
  10. using Admin.Core.Attributes;
  11. using Admin.Core.Common.Helpers;
  12. using Admin.Core.Common.Configs;
  13. using Admin.Core.Common.Auth;
  14. namespace Admin.Core.Controllers.Admin
  15. {
  16. /// <summary>
  17. /// 图片管理
  18. /// </summary>
  19. public class ImgController : AreaController
  20. {
  21. private readonly IUser _user;
  22. private readonly UploadConfig _uploadConfig;
  23. public ImgController(IUser user, IOptionsMonitor<UploadConfig> uploadConfig)
  24. {
  25. _user = user;
  26. _uploadConfig = uploadConfig.CurrentValue;
  27. }
  28. /*
  29. /// <summary>
  30. /// 获取头像
  31. /// </summary>
  32. /// <param name="environment"></param>
  33. /// <param name="fileName"></param>
  34. /// <returns></returns>
  35. [HttpGet]
  36. [Route("{fileName}")]
  37. [NoOprationLog]
  38. [AllowAnonymous]
  39. public FileStreamResult Avatar([FromServices]IWebHostEnvironment environment, string fileName = "")
  40. {
  41. string filePath = Path.Combine(environment.WebRootPath,"avatar", fileName);
  42. var stream = System.IO.File.OpenRead(filePath);
  43. string fileExt = Path.GetExtension(filePath);
  44. var contentTypeProvider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
  45. var contentType = contentTypeProvider.Mappings[fileExt];
  46. var fileDownloadName = Path.GetFileName(filePath);
  47. return File(stream, contentType, fileDownloadName);
  48. }
  49. */
  50. /// <summary>
  51. /// 上传头像
  52. /// </summary>
  53. /// <param name="environment"></param>
  54. /// <param name="file"></param>
  55. /// <returns></returns>
  56. [HttpPost]
  57. [Login]
  58. public async Task<IResponseOutput> AvatarUpload([FromServices]IWebHostEnvironment environment, IFormFile file)
  59. {
  60. if(file == null || file.Length < 1)
  61. {
  62. if (Request.Form.Files != null && Request.Form.Files.Any())
  63. {
  64. file = Request.Form.Files[0];
  65. }
  66. }
  67. if (file == null || file.Length < 1)
  68. {
  69. return ResponseOutput.NotOk("请上传头像!");
  70. }
  71. var avatar = _uploadConfig.Avatar;
  72. //格式限制
  73. if (!avatar.ContentType.Contains(file.ContentType))
  74. {
  75. return ResponseOutput.NotOk("图片格式错误");
  76. }
  77. //大小限制
  78. if (!(file.Length <= avatar.Size))
  79. {
  80. return ResponseOutput.NotOk("图片过大");
  81. }
  82. var dateTimeFormat = avatar.DateTimeFormat.NotNull() ? DateTime.Now.ToString(avatar.DateTimeFormat) : "";
  83. var format = avatar.Format.NotNull() ? string.Format(avatar.Format,_user.Id) : "";
  84. var savePath = Path.Combine(dateTimeFormat, format);
  85. var fullDirectory = Path.Combine(avatar.Path, savePath);
  86. if (!Directory.Exists(fullDirectory))
  87. {
  88. Directory.CreateDirectory(fullDirectory);
  89. }
  90. var saveFileName = $"{new Snowfake(0).nextId()}{Path.GetExtension(file.FileName)}";
  91. var fullPath = Path.Combine(fullDirectory, saveFileName);
  92. using (var stream = new FileStream(fullPath, FileMode.Create))
  93. {
  94. await file.CopyToAsync(stream);
  95. }
  96. return ResponseOutput.Ok(Path.Combine(savePath, saveFileName));
  97. }
  98. }
  99. }