using FluentEmail.Core; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Minio; using Minio.DataModel; using Minio.DataModel.Args; using Org.BouncyCastle.Asn1.Ocsp; using Org.BouncyCastle.Utilities; using Ropin.Inspection.Api.Common; using Ropin.Inspection.Api.Common.Options; using Ropin.Inspection.Common.Helper; using Ropin.Inspection.Model; using Ropin.Inspection.Model.Common; using Ropin.Inspection.Model.ViewModel.LGS; using Ropin.Inspection.Service.LGS.Interface; using System; using System.IO; using System.Net; using System.Security.Policy; using System.Threading.Tasks; namespace Ropin.Inspection.Api.Controllers.LGS { public class ImageLibraryController : BaseController { private readonly IImageLibraryService _imageLibraryService; private readonly IMinioClient minioClient; private readonly MinioSettingsOptions minioSetting; public ImageLibraryController(IImageLibraryService imageLibraryService, IMinioClient minioClient,IOptions options) { _imageLibraryService = imageLibraryService; this.minioClient = minioClient; minioSetting = options.Value; } /// /// 添加 /// /// /// [HttpPost("Add")] public async Task AddAsync(ImageLibraryModel imageLibraryModel) { imageLibraryModel.C_Id=Guid.NewGuid().ToString(); if (!string.IsNullOrEmpty(imageLibraryModel.base64Image)) { //id拼接扩展名当作minio文件名 var objName = imageLibraryModel.C_Id + imageLibraryModel.fileExName; var imagePath=await UploadFile(imageLibraryModel.base64Image, objName); imageLibraryModel.C_ImagePath = imagePath; } try { await _imageLibraryService.CreateOneAsync(imageLibraryModel); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } return new ApiResult(ReturnCode.Success); } /// /// 分页列表 /// /// /// [HttpPost("Page")] public async Task PageAsync(ImageLibraryInput input) { try { var libraryModels = await _imageLibraryService.PageAsync(input); PagesModel pages = new PagesModel(libraryModels,input); pages.Items.ForEach(item => { item.C_ImagePath = minioSetting.Urls + minioSetting.BucketName + item.C_ImagePath; }); return new ApiResult>(pages); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } [HttpGet("Detail/{id}")] public async Task DeatilAsync(string id) { var result =await _imageLibraryService.GetByIdAsync(id); result.C_ImagePath = minioSetting.Urls + minioSetting.BucketName + result.C_ImagePath; return new ApiResult(result); } /// /// 删除 /// /// /// [HttpDelete("DeleteReportAsync/{id}")] public async Task DeleteReportAsync(string id) { try { ImageLibraryModel model = await _imageLibraryService.GetByIdAsync(id); await _imageLibraryService.DeleteAsync(id); RemoveObjectArgs args = new RemoveObjectArgs() .WithBucket(minioSetting.BucketName) .WithObject(model.C_ImagePath); await minioClient.RemoveObjectAsync(args); return new ApiResult(ReturnCode.Success); } catch (Exception ex) { return new ApiResult(ReturnCode.GeneralError, ex.Message); } } /// /// 更新 /// /// /// [HttpPost("Update")] public async Task UpdateAsync(ImageLibraryModel imageLibraryModel) { ImageLibraryModel model = await _imageLibraryService.GetByIdAsync(imageLibraryModel.C_Id); if (!string.IsNullOrEmpty(imageLibraryModel.base64Image)) { RemoveObjectArgs args = new RemoveObjectArgs() .WithBucket(minioSetting.BucketName) .WithObject(model.C_ImagePath); await minioClient.RemoveObjectAsync(args); //id拼接扩展名当作minio文件名 var objName = imageLibraryModel.C_Id + imageLibraryModel.fileExName; var imagePath = await UploadFile(imageLibraryModel.base64Image, objName); imageLibraryModel.C_ImagePath = imagePath; } var result=await _imageLibraryService.UpdateOneAsync(imageLibraryModel); return new ApiResult(result>=1); } /// /// 上传文件到minIO /// /// /// /// [NonAction] public async Task UploadFile(string base64Image, string objName) { // 移除可能存在的 Base64 数据前缀 if (base64Image.StartsWith("data:image", StringComparison.OrdinalIgnoreCase)) { int commaIndex = base64Image.IndexOf(','); if (commaIndex > 0) { base64Image = base64Image.Substring(commaIndex + 1); } } // 将 Base64 字符串解码为字节数组 byte[] imageBytes = Convert.FromBase64String(base64Image); var stream = new MemoryStream(imageBytes); var putObj = new PutObjectArgs() .WithBucket(minioSetting.BucketName) .WithObject($"/image/{objName}") //.WithFileName(fileName); .WithStreamData(stream) .WithObjectSize(stream.Length); var minioResponse = await minioClient.PutObjectAsync(putObj); if (minioResponse.ResponseStatusCode != HttpStatusCode.OK) { return string.Empty; } return $"/image/{objName}"; } } }