180 lines
7.1 KiB
C#
180 lines
7.1 KiB
C#
using AutoMapper;
|
|
using Azure;
|
|
using DamageAssesment.Api.DocuLinks.Db;
|
|
using DamageAssesment.Api.DocuLinks.Interfaces;
|
|
using DamageAssesment.Api.DocuLinks.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.DocuLinks.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;
|
|
}
|
|
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
public Models.Doculink UploadDocument(int counter, ReqDoculink documentInfo)
|
|
{
|
|
Models.Doculink Documents = new Models.Doculink();
|
|
List<Models.DoculinkAttachments> attachments = new List<Models.DoculinkAttachments>();
|
|
try
|
|
{
|
|
string path = "", UserfileName="";
|
|
var fullDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), uploadpath);
|
|
if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist
|
|
Directory.CreateDirectory(fullDirectoryPath);
|
|
if (documentInfo.Files != null)
|
|
{
|
|
|
|
foreach (var item in documentInfo.Files)
|
|
{
|
|
counter++;
|
|
if (item.IsAttachments)
|
|
{
|
|
UserfileName = Path.GetFileName(item.FileName + item.FileExtension);
|
|
var fileName = String.Format("Document_{0}{1}", counter, item.FileExtension);
|
|
path = Path.Combine(fullDirectoryPath, fileName);
|
|
File.WriteAllBytes(path, Convert.FromBase64String(item.FileContent));
|
|
}
|
|
else
|
|
path = item.url;
|
|
attachments.Add(new Models.DoculinkAttachments { docName=UserfileName,Path=path,IsAttachments=item.IsAttachments,CustomOrder=item.CustomOrder,Language=item.Language });
|
|
}
|
|
}
|
|
Documents=new Models.Doculink (){ linkTypeId = documentInfo.linkTypeId,
|
|
documentsTranslations = documentInfo.documentsTranslations,doclinksAttachments=attachments,
|
|
IsDeleted=false,CustomOrder=documentInfo.CustomOrder, IsActive =documentInfo.IsActive
|
|
};
|
|
|
|
return Documents;
|
|
}
|
|
catch (Exception ex) {
|
|
return new Models.Doculink();
|
|
}
|
|
|
|
|
|
}
|
|
public Models.Doculink UpdateDocuments(int counter, Models.Doculink document, ReqDoculink documentInfo)
|
|
{
|
|
try
|
|
{
|
|
foreach (var item in document.doclinksAttachments)
|
|
{
|
|
Movefile(item.Path);
|
|
}
|
|
var fullDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), uploadpath);
|
|
if (!Directory.Exists(fullDirectoryPath)) //Create deirectory if does not exist
|
|
Directory.CreateDirectory(fullDirectoryPath);
|
|
|
|
string path = "", UserfileName = "";
|
|
List<Models.DoculinkAttachments> attachments = new List<Models.DoculinkAttachments>();
|
|
int counter1 = 1;
|
|
foreach (var item in documentInfo.Files)
|
|
{
|
|
if (item.IsAttachments)
|
|
{
|
|
UserfileName = Path.GetFileName(item.FileName+item.FileExtension);
|
|
var fileName = String.Format("Document_{0}{1}", counter1, item.FileExtension);
|
|
path = Path.Combine(fullDirectoryPath, fileName);
|
|
File.WriteAllBytes(path, Convert.FromBase64String(item.FileContent));
|
|
counter1++;
|
|
}
|
|
else
|
|
path = item.url;
|
|
attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path,IsAttachments=item.IsAttachments,CustomOrder=item.CustomOrder,Language=item.Language });
|
|
}
|
|
Models.Doculink Documents = new Models.Doculink()
|
|
{
|
|
Id = documentInfo.Id,
|
|
linkTypeId = documentInfo.linkTypeId,
|
|
documentsTranslations=documentInfo.documentsTranslations,
|
|
IsActive = documentInfo.IsActive,
|
|
IsDeleted=false,
|
|
CustomOrder = documentInfo.CustomOrder,
|
|
doclinksAttachments = attachments
|
|
};
|
|
|
|
return Documents;
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
return new Models.Doculink();
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|