oi-metrology/Server/Services/AttachmentsService.cs
2024-05-23 17:08:44 -07:00

151 lines
7.0 KiB
C#

using System.Globalization;
namespace OI.Metrology.Server.Services;
using OI.Metrology.Server.Models;
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models;
using OI.Metrology.Shared.Models.Stateless;
using OI.Metrology.Shared.Services;
using System.Text.Json;
public class AttachmentsService : IAttachmentsService
{
private readonly AppSettings _AppSettings;
private readonly IHttpClientFactory _HttpClientFactory;
private readonly IFileShareRepository _FileShareRepository;
private readonly IMetrologyRepository _MetrologyRepository;
public AttachmentsService(AppSettings appSettings, IHttpClientFactory httpClientFactory, IFileShareRepository fileShareRepository, IMetrologyRepository metrologyRepository)
{
_AppSettings = appSettings;
_HttpClientFactory = httpClientFactory;
_FileShareRepository = fileShareRepository;
_MetrologyRepository = metrologyRepository;
}
protected Stream GetAttachmentStream(string? tableName, Guid attachmentId, string filename)
{
if (attachmentId.Equals(Guid.Empty))
throw new Exception("No attachments found");
if (tableName is null)
throw new NullReferenceException(nameof(tableName));
DateTime insertDate = Convert.ToDateTime(_MetrologyRepository.GetAttachmentInsertDateByGUID(tableName, attachmentId));
string year = insertDate.Year.ToString();
HttpClient httpClient = _HttpClientFactory.CreateClient();
Uri mesaFileShareMetrologySi = new(_AppSettings.EcMesaFileShareMetrologySi);
int weekNum = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(insertDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
Uri uri = _FileShareRepository.Append(mesaFileShareMetrologySi, "MetrologyAttachments", $"{tableName}_", year, $"WW{weekNum:00}", attachmentId.ToString(), filename);
HttpResponseMessage httpResponseMessage = _FileShareRepository.ReadFile(httpClient, uri);
if (httpResponseMessage.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception("File not found!");
return httpResponseMessage.Content.ReadAsStream();
}
Stream IAttachmentsService.GetAttachmentStreamByTitle(ToolType toolType, bool header, string title, string filename)
{
if (toolType is null)
throw new Exception("Invalid tool type");
Guid attachmentId;
string? tableName;
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);
}
Stream IAttachmentsService.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);
}
void IAttachmentsService.SaveAttachment(ToolType toolType, Attachment attachment)
{
if (toolType is null)
throw new Exception("Invalid tool type");
if (attachment.HeaderId is null)
throw new NullReferenceException($"{nameof(attachment.HeaderId)}");
if (attachment.AttachmentId is null)
throw new NullReferenceException($"{nameof(attachment.AttachmentId)}");
if (attachment.DestinationFileName is null)
throw new NullReferenceException($"{nameof(attachment.DestinationFileName)}");
if (string.IsNullOrWhiteSpace(attachment.UniqueId))
throw new NullReferenceException($"{nameof(attachment.UniqueId)}");
if (attachment.DestinationFileName != "Image.pdf")
_MetrologyRepository.SetHeaderAttachmentID(toolType.ID, attachment.HeaderId.Value, attachment.AttachmentId);
else
_MetrologyRepository.SetDataAttachmentID(toolType.ID, attachment.HeaderId.Value, attachment.UniqueId, attachment.AttachmentId);
}
string? IAttachmentsService.GetProcessDataStandardFormat(IMetrologyRepository metrologyRepository, int toolTypeId, long headerId)
{
string? result;
int weekNum;
string file;
string year;
string directory;
Task<string> json;
Uri weekDirectory;
Uri checkDirectory;
List<string> files = new();
string[] collection = ["-", string.Empty];
NginxFileSystem[]? nginxFileSystemCollection;
Task<HttpResponseMessage> httpResponseMessage;
HttpClient httpClient = _HttpClientFactory.CreateClient();
Uri mesaFileShareMetrologySi = new(_AppSettings.EcMesaFileShareMetrologySi);
DateTime[] dateTimes = new DateTime[] { DateTime.Now, DateTime.Now.AddDays(-6.66) };
ToolType toolType = metrologyRepository.GetToolTypeByID(toolTypeId) ?? throw new Exception("Invalid tool type ID");
foreach (DateTime dateTime in dateTimes)
{
foreach (string item in collection)
{
year = dateTime.Year.ToString();
weekNum = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
weekDirectory = _FileShareRepository.Append(mesaFileShareMetrologySi, "MetrologyAttachments", $"{toolType.HeaderTableName}_", year, $"WW{weekNum:00}");
checkDirectory = _FileShareRepository.Append(weekDirectory, $"{item}{headerId}");
directory = Path.Combine(_AppSettings.EcMetrologySi, "MetrologyAttachments", $"{toolType.HeaderTableName}_", year, $"WW{weekNum:00}", $"{item}{headerId}");
httpResponseMessage = httpClient.GetAsync(checkDirectory);
httpResponseMessage.Wait();
if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK)
continue;
json = httpResponseMessage.Result.Content.ReadAsStringAsync();
json.Wait();
nginxFileSystemCollection = JsonSerializer.Deserialize(json.Result, NginxFileSystemCollectionSourceGenerationContext.Default.NginxFileSystemArray);
if (nginxFileSystemCollection is null)
continue;
foreach (NginxFileSystem nginxFileSystem in nginxFileSystemCollection)
{
if (!nginxFileSystem.Name.EndsWith(".pdsf"))
continue;
file = Path.Combine(directory, nginxFileSystem.Name);
files.Add(file);
}
if (files.Count != 0)
break;
}
if (files.Count != 0)
break;
}
result = files.Count == 0 ? null : files.First();
return result;
}
}