public DTOFile GetFile([FromBody] DTOAPIFile paramDTOAPIFile) { DTOFileParameter paramDTOFileParameter = new DTOFileParameter(); paramDTOFileParameter.attachmentID = paramDTOAPIFile.attachmentID; paramDTOFileParameter.detailId = paramDTOAPIFile.detailId; paramDTOFileParameter.emailFileName = paramDTOAPIFile.emailFileName; paramDTOFileParameter.portalId = paramDTOAPIFile.portalId; paramDTOFileParameter.taskId = paramDTOAPIFile.taskId; paramDTOFileParameter.ticketPassword = paramDTOAPIFile.ticketPassword; var fileResult = FilesController.ReturnFileMethod(paramDTOFileParameter, _SystemFiles, GetConnectionString()); return(fileResult); }
public static DTOFile ReturnFileMethod(DTOFileParameter paramDTOFileParameter, string _SystemFiles, string ConnectionString) { GeneralSettings objGeneralSettings = new GeneralSettings(ConnectionString); DTOFile objDTOFile = new DTOFile(); // Find record var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(ConnectionString); using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { var objTask = (from task in context.AdefHelpDeskTasks .Include(details => details.AdefHelpDeskTaskDetails) where task.TaskId == paramDTOFileParameter.taskId where task.TicketPassword == paramDTOFileParameter.ticketPassword select task).FirstOrDefault(); if (objTask != null) { var objTaskDetail = (from taskDetail in context.AdefHelpDeskTaskDetails .Include(attachment => attachment.AdefHelpDeskAttachments) where taskDetail.DetailId == paramDTOFileParameter.detailId select taskDetail).FirstOrDefault(); if (objTaskDetail != null) { var objAttachment = (from Attachments in context.AdefHelpDeskAttachments where Attachments.AttachmentId == paramDTOFileParameter.attachmentID select Attachments).FirstOrDefault(); if (objAttachment != null) { // Construct path string FullPath = ""; if (objGeneralSettings.StorageFileType == "AzureStorage") { FullPath = objAttachment.FileName; } else { FullPath = Path.Combine(objAttachment.AttachmentPath, objAttachment.FileName).Replace(@"\", @"/"); } // See if this is a file in an .Eml file if (paramDTOFileParameter.emailFileName != null) { // This is a file contained in an .eml file MimeEntity emailFile = GetEmailFile(FullPath, paramDTOFileParameter.emailFileName, ConnectionString); // Get file contents using (var memory = new MemoryStream()) { if (emailFile is MessagePart) { var rfc822 = (MessagePart)emailFile; rfc822.Message.WriteTo(memory); } else { var part = (MimePart)emailFile; part.Content.DecodeTo(memory); } objDTOFile.Buffer = memory.ToArray(); objDTOFile.FileName = paramDTOFileParameter.emailFileName; return(objDTOFile); } } else { if (objGeneralSettings.StorageFileType == "AzureStorage") { CloudStorageAccount storageAccount = null; CloudBlobContainer cloudBlobContainer = null; // Retrieve the connection string for use with the application. string storageConnectionString = objGeneralSettings.AzureStorageConnection; // Check whether the connection string can be parsed. if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount)) { // Ensure there is a AdefHelpDesk Container CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); cloudBlobContainer = cloudBlobClient.GetContainerReference("adefhelpdesk-files"); CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(objAttachment.FileName); // Download cloudBlockBlob.FetchAttributesAsync().Wait(); long fileByteLength = cloudBlockBlob.Properties.Length; byte[] fileContent = new byte[fileByteLength]; for (int i = 0; i < fileByteLength; i++) { fileContent[i] = 0x20; } cloudBlockBlob.DownloadToByteArrayAsync(fileContent, 0).Wait(); objDTOFile.Buffer = fileContent; objDTOFile.FileName = objAttachment.OriginalFileName; return(objDTOFile); } else { throw new Exception("AzureStorage configured but AzureStorageConnection value cannot connect."); } } else { // Get file contents var fileContents = System.IO.File.ReadAllBytes(FullPath); objDTOFile.Buffer = fileContents; objDTOFile.FileName = objAttachment.OriginalFileName; return(objDTOFile); } } } } } } // Return missing file string strPath = Path.Combine(_SystemFiles, "MissingFile.html").Replace(@"\", @"/"); var missingFileContents = System.IO.File.ReadAllBytes(strPath); objDTOFile.Buffer = missingFileContents; objDTOFile.FileName = "MissingFile.html"; return(objDTOFile); }
public FileContentResult ReturnFile([FromBody] DTOFileParameter paramDTOFileParameter) { var fileResult = ReturnFileMethod(paramDTOFileParameter, _SystemFiles, GetConnectionString()); return(File(fileResult.Buffer, "application/octet-stream", fileResult.FileName)); }