oi-metrology/Archive/Services/AttachmentsService.cs
2023-10-31 21:40:11 -07:00

181 lines
7.1 KiB
C#

using System;
using System.Globalization;
using System.IO;
namespace OI.Metrology.Archive.Services;
using OI.Metrology.Archive.Models;
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models.Stateless;
using OI.Metrology.Shared.Services;
using System.Data.SqlClient;
public class AttachmentsService : IAttachmentsService
{
private readonly AppSettings _AppSettings;
private readonly IMetrologyRepository _MetrologyRepository;
public AttachmentsService(AppSettings appSettings, IMetrologyRepository metrologyRepository)
{
_AppSettings = appSettings;
_MetrologyRepository = metrologyRepository;
}
protected Stream GetAttachmentStream(string tableName, Guid attachmentId, string filename)
{
if (attachmentId.Equals(Guid.Empty))
throw new Exception("No attachments found");
DateTime insertDate = Convert.ToDateTime(_MetrologyRepository.GetAttachmentInsertDateByGUID(tableName, attachmentId));
int year = insertDate.Year;
DateTime d = insertDate;
CultureInfo cul = CultureInfo.CurrentCulture;
int weekNum = cul.Calendar.GetWeekOfYear(d, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
string workWeek = "WW" + weekNum.ToString("00");
string dateDir = year + @"\" + workWeek;
string fullPath = Path.Combine(_AppSettings.AttachmentPath, tableName + "_", dateDir, attachmentId.ToString(), filename);
// Check to see if file exists in the "New" directory structure, if not change the path back to the old. and check there
if (!File.Exists(fullPath))
{
fullPath = Path.Combine(_AppSettings.AttachmentPath, tableName, attachmentId.ToString(), filename);
}
if (!File.Exists(fullPath))
throw new Exception("File not found");
return new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
protected Stream GetAttachmentStreamArchive(string tableName, Guid attachmentId, string filename)
{
if (attachmentId.Equals(Guid.Empty))
throw new Exception("No attachments found");
string fullPath = Path.Combine(_AppSettings.AttachmentPath, tableName, "2019\\09", attachmentId.ToString(), filename);
if (!File.Exists(fullPath))
throw new Exception("File not found");
return new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
public Stream GetAttachmentStreamByTitle(ToolType toolType, bool header, string title, string filename)
{
if (toolType is null)
throw new Exception("Invalid tool type");
string queryString = "SELECT * FROM " + toolType.DataTableName + " WHERE AttachmentId = @attachmentId";
if (header)
{
queryString = "SELECT * FROM " + toolType.HeaderTableName + " WHERE AttachmentId = '" + title + "'";
}
DateTime SearchDate = new();
string connectionString = @"Server=messqlec1.infineon.com\PROD1,53959;Database=Metrology_Archive;Integrated Security=True";
using (SqlConnection connection = new(connectionString))
{
SqlCommand command = new(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
Console.WriteLine(string.Format("{0}", reader["InsertDate"]));// etc
string test = string.Format("{0}", reader["InsertDate"]);
SearchDate = Convert.ToDateTime(string.Format("{0}", reader["InsertDate"]));
}
}
finally
{
reader.Close();
}
}
string SearchMonth = SearchDate.Month.ToString();
if (SearchDate.Month < 10)
{
_ = "0" + SearchMonth;
}
_ = SearchDate.Year.ToString();
string tableName;
Guid attachmentId;
if (header)
{
tableName = toolType.HeaderTableName;
attachmentId = _MetrologyRepository.GetHeaderAttachmentIDByTitle(toolType.ID, title);
}
else
{
tableName = toolType.DataTableName;
attachmentId = _MetrologyRepository.GetDataAttachmentIDByTitle(toolType.ID, title);
}
return GetAttachmentStream(tableName, attachmentId, filename);
}
public Stream GetAttachmentStreamByAttachmentId(ToolType toolType, bool header, Guid attachmentId, string filename)
{
if (toolType is null)
throw new Exception("Invalid tool type");
string tableName;
if (header)
tableName = toolType.HeaderTableName;
else
tableName = toolType.DataTableName;
return GetAttachmentStream(tableName, attachmentId, filename);
}
public Stream GetAttachmentStreamByAttachmentIdArchive(ToolType toolType, bool header, Guid attachmentId, string filename)
{
if (toolType is null)
throw new Exception("Invalid tool type");
string tableName;
if (header)
tableName = toolType.HeaderTableName;
else
tableName = toolType.DataTableName;
return GetAttachmentStreamArchive(tableName, attachmentId, filename);
}
private void SaveAttachment(ToolType toolType, long headerId, string dataUniqueId, string filename, Microsoft.AspNetCore.Http.IFormFile uploadedFile)
{
if (toolType is null)
throw new Exception("Invalid tool type");
using System.Transactions.TransactionScope trans = _MetrologyRepository.StartTransaction();
Guid attachmentId = Guid.Empty;
string tableName = "";
if (string.IsNullOrWhiteSpace(dataUniqueId))
{
attachmentId = _MetrologyRepository.GetHeaderAttachmentID(toolType.ID, headerId);
tableName = toolType.HeaderTableName;
}
else
{
attachmentId = _MetrologyRepository.GetDataAttachmentID(toolType.ID, headerId, dataUniqueId);
tableName = toolType.DataTableName;
}
if (Equals(attachmentId, Guid.Empty))
throw new Exception("Invalid attachment ID");
string directoryPath = Path.Combine(_AppSettings.AttachmentPath, tableName, attachmentId.ToString());
if (!Directory.Exists(directoryPath))
_ = Directory.CreateDirectory(directoryPath);
string fullPath = Path.Combine(directoryPath, filename);
using (FileStream s = new(fullPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
uploadedFile.CopyTo(s);
}
trans.Complete();
}
public void SaveAttachment(ToolType toolType, long headerId, string dataUniqueId, string filename, object uploadedFile)
{
Microsoft.AspNetCore.Http.IFormFile formFile = (Microsoft.AspNetCore.Http.IFormFile)uploadedFile;
SaveAttachment(toolType, headerId, dataUniqueId, filename, formFile);
}
string IAttachmentsService.GetProcessDataStandardFormat(IMetrologyRepository metrologyRepository, string attachmentPath, int toolTypeId, long headerId) =>
throw new NotImplementedException();
}