DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Attachments/Providers/UploadService.cs

202 lines
8.9 KiB
C#
Raw Permalink Normal View History

2023-08-15 22:52:30 -05:00
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<UploadService> logger;
private IMapper mapper;
private string uploadpath = "";
private string Deletepath = "";
public UploadService(IConfiguration configuration, ILogger<UploadService> logger, IMapper mapper)
{
this.logger = logger;
this.mapper = mapper;
uploadpath = configuration.GetValue<string>("Fileupload:folderpath");
Deletepath = configuration.GetValue<string>("Fileupload:Deletepath");
}
public async Task<string> GetFile(string path)
{
try
{
if (System.IO.File.Exists(path))
{
return path;
}
return null; // File not found
}
catch (Exception ex)
{
// Handle or log the exception as needed
throw;
}
}
2023-12-01 17:20:11 -05:00
public async Task<byte[]> DownloadFile(string path)
{
try
{
if (System.IO.File.Exists(path))
{
return await System.IO.File.ReadAllBytesAsync(path);
}
return null; // File not found
}
catch (Exception ex)
{
// Handle or log the exception as needed
throw;
}
}
2023-08-25 17:24:46 -05:00
public List<Models.Attachment> UploadAttachment(int responseId,int answerId,int counter, List<IFormFile> postedFile)
2023-08-15 22:52:30 -05:00
{
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);
}
}
2023-08-25 17:24:46 -05:00
List<Models.Attachment> attachments = new List<Models.Attachment>();
2023-08-15 22:52:30 -05:00
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);
}
2023-08-25 17:24:46 -05:00
attachments.Add(new Models.Attachment { AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath });
2023-08-15 22:52:30 -05:00
}
return attachments;
}
2023-08-25 17:24:46 -05:00
public List<Models.Attachment> UploadAttachment(int responseId, int counter,List<AnswerInfo> answers)
2023-08-15 22:52:30 -05:00
{
2023-08-25 17:24:46 -05:00
List<Models.Attachment> attachments = new List<Models.Attachment>();
2023-08-15 22:52:30 -05:00
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++;
2023-12-01 17:20:11 -05:00
var UserfileName = Path.GetFileName(file.FileName+ file.FileExtension);
2023-08-15 22:52:30 -05:00
var fileName = String.Format("Attachment_{0}{1}", counter, file.FileExtension);
var dbPath = Path.Combine(fullDirectoryPath, fileName);
File.WriteAllBytes(dbPath, Convert.FromBase64String(file.FileContent));
2023-08-25 17:24:46 -05:00
attachments.Add(new Models.Attachment { AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath });
2023-08-15 22:52:30 -05:00
}
}
return attachments;
}
catch (Exception ex) {
2023-08-25 17:24:46 -05:00
return new List<Models.Attachment>();
2023-08-15 22:52:30 -05:00
}
}
2023-08-25 17:24:46 -05:00
public List<Models.Attachment> UpdateAttachments(int responseId,List<AnswerInfo> answers,IEnumerable<Models.Attachment> attachments)
2023-08-15 22:52:30 -05:00
{
2023-08-25 17:24:46 -05:00
List<Models.Attachment> Dbattachments = new List<Models.Attachment>();
2023-08-15 22:52:30 -05:00
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();
2023-12-01 17:20:11 -05:00
var UserfileName = Path.GetFileName(file.FileName + file.FileExtension);
2023-08-15 22:52:30 -05:00
var fileName = String.Format("Attachment_{0}{1}", attachment?.Id, file.FileExtension);
var dbPath = Path.Combine(fullDirectoryPath, fileName);
File.WriteAllBytes(dbPath, Convert.FromBase64String(file.FileContent));
2023-08-25 17:24:46 -05:00
Dbattachments.Add(new Models.Attachment { Id=attachment.Id, AnswerId = answerId, ResponseId = responseId, IsDeleted = false, FileName = UserfileName, URI = dbPath });
2023-08-15 22:52:30 -05:00
}
}
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);
}
}
}
}
}