diff --git a/API/FamilyTreeAPI/Entities/FileContent.cs b/API/FamilyTreeAPI/Entities/FileContent.cs index faeeda9..8a027e4 100644 --- a/API/FamilyTreeAPI/Entities/FileContent.cs +++ b/API/FamilyTreeAPI/Entities/FileContent.cs @@ -20,7 +20,7 @@ public class UploadCriteria { public string FileName { get; set; } public int PersonId { get; set; } - + public int? PhotoType {get; set;} public IFormFile File { get; set; } } public class DeleteFileCriteria diff --git a/API/FamilyTreeAPI/Entities/PersonPhotoDto.cs b/API/FamilyTreeAPI/Entities/PersonPhotoDto.cs index 1e529f4..ab4f031 100644 --- a/API/FamilyTreeAPI/Entities/PersonPhotoDto.cs +++ b/API/FamilyTreeAPI/Entities/PersonPhotoDto.cs @@ -1,5 +1,12 @@ namespace FamilyTreeAPI.Entities; + +//photo Type string number +public enum enumPhotoType +{ + Profile = 1, + Other = 2 +} public class PersonPhotoDto { public int Id { get; set; } diff --git a/API/FamilyTreeAPI/Repository/PersonPhotoRepository.cs b/API/FamilyTreeAPI/Repository/PersonPhotoRepository.cs index 1f054d4..8dca496 100644 --- a/API/FamilyTreeAPI/Repository/PersonPhotoRepository.cs +++ b/API/FamilyTreeAPI/Repository/PersonPhotoRepository.cs @@ -44,8 +44,10 @@ namespace FamilyTreeAPI.Repository { int result = default(int); int statuscode = 0; - string error = ""; - + string error = "";//Other + string photoType = "2"; // + if (criteria.PhotoType.HasValue) + photoType = criteria.PhotoType.Value.ToString(); HttpContext? httpContext = _httpContext.HttpContext; string loginName = ""; if (httpContext != null) @@ -62,7 +64,7 @@ namespace FamilyTreeAPI.Repository PersonPhoto model; model = new(); - + model.PhotoType = photoType; model = FillModel(model,criteria.PersonId, formfile.FileName); using (var stream = new MemoryStream()) diff --git a/API/FamilyTreeAPI/Repository/PersonRepository.cs b/API/FamilyTreeAPI/Repository/PersonRepository.cs index 4583522..448ac53 100644 --- a/API/FamilyTreeAPI/Repository/PersonRepository.cs +++ b/API/FamilyTreeAPI/Repository/PersonRepository.cs @@ -283,7 +283,7 @@ public partial class PersonRepository : IPerson node.Key = item.Id.ToString(); node.Type = type; if (item.Image != null) - node.Data.Image = await LoadPersonImage(item.Image); + node.Data.Image = await LoadPersonImage(item.Id,item.Image); node.Icon = "T"; node.StyleClass = getStyleClass(node.Icon); @@ -319,8 +319,8 @@ public partial class PersonRepository : IPerson TreeNode child; PersonDto dto; Person pe; - int fatherId, motherId; - fatherId = motherId = 0; + int fatherId, motherId, personId; + personId = fatherId = motherId = 0; string data = person.Sex; TreeNode citem; string pName = ""; @@ -371,6 +371,7 @@ public partial class PersonRepository : IPerson pName = pe.FirstName; image = pe.Image; key = motherId.ToString(); + personId = motherId; data = pe.Sex; } } @@ -382,6 +383,7 @@ public partial class PersonRepository : IPerson pName = pe.FirstName; image = pe.Image; key = fatherId.ToString(); + personId = fatherId; data = pe.Sex; } } @@ -390,7 +392,7 @@ public partial class PersonRepository : IPerson citem.Data = new(); citem.Label = pName; if (image != null) - citem.Data.Image = await LoadPersonImage(image); + citem.Data.Image = await LoadPersonImage(personId,image); citem.Icon = "P"; @@ -411,7 +413,7 @@ public partial class PersonRepository : IPerson child.Type = type; image = dto.Image; if (image != null) - child.Data.Image = await LoadPersonImage(image); + child.Data.Image = await LoadPersonImage(dto.Id,image); child.Icon = "C"; child.StyleClass = getStyleClass(child.Icon); child.Label = dto.FirstName; @@ -480,12 +482,12 @@ public partial class PersonRepository : IPerson /// /// /// - private string SaveFile(string fileName, int familyId, string base64) + private async Task SaveFile(string fileName, int personId, string base64) { string path = ""; int statusCode = 0; - string error = ""; + // string error = ""; string sdate = GetCurrentDateTime(); string filename, extention; filename = extention = ""; @@ -494,14 +496,33 @@ public partial class PersonRepository : IPerson string rbase = base64.Substring(first, last - first); byte[] newBytes = Convert.FromBase64String(rbase); + extention = System.IO.Path.GetExtension(fileName); + filename = personId + "_" + sdate + extention; + + // Wrap the byte array in a MemoryStream + var stream = new MemoryStream(newBytes); + + // Create the IFormFile instance + IFormFile file = new FormFile(stream, 0, newBytes.Length, "Profile", filename) + { + Headers = new HeaderDictionary(), + ContentType = "application/octet-stream" // Change to your specific MIME type if known (e.g., "image/jpeg", "application/pdf") + }; + //save to database. + UploadCriteria criteria = new(); + criteria.FileName = filename; + criteria.PersonId = personId; + criteria.PhotoType = (int) enumPhotoType.Profile; + criteria.File = file; + var rval = await _personPhoto.SaveAsync(criteria); + + /* path = _config["AppSettings:ImageFolder"]; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } - - extention = System.IO.Path.GetExtension(fileName); - filename = familyId + "_" + sdate + extention; + string fullpath = System.IO.Path.Combine(path, filename); /* using (var fileStream = new FileStream(fullpath, FileMode.Create)) @@ -520,10 +541,12 @@ public partial class PersonRepository : IPerson } */ + /* using (FileStream fileStream = new FileStream(fullpath, FileMode.Create, FileAccess.Write, FileShare.None)) { fileStream.Write(newBytes, 0, newBytes.Length); } + */ return filename; } @@ -538,7 +561,7 @@ public partial class PersonRepository : IPerson string loginName = ""; if (httpContext != null) { - UserDto? user = (UserDto?)httpContext.Items["User"]; + UserDto? user = (UserDto?)httpContext.Items["User"]; if (user != null) loginName = user.FirstName + " " + user.LastName; } @@ -571,7 +594,7 @@ public partial class PersonRepository : IPerson if (!string.IsNullOrEmpty(container.FileName)) { - string image = SaveFile(container.FileName, result, container.FormData); + string image = await SaveFile(container.FileName, result, container.FormData); // update the image in table to new model.Image = image; await _context.SaveChangesAsync(); @@ -587,7 +610,7 @@ public partial class PersonRepository : IPerson result = item.Id; if (!string.IsNullOrEmpty(container.FileName)) { - string image = SaveFile(container.FileName, result, container.FormData); + string image = await SaveFile(container.FileName, result, container.FormData); // update the image in table to new model1.Image = image; } @@ -714,17 +737,34 @@ public partial class PersonRepository : IPerson }; } - private async Task LoadPersonImage(string filename) + private async Task LoadPersonImage_old(string filename) { string result = ""; DownloadFileCriteria criteria = new() { FileName = filename }; var resultModel = await _importPersonRepository.DownloadFile(criteria); - if (!string.IsNullOrEmpty((resultModel.Data))) + + + if (!string.IsNullOrEmpty(resultModel.Data)) result = "data:image/png;base64," + resultModel.Data; return result; } + private async Task LoadPersonImage(int personId,string filename) + { + string result = ""; + + // DownloadFileCriteria criteria = new() { FileName = filename }; + + var resultModel = await _personPhoto.DownloadPersonPhoto(personId, filename); + //if (resultModel.Data.Content) + string base64String = Convert.ToBase64String(resultModel.Data.Content); + + if (!string.IsNullOrEmpty(base64String)) + result = "data:image/png;base64," + base64String; + return result; + } + public ResultModel DeleteUploadFile(DeleteFileCriteria criteria) { int result = -1;