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

124 lines
5.1 KiB
C#
Raw Normal View History

2023-08-31 18:00:51 -05:00
using AutoMapper;
using Azure;
using DamageAssesment.Api.Documents.Db;
using DamageAssesment.Api.Documents.Interfaces;
using DamageAssesment.Api.Documents.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.Diagnostics.Metrics;
using System.Net.Http;
using System.Security.AccessControl;
using System.Security.Principal;
namespace DamageAssesment.Api.Documents.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 Models.Document UploadDocument(int counter, DocumentInfo documentInfo)
{
Models.Document Documents = new Models.Document();
try
{
string path = "", UserfileName="";
if (documentInfo.File != null)
{
counter++;
var fullDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), uploadpath);
if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist
Directory.CreateDirectory(fullDirectoryPath);
UserfileName = Path.GetFileName(documentInfo.File.FileName);
var fileName = String.Format("Document_{0}{1}", counter, documentInfo.File.FileExtension);
path = Path.Combine(fullDirectoryPath, fileName);
File.WriteAllBytes(path, Convert.FromBase64String(documentInfo.File.FileContent));
}
Documents=new Models.Document (){ linkTypeId = documentInfo.linkTypeId,
documentsTranslations = documentInfo.documentsTranslations,
docName = UserfileName,
url = documentInfo.url, Path = path,IsActive =true,dateCreated=DateTime.Now,dateUpdated=DateTime.Now};
return Documents;
}
catch (Exception ex) {
return new Models.Document();
}
}
public Models.Document UpdateDocuments(Models.Document document, DocumentInfo documentInfo)
{
try
{
Deletefile(document.Path);
var fullDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), uploadpath);
if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist
Directory.CreateDirectory(fullDirectoryPath);
string path = "", UserfileName = "";
if (documentInfo.File != null)
{
UserfileName = Path.GetFileName(documentInfo.File.FileName);
var fileName = String.Format("Document_{0}{1}", documentInfo.Id, documentInfo.File.FileExtension);
path = Path.Combine(fullDirectoryPath, fileName);
File.WriteAllBytes(path, Convert.FromBase64String(documentInfo.File.FileContent));
}
Models.Document Documents = new Models.Document()
{
Id = documentInfo.Id,
linkTypeId = documentInfo.linkTypeId,
documentsTranslations=documentInfo.documentsTranslations,
docName = UserfileName,
url = documentInfo.url,
Path = path,
IsActive = true,
dateCreated = document.dateCreated,
dateUpdated = DateTime.Now
};
return Documents;
}
catch (Exception ex) {
return new Models.Document();
}
}
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);
}
}
}
}
}