change all save to database no folder anymore

This commit is contained in:
2026-09-19 22:37:43 +10:00
parent ba4976fce4
commit 75b49be476
4 changed files with 68 additions and 19 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ public class UploadCriteria
{ {
public string FileName { get; set; } public string FileName { get; set; }
public int PersonId { get; set; } public int PersonId { get; set; }
public int? PhotoType {get; set;}
public IFormFile File { get; set; } public IFormFile File { get; set; }
} }
public class DeleteFileCriteria public class DeleteFileCriteria
@@ -1,5 +1,12 @@
namespace FamilyTreeAPI.Entities; namespace FamilyTreeAPI.Entities;
//photo Type string number
public enum enumPhotoType
{
Profile = 1,
Other = 2
}
public class PersonPhotoDto public class PersonPhotoDto
{ {
public int Id { get; set; } public int Id { get; set; }
@@ -44,8 +44,10 @@ namespace FamilyTreeAPI.Repository
{ {
int result = default(int); int result = default(int);
int statuscode = 0; int statuscode = 0;
string error = ""; string error = "";//Other
string photoType = "2"; //
if (criteria.PhotoType.HasValue)
photoType = criteria.PhotoType.Value.ToString();
HttpContext? httpContext = _httpContext.HttpContext; HttpContext? httpContext = _httpContext.HttpContext;
string loginName = ""; string loginName = "";
if (httpContext != null) if (httpContext != null)
@@ -62,7 +64,7 @@ namespace FamilyTreeAPI.Repository
PersonPhoto model; PersonPhoto model;
model = new(); model = new();
model.PhotoType = photoType;
model = FillModel(model,criteria.PersonId, formfile.FileName); model = FillModel(model,criteria.PersonId, formfile.FileName);
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
@@ -283,7 +283,7 @@ public partial class PersonRepository : IPerson
node.Key = item.Id.ToString(); node.Key = item.Id.ToString();
node.Type = type; node.Type = type;
if (item.Image != null) if (item.Image != null)
node.Data.Image = await LoadPersonImage(item.Image); node.Data.Image = await LoadPersonImage(item.Id,item.Image);
node.Icon = "T"; node.Icon = "T";
node.StyleClass = getStyleClass(node.Icon); node.StyleClass = getStyleClass(node.Icon);
@@ -319,8 +319,8 @@ public partial class PersonRepository : IPerson
TreeNode<TreeData> child; TreeNode<TreeData> child;
PersonDto dto; PersonDto dto;
Person pe; Person pe;
int fatherId, motherId; int fatherId, motherId, personId;
fatherId = motherId = 0; personId = fatherId = motherId = 0;
string data = person.Sex; string data = person.Sex;
TreeNode<TreeData> citem; TreeNode<TreeData> citem;
string pName = ""; string pName = "";
@@ -371,6 +371,7 @@ public partial class PersonRepository : IPerson
pName = pe.FirstName; pName = pe.FirstName;
image = pe.Image; image = pe.Image;
key = motherId.ToString(); key = motherId.ToString();
personId = motherId;
data = pe.Sex; data = pe.Sex;
} }
} }
@@ -382,6 +383,7 @@ public partial class PersonRepository : IPerson
pName = pe.FirstName; pName = pe.FirstName;
image = pe.Image; image = pe.Image;
key = fatherId.ToString(); key = fatherId.ToString();
personId = fatherId;
data = pe.Sex; data = pe.Sex;
} }
} }
@@ -390,7 +392,7 @@ public partial class PersonRepository : IPerson
citem.Data = new(); citem.Data = new();
citem.Label = pName; citem.Label = pName;
if (image != null) if (image != null)
citem.Data.Image = await LoadPersonImage(image); citem.Data.Image = await LoadPersonImage(personId,image);
citem.Icon = "P"; citem.Icon = "P";
@@ -411,7 +413,7 @@ public partial class PersonRepository : IPerson
child.Type = type; child.Type = type;
image = dto.Image; image = dto.Image;
if (image != null) if (image != null)
child.Data.Image = await LoadPersonImage(image); child.Data.Image = await LoadPersonImage(dto.Id,image);
child.Icon = "C"; child.Icon = "C";
child.StyleClass = getStyleClass(child.Icon); child.StyleClass = getStyleClass(child.Icon);
child.Label = dto.FirstName; child.Label = dto.FirstName;
@@ -480,12 +482,12 @@ public partial class PersonRepository : IPerson
/// <param name="fileName"></param> /// <param name="fileName"></param>
/// <param name="familyId"></param> /// <param name="familyId"></param>
/// <param name="base64"></param> /// <param name="base64"></param>
private string SaveFile(string fileName, int familyId, string base64) private async Task<string> SaveFile(string fileName, int personId, string base64)
{ {
string path = ""; string path = "";
int statusCode = 0; int statusCode = 0;
string error = ""; // string error = "";
string sdate = GetCurrentDateTime(); string sdate = GetCurrentDateTime();
string filename, extention; string filename, extention;
filename = extention = ""; filename = extention = "";
@@ -494,14 +496,33 @@ public partial class PersonRepository : IPerson
string rbase = base64.Substring(first, last - first); string rbase = base64.Substring(first, last - first);
byte[] newBytes = Convert.FromBase64String(rbase); 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"]; path = _config["AppSettings:ImageFolder"];
if (!Directory.Exists(path)) if (!Directory.Exists(path))
{ {
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
} }
extention = System.IO.Path.GetExtension(fileName);
filename = familyId + "_" + sdate + extention;
string fullpath = System.IO.Path.Combine(path, filename); string fullpath = System.IO.Path.Combine(path, filename);
/* /*
using (var fileStream = new FileStream(fullpath, FileMode.Create)) 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)) using (FileStream fileStream = new FileStream(fullpath, FileMode.Create, FileAccess.Write, FileShare.None))
{ {
fileStream.Write(newBytes, 0, newBytes.Length); fileStream.Write(newBytes, 0, newBytes.Length);
} }
*/
return filename; return filename;
} }
@@ -571,7 +594,7 @@ public partial class PersonRepository : IPerson
if (!string.IsNullOrEmpty(container.FileName)) 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 // update the image in table to new
model.Image = image; model.Image = image;
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
@@ -587,7 +610,7 @@ public partial class PersonRepository : IPerson
result = item.Id; result = item.Id;
if (!string.IsNullOrEmpty(container.FileName)) 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 // update the image in table to new
model1.Image = image; model1.Image = image;
} }
@@ -714,17 +737,34 @@ public partial class PersonRepository : IPerson
}; };
} }
private async Task<string> LoadPersonImage(string filename) private async Task<string> LoadPersonImage_old(string filename)
{ {
string result = ""; string result = "";
DownloadFileCriteria criteria = new() { FileName = filename }; DownloadFileCriteria criteria = new() { FileName = filename };
var resultModel = await _importPersonRepository.DownloadFile(criteria); var resultModel = await _importPersonRepository.DownloadFile(criteria);
if (!string.IsNullOrEmpty((resultModel.Data)))
if (!string.IsNullOrEmpty(resultModel.Data))
result = "data:image/png;base64," + resultModel.Data; result = "data:image/png;base64," + resultModel.Data;
return result; return result;
} }
private async Task<string> 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<int> DeleteUploadFile(DeleteFileCriteria criteria) public ResultModel<int> DeleteUploadFile(DeleteFileCriteria criteria)
{ {
int result = -1; int result = -1;