using Azure; using DamageAssesment.Api.Attachments.Interfaces; using DamageAssesment.Api.Attachments.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System.Net.Http.Headers; namespace DamageAssesment.Api.Attachments.Controllers { [ApiController] public class AttachmentsController : ControllerBase { private IAttachmentsProvider AttachmentProvider; private IUploadService UploadService; private IAzureBlobService azureBlobService; public AttachmentsController(IAttachmentsProvider AttachmentsProvider, IUploadService UploadService) { this.AttachmentProvider = AttachmentsProvider; this.UploadService = UploadService; } /// /// Get all attachments. /// [Authorize(Roles = "admin")] [HttpGet("attachments")] public async Task GetAttachmentsAsync() { var result = await AttachmentProvider.GetAttachmentsAsync(); if (result.IsSuccess) { return Ok(result.Attachments); } return NoContent(); } /// /// Get all attachments by attachmentId. /// [Authorize(Roles = "admin")] [HttpGet("attachments/{id}")] public async Task GetAttachmentbyIdAsync(int id) { var result = await AttachmentProvider.GetAttachmentByIdAsync(id); if (result.IsSuccess) { return Ok(result.Attachment); } return NotFound(); } ////Save new Attachment //[HttpPost("Attachments"), DisableRequestSizeLimit] //public async Task UploadAsync(int responseId, int answerId, List postedFile) //{ // try // { // if (postedFile.Count > 0) // { // //Upload logic for all files // var Attachments= await this.AttachmentProvider.DeleteAttachmentsAsync(responseId,answerId); // List attachments = UploadService.UploadAttachment(responseId, answerId, Attachments.counter, postedFile); // //inserting all uploaded files in database // var result = await this.AttachmentProvider.PostAttachmentAsync(attachments); // if (result.IsSuccess) // { // return Ok(result.Attachments); // } // return BadRequest(result.ErrorMessage); // } // return NoContent(); // } // catch (Exception ex) // { // return BadRequest($"Internal server error: {ex}"); // } //} /// /// Save new Attachment(s) /// [Authorize(Roles = "admin")] [HttpPost("attachments"), DisableRequestSizeLimit] public async Task UploadAttachmentAsync(AttachmentInfo attachmentInfo) { try { if (attachmentInfo.Answers.Count > 0) { var Attachments = await this.AttachmentProvider.GetAttachmentCounter(); List attachments = UploadService.UploadAttachment(attachmentInfo.ResponseId, Attachments.counter, attachmentInfo.Answers); var result = await this.AttachmentProvider.PostAttachmentAsync(attachments); if (result.IsSuccess) { return Ok(result.Attachments); } return NoContent(); } return BadRequest(); } catch (Exception ex) { return BadRequest($"Internal server error: {ex}"); } } /// /// Modify an new attachment. /// [Authorize(Roles = "admin")] [HttpPut("attachments"), DisableRequestSizeLimit] public async Task UpdateAttachmentAsync(AttachmentInfo attachmentInfo) { try { if (attachmentInfo.Answers.Count > 0) { var res = await this.AttachmentProvider.GetAttachmentInfo(attachmentInfo.Answers); if (res.IsSuccess) { List attachments = UploadService.UpdateAttachments(attachmentInfo.ResponseId, attachmentInfo.Answers, res.Attachments); var result = await this.AttachmentProvider.PutAttachmentAsync(attachments); if (result.IsSuccess) { return Ok(result.Attachments); } return NoContent(); } return NoContent(); } return BadRequest(); } catch (Exception ex) { return BadRequest($"Internal server error: {ex}"); } } /// /// download an existing attachment. /// [Authorize(Roles = "admin")] [HttpGet("attachments/download/{id}")] public async Task downloadfile(int id) { try { var result = await this.AttachmentProvider.GetDownloadAttachmentAsync(id); if (!result.IsSuccess) return NotFound(); string path = await UploadService.GetFile(result.Attachment.URI); if (path == null) return NotFound(); var contentType = GetContentType(result.Attachment.FileName); if (contentType == "application/octet-stream") return PhysicalFile(path, contentType, result.Attachment.FileName); return PhysicalFile(path, contentType, enableRangeProcessing: true);// result.Attachment.FileName); } catch (Exception ex) { // Handle the exception here or log it return StatusCode(500, "An error occurred: " + ex.Message); } //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); //} } private string GetContentType(string fileName) { // You can add more content types based on the file extensions switch (Path.GetExtension(fileName).ToLower()) { //case ".txt": // return "text/plain"; case ".jpg": case ".jpeg": return "image/jpeg"; case ".png": return "image/png"; case ".gif": return "image/gif"; case ".bmp": return "image/bmp"; case ".webp": return "image/webp"; case ".csv": return "text/csv"; case ".pdf": return "application/pdf"; case ".docx": case ".doc": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; case ".xlsx": case ".xls": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // Add more cases as needed default: return "application/octet-stream"; } } /// /// Delete an existing attachment. /// [Authorize(Roles = "admin")] [HttpDelete("attachments/{id}")] public async Task DeleteAttachment(int id) { // database soft delete var result = await this.AttachmentProvider.DeleteAttachmentAsync(id); if (result.IsSuccess) { // deleting file from folder UploadService.Movefile(result.Attachment.URI); return Ok(result.Attachment); } return NotFound(); } } }