2023-09-22 10:52:17 -05:00
|
|
|
|
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");
|
|
|
|
|
}
|
2023-12-04 09:16:05 -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-09-22 10:52:17 -05:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-12-04 09:16:05 -05:00
|
|
|
|
UserfileName = Path.GetFileName(item.FileName + item.FileExtension);
|
2023-09-22 10:52:17 -05:00
|
|
|
|
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;
|
2023-11-30 19:27:26 -05:00
|
|
|
|
attachments.Add(new Models.DoculinkAttachments { docName=UserfileName,Path=path,IsAttachments=item.IsAttachments,CustomOrder=item.CustomOrder,Language=item.Language });
|
2023-09-22 10:52:17 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Documents=new Models.Doculink (){ linkTypeId = documentInfo.linkTypeId,
|
|
|
|
|
documentsTranslations = documentInfo.documentsTranslations,doclinksAttachments=attachments,
|
|
|
|
|
IsDeleted=false,CustomOrder=documentInfo.CustomOrder, IsActive =true};
|
|
|
|
|
|
|
|
|
|
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>();
|
2023-10-19 14:59:02 -05:00
|
|
|
|
int counter1 = 1;
|
2023-09-22 10:52:17 -05:00
|
|
|
|
foreach (var item in documentInfo.Files)
|
|
|
|
|
{
|
|
|
|
|
if (item.IsAttachments)
|
|
|
|
|
{
|
2023-12-04 09:16:05 -05:00
|
|
|
|
UserfileName = Path.GetFileName(item.FileName+item.FileExtension);
|
2023-10-19 14:59:02 -05:00
|
|
|
|
var fileName = String.Format("Document_{0}_{1}{2}", document.Id, counter1, item.FileExtension);
|
2023-09-22 10:52:17 -05:00
|
|
|
|
path = Path.Combine(fullDirectoryPath, fileName);
|
|
|
|
|
File.WriteAllBytes(path, Convert.FromBase64String(item.FileContent));
|
2023-10-19 14:59:02 -05:00
|
|
|
|
counter1++;
|
2023-09-22 10:52:17 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
path = item.url;
|
2023-11-30 19:27:26 -05:00
|
|
|
|
attachments.Add(new Models.DoculinkAttachments { docName = UserfileName, Path = path,IsAttachments=item.IsAttachments,CustomOrder=item.CustomOrder,Language=item.Language });
|
2023-09-22 10:52:17 -05:00
|
|
|
|
}
|
|
|
|
|
Models.Doculink Documents = new Models.Doculink()
|
|
|
|
|
{
|
|
|
|
|
Id = documentInfo.Id,
|
|
|
|
|
linkTypeId = documentInfo.linkTypeId,
|
|
|
|
|
documentsTranslations=documentInfo.documentsTranslations,
|
|
|
|
|
IsActive = true,
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|