Bug fix: Attachment

This commit is contained in:
Mike Phares 2024-05-23 17:08:44 -07:00
parent 6317c385f6
commit f53ae8c1c8
4 changed files with 42 additions and 44 deletions

View File

@ -13,6 +13,7 @@ public record AppSettings(string ApiFileShare,
string EcCharacterizationSi,
string EcMesaFileShareCharacterizationSi,
string EcMesaFileShareMetrologySi,
string EcMetrologySi,
string GitCommitSeven,
string InboundApiAllowedIPList,
string IqsColumns,

View File

@ -17,6 +17,7 @@ public class AppSettings
public string? EcCharacterizationSi { get; set; }
public string? EcMesaFileShareCharacterizationSi { get; set; }
public string? EcMesaFileShareMetrologySi { get; set; }
public string? EcMetrologySi { get; set; }
public string? GitCommitSeven { get; set; }
public string? InboundApiAllowedIPList { get; set; }
public string? IqsColumns { get; set; }
@ -74,6 +75,7 @@ public class AppSettings
if (appSettings.EcCharacterizationSi is null) throw new NullReferenceException(nameof(EcCharacterizationSi));
if (appSettings.EcMesaFileShareCharacterizationSi is null) throw new NullReferenceException(nameof(EcMesaFileShareCharacterizationSi));
if (appSettings.EcMesaFileShareMetrologySi is null) throw new NullReferenceException(nameof(EcMesaFileShareMetrologySi));
if (appSettings.EcMetrologySi is null) throw new NullReferenceException(nameof(EcMetrologySi));
if (appSettings.GitCommitSeven is null) throw new NullReferenceException(nameof(GitCommitSeven));
if (appSettings.InboundApiAllowedIPList is null) throw new NullReferenceException(nameof(InboundApiAllowedIPList));
if (appSettings.IqsColumns is null) throw new NullReferenceException(nameof(IqsColumns));
@ -104,6 +106,7 @@ public class AppSettings
appSettings.EcCharacterizationSi,
appSettings.EcMesaFileShareCharacterizationSi,
appSettings.EcMesaFileShareMetrologySi,
appSettings.EcMetrologySi,
appSettings.GitCommitSeven,
appSettings.InboundApiAllowedIPList,
appSettings.IqsColumns,

View File

@ -1,6 +1,7 @@
using OI.Metrology.Shared.Models;
using OI.Metrology.Shared.Models.Stateless;
using System.Text.Json;
using System.Web;
namespace OI.Metrology.Server.Repository;
@ -49,7 +50,9 @@ public class FileShareRepository : IFileShareRepository
void IFileShareRepository.CopyFile(HttpClient httpClient, string from, string to)
{
Uri uri = GetEndPoint(httpClient, "copy-file");
Task<HttpResponseMessage> httpResponseMessage = httpClient.GetAsync(uri);
string encodedTo = HttpUtility.UrlEncode(to);
string encodedFrom = HttpUtility.UrlEncode(from);
Task<HttpResponseMessage> httpResponseMessage = httpClient.GetAsync($"{uri.OriginalString}?from={encodedFrom}&to={encodedTo}");
httpResponseMessage.Wait();
if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception(httpResponseMessage.Result.StatusCode.ToString());
@ -58,7 +61,9 @@ public class FileShareRepository : IFileShareRepository
void IFileShareRepository.MoveFile(HttpClient httpClient, string from, string to)
{
Uri uri = GetEndPoint(httpClient, "move-file");
Task<HttpResponseMessage> httpResponseMessage = httpClient.GetAsync(uri);
string encodedTo = HttpUtility.UrlEncode(to);
string encodedFrom = HttpUtility.UrlEncode(from);
Task<HttpResponseMessage> httpResponseMessage = httpClient.GetAsync($"{uri.OriginalString}?from={encodedFrom}&to={encodedTo}");
httpResponseMessage.Wait();
if (httpResponseMessage.Result.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception(httpResponseMessage.Result.StatusCode.ToString());

View File

@ -85,50 +85,28 @@ public class AttachmentsService : IAttachmentsService
throw new NullReferenceException($"{nameof(attachment.HeaderId)}");
if (attachment.AttachmentId is null)
throw new NullReferenceException($"{nameof(attachment.AttachmentId)}");
if (attachment.SourceFileName is null)
throw new NullReferenceException($"{nameof(attachment.SourceFileName)}");
if (attachment.DestinationFileName is null)
throw new NullReferenceException($"{nameof(attachment.DestinationFileName)}");
string? tableName;
DateTime insertDate;
using System.Transactions.TransactionScope trans = _MetrologyRepository.StartTransaction();
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);
insertDate = Convert.ToDateTime(_MetrologyRepository.GetHeaderInsertDate(toolType.ID, attachment.HeaderId.Value));
tableName = toolType.HeaderTableName;
}
else
{
_MetrologyRepository.SetDataAttachmentID(toolType.ID, attachment.HeaderId.Value, attachment.UniqueId, attachment.AttachmentId);
insertDate = Convert.ToDateTime(_MetrologyRepository.GetDataInsertDate(toolType.ID, attachment.HeaderId.Value, attachment.UniqueId));
tableName = toolType.DataTableName;
}
if (Equals(attachment.AttachmentId, Guid.Empty))
{
trans.Dispose();
throw new Exception("Invalid attachment ID!");
}
const string asdf = "\\asdf\asdf";
int weekNum = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(insertDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
string checkPath = Path.Combine(asdf, $"{tableName}_", insertDate.Year.ToString(), $"WW{weekNum:00}", attachment.AttachmentId.ToString(), attachment.DestinationFileName);
if (!attachment.SourceFileName.EndsWith(checkPath[asdf.Length..]))
{
trans.Dispose();
throw new Exception("Invalid attachment path!");
}
trans.Complete();
}
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();
@ -137,21 +115,32 @@ public class AttachmentsService : IAttachmentsService
ToolType toolType = metrologyRepository.GetToolTypeByID(toolTypeId) ?? throw new Exception("Invalid tool type ID");
foreach (DateTime dateTime in dateTimes)
{
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, $"-{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)
files.Add(_FileShareRepository.Append(checkDirectory, nginxFileSystem.Name).AbsoluteUri);
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;
}