using AutoMapper; using Azure; using DamageAssesment.Api.Attachments.Db; using DamageAssesment.Api.Attachments.Interfaces; using DamageAssesment.Api.Attachments.Models; using Microsoft.AspNetCore.Http; using System.Diagnostics.Metrics; using System.Net.Http; using System.Security.AccessControl; using System.Security.Principal; namespace DamageAssesment.Api.Attachments.Providers { public class UploadService : IUploadService { private ILogger logger; private IMapper mapper; private string uploadpath = ""; private string Deletepath = ""; public UploadService(IConfiguration configuration, ILogger logger, IMapper mapper) { this.logger = logger; this.mapper = mapper; uploadpath = configuration.GetValue("Fileupload:folderpath"); Deletepath = configuration.GetValue("Fileupload:Deletepath"); } public List UploadAttachment(int responseId,int answerId,int counter, List postedFile) { var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), uploadpath); String responseDirectory = "Response-" + responseId; String fullDirectoryPath = Path.Combine(pathToSave, responseDirectory); if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist Directory.CreateDirectory(fullDirectoryPath); fullDirectoryPath = Path.Combine(fullDirectoryPath, "Answer-" + answerId); if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist Directory.CreateDirectory(fullDirectoryPath); String[] searchFiles = Directory.GetFiles(fullDirectoryPath); //Search for existing answer files if (searchFiles.Length > 0) { foreach (String searchFile in searchFiles) { Deletefile(searchFile); } } List attachments = new List(); foreach (IFormFile item in postedFile) { counter++; var UserfileName = Path.GetFileName(item.FileName); var extension = System.IO.Path.GetExtension(UserfileName); var fileName = String.Format("Attachment_{0}{1}", counter, extension); var dbPath = Path.Combine(fullDirectoryPath, fileName); using (var stream = new FileStream(dbPath, FileMode.Create, FileAccess.ReadWrite)) { item.CopyTo(stream); } attachments.Add(new Models.Attachment { AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath }); } return attachments; } public List UploadAttachment(int responseId, int counter,List answers) { List attachments = new List(); try { foreach (var item in answers) { int answerId = item.AnswerId; var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), uploadpath); String responseDirectory = "Response-" + responseId; String fullDirectoryPath = Path.Combine(pathToSave, responseDirectory); if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist Directory.CreateDirectory(fullDirectoryPath); fullDirectoryPath = Path.Combine(fullDirectoryPath, "Answer-" + answerId); if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist Directory.CreateDirectory(fullDirectoryPath); //String[] searchFiles = Directory.GetFiles(fullDirectoryPath); //Search for existing answer files //if (searchFiles.Length > 0) //{ // foreach (String searchFile in searchFiles) // { // Deletefile(searchFile); // } //} foreach (var file in item.postedFiles) { counter++; var UserfileName = Path.GetFileName(file.FileName); var fileName = String.Format("Attachment_{0}{1}", counter, file.FileExtension); var dbPath = Path.Combine(fullDirectoryPath, fileName); File.WriteAllBytes(dbPath, Convert.FromBase64String(file.FileContent)); attachments.Add(new Models.Attachment { AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath }); } } return attachments; } catch (Exception ex) { return new List(); } } public List UpdateAttachments(int responseId,List answers,IEnumerable attachments) { List Dbattachments = new List(); foreach (Models.Attachment searchFile in attachments) { Deletefile(searchFile.URI); } foreach (var item in answers) { int answerId = item.AnswerId; var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), uploadpath); String responseDirectory = "Response-" + responseId; String fullDirectoryPath = Path.Combine(pathToSave, responseDirectory); if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist Directory.CreateDirectory(fullDirectoryPath); fullDirectoryPath = Path.Combine(fullDirectoryPath, "Answer-" + answerId); if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist Directory.CreateDirectory(fullDirectoryPath); foreach (var file in item.postedFiles) { Models.Attachment attachment= attachments.Where(a=>a.Id == file.AttachmentId).FirstOrDefault(); var UserfileName = Path.GetFileName(file.FileName); var fileName = String.Format("Attachment_{0}{1}", attachment?.Id, file.FileExtension); var dbPath = Path.Combine(fullDirectoryPath, fileName); File.WriteAllBytes(dbPath, Convert.FromBase64String(file.FileContent)); Dbattachments.Add(new Models.Attachment { Id=attachment.Id, AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath }); } } return Dbattachments; } public void Deletefile(string path) { if (path != "") { FileInfo file = new FileInfo(path); if (file?.Exists??false)//check file exsit or not { file.Delete(); } } } public void Movefile(string path) { if (path != "") { var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), Deletepath); if (!Directory.Exists(pathToSave)) //Create deirectory if does not exist Directory.CreateDirectory(pathToSave); FileInfo file = new FileInfo(path); if (file?.Exists ?? false)//check file exsit or not { string filename = file.Name.Replace(file.Extension, " ") + DateTime.Now.ToShortDateString().Replace("/","_") + file.Extension; file.MoveTo(pathToSave+"\\"+ filename); } } } } }