From 55128d5b4319a6c1ad4957e921fd7bd2ec9662c4 Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 4 Jul 2025 14:00:36 +0700 Subject: [PATCH] =?UTF-8?q?Fix=20=E0=B8=A3=E0=B8=B2=E0=B8=A2=E0=B8=8A?= =?UTF-8?q?=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B8=9C=E0=B8=B9=E0=B9=89=E0=B8=AA?= =?UTF-8?q?=E0=B8=AD=E0=B8=9A=E0=B8=9C=E0=B9=88=E0=B8=B2=E0=B8=99=E0=B8=A3?= =?UTF-8?q?=E0=B8=B9=E0=B8=9B=E0=B8=A0=E0=B8=B2=E0=B8=9E=E0=B8=9C=E0=B8=B9?= =?UTF-8?q?=E0=B9=89=E0=B8=AA=E0=B8=A1=E0=B8=B1=E0=B8=84=E0=B8=A3=E0=B8=AA?= =?UTF-8?q?=E0=B8=AD=E0=B8=9A=E0=B9=84=E0=B8=A1=E0=B9=88=E0=B9=81=E0=B8=AA?= =?UTF-8?q?=E0=B8=94=E0=B8=87=20issue=20#908?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/MinIOService.cs | 98 +++++++++++++++++++++++++++++++++++ Services/PeriodExamService.cs | 13 +++++ 2 files changed, 111 insertions(+) diff --git a/Services/MinIOService.cs b/Services/MinIOService.cs index 8af1740..bbe8799 100644 --- a/Services/MinIOService.cs +++ b/Services/MinIOService.cs @@ -19,6 +19,7 @@ namespace BMA.EHR.Recurit.Exam.Service.Services #region " Fields " private readonly ApplicationDbContext _context; + private readonly MetadataDbContext _contextMetadata; private readonly IConfiguration _configuration; private readonly IWebHostEnvironment _webHostEnvironment; private readonly AmazonS3Client _s3Client; @@ -29,10 +30,12 @@ namespace BMA.EHR.Recurit.Exam.Service.Services #region " Constructors " public MinIOService(ApplicationDbContext context, + MetadataDbContext contextMetadata, IConfiguration configuration, IWebHostEnvironment webHostEnvironment) { _context = context; + _contextMetadata = contextMetadata; _configuration = configuration; _webHostEnvironment = webHostEnvironment; @@ -165,6 +168,101 @@ namespace BMA.EHR.Recurit.Exam.Service.Services } } + public async Task UploadImageFileAsync(IFormFile file, string newFileName = "") + { + var fileName = ""; + var fileExt = Path.GetExtension(file.FileName); + if (newFileName != "") + fileName = $"{newFileName}"; + else + fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); + + var tmpDir = Path.Combine(_webHostEnvironment.ContentRootPath, "tmp"); + if (!Directory.Exists(tmpDir)) + Directory.CreateDirectory(tmpDir); + + var tmpFile = Path.Combine(tmpDir, $"tmp_{DateTime.Now.ToString("ddMMyyyyHHmmss")}{fileExt}"); + + try + { + using (var ms = new MemoryStream()) + { + var id = Guid.NewGuid(); + file.CopyTo(ms); + var fileBytes = ms.ToArray(); + System.IO.MemoryStream filestream = new System.IO.MemoryStream(fileBytes); + + var request = new PutObjectRequest + { + BucketName = _bucketName, + Key = id.ToString("D"), + InputStream = filestream, + ContentType = file.ContentType, + CannedACL = S3CannedACL.PublicRead + }; + + await _s3Client.PutObjectAsync(request); + + // create document object + var doc = new Document() + { + FileName = fileName, + FileType = file.ContentType, + FileSize = Convert.ToInt32(file.Length), + ObjectRefId = id, + CreatedDate = DateTime.Now + }; + // ใช้ _contextMetadata เพื่อ Save ลงฝั่งระบบบรรจุ + await _contextMetadata.Documents.AddAsync(doc); + await _contextMetadata.SaveChangesAsync(); + + return doc; + } + } + catch + { + throw; + } + finally + { + File.Delete(tmpFile); + } + } + + public async Task GetImageToFormFileAsync(string refId) + { + try + { + using var memoryStream = new MemoryStream(); + var request = new GetObjectRequest + { + BucketName = _bucketName, + Key = refId + }; + + using var response = await _s3Client.GetObjectAsync(request); + using var responseStream = response.ResponseStream; + + await responseStream.CopyToAsync(memoryStream); + + var finalBytes = memoryStream.ToArray(); + + var finalStream = new MemoryStream(finalBytes); + var fileName = Path.GetFileName(refId); + var contentType = response.Headers.ContentType ?? "image/jpeg"; + + return new FormFile(finalStream, 0, finalStream.Length, "file", fileName) + { + Headers = new HeaderDictionary(), + ContentType = contentType + }; + } + catch + { + throw; + } + } + public async Task DeleteFileAsync(Guid fileId) { try diff --git a/Services/PeriodExamService.cs b/Services/PeriodExamService.cs index cd0a2f2..fc0d0d3 100644 --- a/Services/PeriodExamService.cs +++ b/Services/PeriodExamService.cs @@ -2959,6 +2959,19 @@ namespace BMA.EHR.Recurit.Exam.Service.Services posLevelIdOld = org?.result?.posLevelId ?? null, posLevelNameOld = org?.result?.posLevelName ?? null, }; + + if (candidate.ProfileImg != null && candidate.ProfileImg.ObjectRefId != null) + { + IFormFile imageFile = await _minioService.GetImageToFormFileAsync(candidate.ProfileImg.ObjectRefId.ToString()); + var doc = await _minioService.UploadImageFileAsync(imageFile, imageFile.FileName); + var _doc = await _contextMetadata.Documents.AsQueryable() + .FirstOrDefaultAsync(x => x.Id == doc.Id); + if (_doc != null) + { + placementProfile.ProfileImg = _doc; + } + } + await _contextMetadata.PlacementProfiles.AddAsync(placementProfile); foreach (var education in candidate.Educations)