using FamilyTreeAPI.Entities; using FamilyTreeAPI.Repository; namespace FamilyTreeAPI.Controllers; using DocumentFormat.OpenXml.Drawing.Charts; using FamilyTreeAPI.Interface; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using System.IO; using System.Threading.Tasks; [ApiController] [Route("api/[controller]")] public class FileUploadController : ControllerBase { private readonly IWebHostEnvironment _hostingEnvironment; private readonly ImportPersonRepository _importPersonRepository; private readonly IConfiguration _config; private readonly IPersonPhoto _personPhoto; public FileUploadController(IWebHostEnvironment hostingEnvironment, IPersonPhoto personPhoto, ImportPersonRepository importPersonRepository, IConfiguration config) { _hostingEnvironment = hostingEnvironment; _importPersonRepository = importPersonRepository; _config = config; _personPhoto = personPhoto; } [HttpPost("[action]")] public async Task UploadFile(IFormFile file) { List> output = new (); if (file == null || file.Length == 0) { return BadRequest("No file uploaded."); } try { // Define the upload directory // var uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "uploads"); string importFolder = _config.GetValue("AppSettings:ImportFolder"); var uploadsFolder = Path.Combine(_hostingEnvironment.ContentRootPath, importFolder); if (!Directory.Exists(uploadsFolder)) { Directory.CreateDirectory(uploadsFolder); } // Create a unique file name to avoid overwriting var uniqueFileName = Path.GetFileNameWithoutExtension(file.FileName) + "_" + System.Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var filePath = Path.Combine(uploadsFolder, uniqueFileName); // Save the file to the server using (var stream = new FileStream(filePath, FileMode.Create)) { // await file.CopyToAsync(stream); await file.CopyToAsync(stream); // output = await _importPersonRepository.ImportPerson(stream, "Sheet1"); } using (var stream = new MemoryStream()) { await file.CopyToAsync(stream); output = await _importPersonRepository.ImportPerson(stream, "Sheet1"); } //return Ok(new { FileName = uniqueFileName, FilePath = filePath }); return Ok(output); } catch (System.Exception ex) { return StatusCode(500, $"Internal server error: {ex.Message}"); } } [HttpPost("[action]")] public async Task DownloadFile(DownloadFileCriteria criteria) { var rev = await _importPersonRepository.DownloadFile(criteria); return Ok(rev); } [HttpPost("[action]")] public async Task SavePersonPhoto(List files) { var keys = Request.Form; var ffiles = Request.Form.Files; ResultModel ret = new(); if (files.Count > 0) { StringValues sdocCId = ""; keys.TryGetValue("personId", out sdocCId); for (int i = 0; i < files.Count; i++) { var file = files[i]; UploadCriteria criteria = new(); criteria.File = file; criteria.PersonId = int.Parse(sdocCId); criteria.FileName = file.FileName; ret = await _personPhoto.SaveAsync(criteria); } } return Ok(ret); } [HttpPost("[action]")] public async Task DownloadPersonPhotoById(DeleteCriteria criteria) { var result = await _personPhoto.DownloadPersonPhotoById(criteria.Id); if (result.StatusCode > 0) return File(result.Data.Content, result.Data.ContentType, result.Data.FileName); else return NotFound("not found image"); } [HttpPost("[action]")] public async Task DownloadPersonPhotoPerson(DownloadFileCriteria criteria) { var result = await _personPhoto.DownloadPersonPhotoPerson(criteria.Id,criteria.PhotoType ,criteria.FileName); if (result.StatusCode > 0) return File(result.Data.Content, result.Data.ContentType, result.Data.FileName); else return NotFound("not found image"); } [HttpPost("[action]")] public async Task DownloadPersonPhoto64(DownloadFileCriteria criteria) { var result = await _personPhoto.DownloadPersonPhotoPerson(criteria.Id,criteria.PhotoType ,criteria.FileName); if (result.StatusCode > 0) { byte[] file64 = result.Data.Content; string rval = Convert.ToBase64String(file64); ResultModel retval = new() { Data = rval, StatusCode = 1, Message = "" }; return Ok(retval); } else return NotFound("not found image"); } [HttpPost("[action]")] public async Task DeletePersonPhoto(DeleteFileCriteria criteria) { ResultModel ret = new(); if ( criteria.Id > 0) { ret = await _personPhoto.DeletePersonPhoto(criteria.Id, criteria.PhotoType); } return Ok(ret); } [HttpPost("[action]")] public async Task DeletePersonPhotoById(DeleteCriteria criteria) { ResultModel ret = new(); ret = await _personPhoto.DeletePersonPhotoById(criteria.Id); return Ok(ret); } }