ImgController.cs 3.6 KB

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