Fix รายชื่อผู้สอบผ่านรูปภาพผู้สมัครสอบไม่แสดง issue #908

This commit is contained in:
Bright 2025-07-04 14:00:36 +07:00
parent 3e1701cfdf
commit 55128d5b43
2 changed files with 111 additions and 0 deletions

View file

@ -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<Document> 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<FormFile> 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

View file

@ -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)