forked from MDCPS/DamageAssessment_Backend
		
	attachment and doculinks url changes
This commit is contained in:
		| @ -13,25 +13,33 @@ namespace DamageAssesment.Api.Attachments.Providers | ||||
|         private AttachmentsDbContext AttachmentDbContext; | ||||
|         private ILogger<AttachmentsProvider> logger; | ||||
|         private IUploadService uploadservice; | ||||
|         private IMapper mapper; | ||||
|  | ||||
|         public AttachmentsProvider(AttachmentsDbContext AttachmentDbContext, ILogger<AttachmentsProvider> logger, IMapper mapper,IUploadService uploadservice) | ||||
|         private IMapper mapper;  | ||||
|         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; | ||||
|             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() | ||||
|         { | ||||
|  | ||||
|              | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Query Question"); | ||||
|                 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)); | ||||
|  | ||||
		Reference in New Issue
	
	Block a user