ImageLibraryController.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using FluentEmail.Core;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Options;
  6. using Minio;
  7. using Minio.DataModel;
  8. using Minio.DataModel.Args;
  9. using Org.BouncyCastle.Asn1.Ocsp;
  10. using Org.BouncyCastle.Utilities;
  11. using Ropin.Inspection.Api.Common;
  12. using Ropin.Inspection.Api.Common.Options;
  13. using Ropin.Inspection.Common.Helper;
  14. using Ropin.Inspection.Model;
  15. using Ropin.Inspection.Model.Common;
  16. using Ropin.Inspection.Model.ViewModel.LGS;
  17. using Ropin.Inspection.Service.LGS.Interface;
  18. using System;
  19. using System.IO;
  20. using System.Net;
  21. using System.Security.Policy;
  22. using System.Threading.Tasks;
  23. namespace Ropin.Inspection.Api.Controllers.LGS
  24. {
  25. public class ImageLibraryController : BaseController
  26. {
  27. private readonly IImageLibraryService _imageLibraryService;
  28. private readonly IMinioClient minioClient;
  29. private readonly MinioSettingsOptions minioSetting;
  30. public ImageLibraryController(IImageLibraryService imageLibraryService, IMinioClient minioClient,IOptions<MinioSettingsOptions> options)
  31. {
  32. _imageLibraryService = imageLibraryService;
  33. this.minioClient = minioClient;
  34. minioSetting = options.Value;
  35. }
  36. /// <summary>
  37. /// 添加
  38. /// </summary>
  39. /// <param name="imageLibraryModel"></param>
  40. /// <returns></returns>
  41. [HttpPost("Add")]
  42. public async Task<ApiResult> AddAsync(ImageLibraryModel imageLibraryModel)
  43. {
  44. imageLibraryModel.C_Id=Guid.NewGuid().ToString();
  45. if (!string.IsNullOrEmpty(imageLibraryModel.base64Image))
  46. {
  47. //id拼接扩展名当作minio文件名
  48. var objName = imageLibraryModel.C_Id + imageLibraryModel.fileExName;
  49. var imagePath=await UploadFile(imageLibraryModel.base64Image, objName);
  50. imageLibraryModel.C_ImagePath = imagePath;
  51. }
  52. try
  53. {
  54. await _imageLibraryService.CreateOneAsync(imageLibraryModel);
  55. }
  56. catch (Exception ex)
  57. {
  58. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  59. }
  60. return new ApiResult(ReturnCode.Success);
  61. }
  62. /// <summary>
  63. /// 分页列表
  64. /// </summary>
  65. /// <param name="imageLibraryModel"></param>
  66. /// <returns></returns>
  67. [HttpPost("Page")]
  68. public async Task<ApiResult> PageAsync(ImageLibraryInput input)
  69. {
  70. try
  71. {
  72. var libraryModels = await _imageLibraryService.PageAsync(input);
  73. PagesModel<ImageLibraryModel> pages = new PagesModel<ImageLibraryModel>(libraryModels,input);
  74. pages.Items.ForEach(item =>
  75. {
  76. item.C_ImagePath = minioSetting.Urls + minioSetting.BucketName + item.C_ImagePath;
  77. });
  78. return new ApiResult<PagesModel<ImageLibraryModel>>(pages);
  79. }
  80. catch (Exception ex)
  81. {
  82. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  83. }
  84. }
  85. [HttpGet("Detail/{id}")]
  86. public async Task<ApiResult> DeatilAsync(string id)
  87. {
  88. var result =await _imageLibraryService.GetByIdAsync(id);
  89. result.C_ImagePath = minioSetting.Urls + minioSetting.BucketName + result.C_ImagePath;
  90. return new ApiResult<ImageLibraryModel>(result);
  91. }
  92. /// <summary>
  93. /// 删除
  94. /// </summary>
  95. /// <param name="id"></param>
  96. /// <returns></returns>
  97. [HttpDelete("DeleteReportAsync/{id}")]
  98. public async Task<ApiResult> DeleteReportAsync(string id)
  99. {
  100. try
  101. {
  102. ImageLibraryModel model = await _imageLibraryService.GetByIdAsync(id);
  103. await _imageLibraryService.DeleteAsync(id);
  104. RemoveObjectArgs args = new RemoveObjectArgs()
  105. .WithBucket(minioSetting.BucketName)
  106. .WithObject(model.C_ImagePath);
  107. await minioClient.RemoveObjectAsync(args);
  108. return new ApiResult(ReturnCode.Success);
  109. }
  110. catch (Exception ex)
  111. {
  112. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  113. }
  114. }
  115. /// <summary>
  116. /// 更新
  117. /// </summary>
  118. /// <param name="imageLibraryModel"></param>
  119. /// <returns></returns>
  120. [HttpPost("Update")]
  121. public async Task<ApiResult> UpdateAsync(ImageLibraryModel imageLibraryModel)
  122. {
  123. ImageLibraryModel model = await _imageLibraryService.GetByIdAsync(imageLibraryModel.C_Id);
  124. if (!string.IsNullOrEmpty(imageLibraryModel.base64Image))
  125. {
  126. RemoveObjectArgs args = new RemoveObjectArgs()
  127. .WithBucket(minioSetting.BucketName)
  128. .WithObject(model.C_ImagePath);
  129. await minioClient.RemoveObjectAsync(args);
  130. //id拼接扩展名当作minio文件名
  131. var objName = imageLibraryModel.C_Id + imageLibraryModel.fileExName;
  132. var imagePath = await UploadFile(imageLibraryModel.base64Image, objName);
  133. imageLibraryModel.C_ImagePath = imagePath;
  134. }
  135. var result=await _imageLibraryService.UpdateOneAsync(imageLibraryModel);
  136. return new ApiResult<bool>(result>=1);
  137. }
  138. /// <summary>
  139. /// 上传文件到minIO
  140. /// </summary>
  141. /// <param name="base64Image"></param>
  142. /// <param name="objName"></param>
  143. /// <returns></returns>
  144. [NonAction]
  145. public async Task<string> UploadFile(string base64Image, string objName)
  146. {
  147. // 移除可能存在的 Base64 数据前缀
  148. if (base64Image.StartsWith("data:image", StringComparison.OrdinalIgnoreCase))
  149. {
  150. int commaIndex = base64Image.IndexOf(',');
  151. if (commaIndex > 0)
  152. {
  153. base64Image = base64Image.Substring(commaIndex + 1);
  154. }
  155. }
  156. // 将 Base64 字符串解码为字节数组
  157. byte[] imageBytes = Convert.FromBase64String(base64Image);
  158. var stream = new MemoryStream(imageBytes);
  159. var putObj = new PutObjectArgs()
  160. .WithBucket(minioSetting.BucketName)
  161. .WithObject($"/image/{objName}")
  162. //.WithFileName(fileName);
  163. .WithStreamData(stream)
  164. .WithObjectSize(stream.Length);
  165. var minioResponse = await minioClient.PutObjectAsync(putObj);
  166. if (minioResponse.ResponseStatusCode != HttpStatusCode.OK)
  167. {
  168. return string.Empty;
  169. }
  170. return $"/image/{objName}";
  171. }
  172. }
  173. }