Merged PR 73: attachment and doculinks url changes
attachment and doculinks url changes
This commit is contained in:
commit
8c12477763
@ -139,6 +139,30 @@ namespace DamageAssesment.Api.Attachments.Controllers
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// download an existing attachment.
|
||||
/// </summary>
|
||||
[Authorize(Roles = "admin")]
|
||||
[HttpGet("attachments/download/{id}")]
|
||||
public async Task<IActionResult> downloadfile1(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await this.AttachmentProvider.GetDownloadAttachmentAsync(id);
|
||||
if(!result.IsSuccess)
|
||||
return NotFound();
|
||||
byte[] fileContent = await UploadService.DownloadFile(result.Attachment.URI);
|
||||
if (fileContent == null || fileContent.Length == 0)
|
||||
return NotFound();
|
||||
var contentType = "application/octet-stream";
|
||||
return File(fileContent, contentType, result.Attachment.FileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Handle the exception here or log it
|
||||
return StatusCode(500, "An error occurred: " + ex.Message);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Delete an existing attachment.
|
||||
/// </summary>
|
||||
[Authorize(Roles = "admin")]
|
||||
|
@ -9,6 +9,7 @@ namespace DamageAssesment.Api.Attachments.Interfaces
|
||||
Task<(bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage)> PostAttachmentAsync(List<Models.Attachment> Attachments);
|
||||
Task<(bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage)> PutAttachmentAsync(List<Models.Attachment> Attachments);
|
||||
Task<(bool IsSuccess, Models.Attachment Attachment, string Path)> DeleteAttachmentAsync(int Id);
|
||||
Task<(bool IsSuccess, Models.Attachment Attachment, string Path)> GetDownloadAttachmentAsync(int Id);
|
||||
Task<(bool IsSuccess, int counter, string Path)> DeleteAttachmentsAsync(int responseId, int answerId);
|
||||
Task<(bool IsSuccess, int counter, string Path)> DeleteBulkAttachmentsAsync(int responseId, List<int> answerIds);
|
||||
Task<(bool IsSuccess, int counter, string message)> GetAttachmentCounter();
|
||||
|
@ -7,6 +7,7 @@ namespace DamageAssesment.Api.Attachments.Interfaces
|
||||
List<Models.Attachment> UploadAttachment(int responseId,int answerId, int counter, List<IFormFile> postedFile);
|
||||
List<Models.Attachment> UploadAttachment(int responseId, int counter, List<AnswerInfo> answers);
|
||||
public List<Models.Attachment> UpdateAttachments(int responseId, List<AnswerInfo> answers, IEnumerable<Models.Attachment> attachments);
|
||||
Task<byte[]> DownloadFile(string path);
|
||||
void Deletefile(string path);
|
||||
void Movefile(string path);
|
||||
}
|
||||
|
@ -75,6 +75,7 @@ builder.Services.AddSwaggerGen(options =>
|
||||
|
||||
options.AddSecurityRequirement(securityRequirements);
|
||||
});
|
||||
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
builder.Services.AddScoped<IAttachmentsProvider, AttachmentsProvider>();
|
||||
builder.Services.AddScoped<IUploadService, UploadService>();
|
||||
builder.Services.AddScoped<IAzureBlobService,AzureBlobService>();
|
||||
|
@ -14,14 +14,18 @@ namespace DamageAssesment.Api.Attachments.Providers
|
||||
private ILogger<AttachmentsProvider> logger;
|
||||
private IUploadService uploadservice;
|
||||
private IMapper mapper;
|
||||
|
||||
public AttachmentsProvider(AttachmentsDbContext AttachmentDbContext, ILogger<AttachmentsProvider> logger, IMapper mapper,IUploadService uploadservice)
|
||||
private readonly IHttpContextAccessor httpContextAccessor;
|
||||
private string baseUrl;
|
||||
public AttachmentsProvider(AttachmentsDbContext AttachmentDbContext, ILogger<AttachmentsProvider> logger, IMapper mapper,IUploadService uploadservice, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
this.AttachmentDbContext = AttachmentDbContext;
|
||||
this.logger = logger;
|
||||
this.mapper = mapper;
|
||||
this.uploadservice = uploadservice;
|
||||
//SeedData();
|
||||
this.httpContextAccessor = httpContextAccessor;
|
||||
baseUrl = $"{httpContextAccessor.HttpContext.Request.Scheme}://{httpContextAccessor.HttpContext.Request.Host}";
|
||||
baseUrl = baseUrl + "/attachments/download";
|
||||
// SeedData();
|
||||
}
|
||||
public async Task<(bool IsSuccess, IEnumerable<Models.Attachment> Attachments, string ErrorMessage)> GetAttachmentsAsync()
|
||||
{
|
||||
@ -32,6 +36,10 @@ namespace DamageAssesment.Api.Attachments.Providers
|
||||
var Attachment = await AttachmentDbContext.Attachments.AsNoTracking().Where(a => !a.IsDeleted).ToListAsync();
|
||||
if (Attachment != null)
|
||||
{
|
||||
foreach (var attachment in Attachment)
|
||||
{
|
||||
attachment.URI = $"{baseUrl}/{attachment.Id}";
|
||||
}
|
||||
logger?.LogInformation($"{Attachment.Count} Attachments(s) found");
|
||||
var result = mapper.Map<IEnumerable<Db.Attachment>, IEnumerable<Models.Attachment>>(Attachment);
|
||||
return (true, result, null);
|
||||
@ -54,6 +62,7 @@ namespace DamageAssesment.Api.Attachments.Providers
|
||||
if (Attachment != null)
|
||||
{
|
||||
logger?.LogInformation($"{Attachment} customer(s) found");
|
||||
Attachment.URI = $"{baseUrl}/{Attachment.Id}";
|
||||
var result = mapper.Map<Db.Attachment, Models.Attachment>(Attachment);
|
||||
return (true, result, null);
|
||||
}
|
||||
@ -73,6 +82,10 @@ namespace DamageAssesment.Api.Attachments.Providers
|
||||
List<Db.Attachment> attachments = mapper.Map<List<Models.Attachment>, List<Db.Attachment>>(Attachments);
|
||||
AttachmentDbContext.Attachments.AddRange(attachments);
|
||||
await AttachmentDbContext.SaveChangesAsync();
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
attachment.URI = $"{baseUrl}/{attachment.Id}";
|
||||
}
|
||||
var result = mapper.Map<IEnumerable<Db.Attachment>, IEnumerable<Models.Attachment>>(attachments);
|
||||
return (true, result, null);
|
||||
}
|
||||
@ -91,6 +104,10 @@ namespace DamageAssesment.Api.Attachments.Providers
|
||||
List<Db.Attachment> attachments = mapper.Map<List<Models.Attachment>, List<Db.Attachment>>(Attachments);
|
||||
AttachmentDbContext.Attachments.UpdateRange(attachments);
|
||||
await AttachmentDbContext.SaveChangesAsync();
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
attachment.URI = $"{baseUrl}/{attachment.Id}";
|
||||
}
|
||||
var result = mapper.Map<IEnumerable<Db.Attachment>, IEnumerable<Models.Attachment>>(attachments);
|
||||
return (true, result, null);
|
||||
}
|
||||
@ -197,6 +214,24 @@ namespace DamageAssesment.Api.Attachments.Providers
|
||||
{
|
||||
return AttachmentDbContext.Attachments.AsNoTracking().Count(e => e.Id == id && !e.IsDeleted) > 0;
|
||||
}
|
||||
public async Task<(bool IsSuccess, Models.Attachment Attachment, string Path)> GetDownloadAttachmentAsync(int Id)
|
||||
{
|
||||
try
|
||||
{
|
||||
Db.Attachment Attachment = AttachmentDbContext.Attachments.Where(a => a.Id == Id).AsNoTracking().FirstOrDefault();
|
||||
if (Attachment == null)
|
||||
{
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
return (true, mapper.Map<Db.Attachment, Models.Attachment>(Attachment), $"Attachment {Id}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private void SeedData()
|
||||
{
|
||||
|
@ -24,6 +24,23 @@ namespace DamageAssesment.Api.Attachments.Providers
|
||||
uploadpath = configuration.GetValue<string>("Fileupload:folderpath");
|
||||
Deletepath = configuration.GetValue<string>("Fileupload:Deletepath");
|
||||
}
|
||||
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 List<Models.Attachment> UploadAttachment(int responseId,int answerId,int counter, List<IFormFile> postedFile)
|
||||
{
|
||||
var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), uploadpath);
|
||||
@ -89,7 +106,7 @@ namespace DamageAssesment.Api.Attachments.Providers
|
||||
{
|
||||
counter++;
|
||||
|
||||
var UserfileName = Path.GetFileName(file.FileName);
|
||||
var UserfileName = Path.GetFileName(file.FileName+ file.FileExtension);
|
||||
var fileName = String.Format("Attachment_{0}{1}", counter, file.FileExtension);
|
||||
var dbPath = Path.Combine(fullDirectoryPath, fileName);
|
||||
File.WriteAllBytes(dbPath, Convert.FromBase64String(file.FileContent));
|
||||
@ -126,7 +143,7 @@ namespace DamageAssesment.Api.Attachments.Providers
|
||||
foreach (var file in item.postedFiles)
|
||||
{
|
||||
Models.Attachment attachment= attachments.Where(a=>a.Id == file.AttachmentId).FirstOrDefault();
|
||||
var UserfileName = Path.GetFileName(file.FileName);
|
||||
var UserfileName = Path.GetFileName(file.FileName + file.FileExtension);
|
||||
var fileName = String.Format("Attachment_{0}{1}", attachment?.Id, file.FileExtension);
|
||||
var dbPath = Path.Combine(fullDirectoryPath, fileName);
|
||||
File.WriteAllBytes(dbPath, Convert.FromBase64String(file.FileContent));
|
||||
|
@ -111,7 +111,31 @@ namespace DamageAssesment.Api.DocuLinks.Controllers
|
||||
return NotFound();
|
||||
}
|
||||
/// <summary>
|
||||
/// Get all documents.
|
||||
/// download an existing attachment.
|
||||
/// </summary>
|
||||
[Authorize(Roles = "admin")]
|
||||
[HttpGet("doculinks/download/{id}")]
|
||||
public async Task<IActionResult> downloadfile1(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await this.documentsProvider.GetDownloadAttachmentAsync(id);
|
||||
if (!result.IsSuccess)
|
||||
return NotFound();
|
||||
byte[] fileContent = await uploadService.DownloadFile(result.DoculinkAttachments.Path);
|
||||
if (fileContent == null || fileContent.Length == 0)
|
||||
return NotFound();
|
||||
var contentType = "application/octet-stream";
|
||||
return File(fileContent, contentType, result.DoculinkAttachments.docName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Handle the exception here or log it
|
||||
return StatusCode(500, "An error occurred: " + ex.Message);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Get all Doculink.
|
||||
/// </summary>
|
||||
|
||||
[Authorize(Roles = "admin")]
|
||||
|
@ -12,6 +12,7 @@ namespace DamageAssesment.Api.DocuLinks.Interfaces
|
||||
Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> PostDocumentAsync(Models.Doculink Document);
|
||||
Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> UpdateDocumentAsync(int id, Models.Doculink Document);
|
||||
Task<(bool IsSuccess, Models.ResDoculink Document, string ErrorMessage)> DeleteDocumentAsync(int id);
|
||||
Task<(bool IsSuccess, Models.DoculinkAttachments DoculinkAttachments, string Path)> GetDownloadAttachmentAsync(int id);
|
||||
Task<(bool IsSuccess, int counter, string message)> GetDocumentCounter();
|
||||
|
||||
|
||||
|
@ -7,6 +7,7 @@ namespace DamageAssesment.Api.DocuLinks.Interfaces
|
||||
Models.Doculink UploadDocument( int counter, ReqDoculink documentInfo);
|
||||
public Models.Doculink UpdateDocuments(int counter, Models.Doculink document, ReqDoculink documentInfo);
|
||||
void Deletefile(string path);
|
||||
Task<byte[]> DownloadFile(string path);
|
||||
void Movefile(string path);
|
||||
}
|
||||
}
|
||||
|
@ -72,6 +72,7 @@ builder.Services.AddSwaggerGen(options =>
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
builder.Services.AddScoped<IDoculinkProvider, DoculinkProvider>();
|
||||
builder.Services.AddScoped<IUploadService, UploadService>();
|
||||
builder.Services.AddScoped<IAzureBlobService, AzureBlobService>();
|
||||
|
@ -2,10 +2,12 @@
|
||||
using DamageAssesment.Api.DocuLinks.Db;
|
||||
using DamageAssesment.Api.DocuLinks.Interfaces;
|
||||
using DamageAssesment.Api.DocuLinks.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.Eventing.Reader;
|
||||
using System.Reflection.Metadata;
|
||||
@ -24,13 +26,18 @@ namespace DamageAssesment.Api.DocuLinks.Providers
|
||||
private IUploadService uploadservice;
|
||||
private IAzureBlobService azureBlobService;
|
||||
private IMapper mapper;
|
||||
private readonly IHttpContextAccessor httpContextAccessor;
|
||||
private string baseUrl;
|
||||
|
||||
public DoculinkProvider(DoculinkDbContext DocumentDbContext, ILogger<DoculinkProvider> logger, IMapper mapper, IUploadService uploadservice, IAzureBlobService azureBlobService)
|
||||
public DoculinkProvider(DoculinkDbContext DocumentDbContext, ILogger<DoculinkProvider> logger, IMapper mapper, IUploadService uploadservice, IAzureBlobService azureBlobService, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
this.DocumentDbContext = DocumentDbContext;
|
||||
this.logger = logger;
|
||||
this.mapper = mapper;
|
||||
this.uploadservice = uploadservice;
|
||||
this.httpContextAccessor = httpContextAccessor;
|
||||
baseUrl = $"{httpContextAccessor.HttpContext.Request.Scheme}://{httpContextAccessor.HttpContext.Request.Host}";
|
||||
baseUrl = baseUrl + "/doculinks/download";
|
||||
this.azureBlobService = azureBlobService;
|
||||
//SeedData();
|
||||
}
|
||||
@ -175,15 +182,38 @@ namespace DamageAssesment.Api.DocuLinks.Providers
|
||||
}
|
||||
private List<Models.DoculinkAttachments> GetDocumentAttachment(int id,string? language)
|
||||
{
|
||||
List<Db.DoculinkAttachments> doculinkAttachments = null;
|
||||
if (string.IsNullOrEmpty(language))
|
||||
{
|
||||
return mapper.Map<List<Db.DoculinkAttachments>, List<Models.DoculinkAttachments>>(
|
||||
DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id).ToList());
|
||||
doculinkAttachments = DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
return mapper.Map<List<Db.DoculinkAttachments>, List<Models.DoculinkAttachments>>(
|
||||
DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id && a.Language == language).ToList());
|
||||
doculinkAttachments = DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id && a.Language == language).ToList();
|
||||
}
|
||||
foreach (var attachment in doculinkAttachments)
|
||||
{
|
||||
if (attachment.IsAttachments)
|
||||
attachment.Path = $"{baseUrl}/{attachment.Id}";
|
||||
}
|
||||
return mapper.Map<List<Db.DoculinkAttachments>, List<Models.DoculinkAttachments>>(doculinkAttachments);
|
||||
}
|
||||
public async Task<(bool IsSuccess, Models.DoculinkAttachments DoculinkAttachments, string Path)> GetDownloadAttachmentAsync(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
Db.DoculinkAttachments Attachment = DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.Id == id).AsNoTracking().FirstOrDefault();
|
||||
if (Attachment == null)
|
||||
{
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
return (true, mapper.Map<Db.DoculinkAttachments, Models.DoculinkAttachments>(Attachment), $"Attachment {id}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
public async Task<(bool IsSuccess, IEnumerable<Models.ResDoculink> documents, string ErrorMessage)> GetdocumentsByLinkTypeIdAsync(int? linkTypeId, string? language, bool? isactive)
|
||||
@ -360,7 +390,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers
|
||||
result.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(Document.linkTypeId, ""));
|
||||
result.titles = multilan.titles;
|
||||
result.description = multilan.description;
|
||||
result.doclinksAttachments = Document.doclinksAttachments;
|
||||
result.doclinksAttachments = GetDocumentAttachment(document.Id,"");
|
||||
return (true, result, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -402,7 +432,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers
|
||||
result.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(document.linkTypeId, ""));
|
||||
result.titles = multilan.titles;
|
||||
result.description = multilan.description;
|
||||
result.doclinksAttachments = Document.doclinksAttachments;
|
||||
result.doclinksAttachments = GetDocumentAttachment(document.Id, "");
|
||||
return (true, result, "Successful");
|
||||
}
|
||||
else
|
||||
@ -440,7 +470,8 @@ namespace DamageAssesment.Api.DocuLinks.Providers
|
||||
result.titles = multilan.titles;
|
||||
result.description = multilan.description;
|
||||
result.linktypes = CreateMultiLanguageLinkTypeObject(GetLinkTypeTranslations(result.linkTypeId, ""));
|
||||
result.doclinksAttachments = GetDocumentAttachment(id, "");
|
||||
result.doclinksAttachments = mapper.Map<List<Db.DoculinkAttachments>, List<Models.DoculinkAttachments>>(
|
||||
DocumentDbContext.DoclinksAttachments.AsNoTracking().Where(a => a.DocumentId == id).ToList());
|
||||
Document.IsActive = false;
|
||||
DocumentDbContext.Documents.Update(Document);
|
||||
DocumentDbContext.SaveChanges();
|
||||
|
@ -25,6 +25,23 @@ namespace DamageAssesment.Api.DocuLinks.Providers
|
||||
uploadpath = configuration.GetValue<string>("Fileupload:folderpath");
|
||||
Deletepath = configuration.GetValue<string>("Fileupload:Deletepath");
|
||||
}
|
||||
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)
|
||||
{
|
||||
@ -44,7 +61,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers
|
||||
counter++;
|
||||
if (item.IsAttachments)
|
||||
{
|
||||
UserfileName = Path.GetFileName(item.FileName);
|
||||
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));
|
||||
@ -85,7 +102,7 @@ namespace DamageAssesment.Api.DocuLinks.Providers
|
||||
{
|
||||
if (item.IsAttachments)
|
||||
{
|
||||
UserfileName = Path.GetFileName(item.FileName);
|
||||
UserfileName = Path.GetFileName(item.FileName+item.FileExtension);
|
||||
var fileName = String.Format("Document_{0}_{1}{2}", document.Id, counter1, item.FileExtension);
|
||||
path = Path.Combine(fullDirectoryPath, fileName);
|
||||
File.WriteAllBytes(path, Convert.FromBase64String(item.FileContent));
|
||||
|
Loading…
Reference in New Issue
Block a user