138 lines
5.2 KiB
C#
138 lines
5.2 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
|
|
namespace OI.Metrology.Viewer.Services;
|
|
|
|
using OI.Metrology.Shared.DataModels;
|
|
using OI.Metrology.Shared.Repositories;
|
|
using OI.Metrology.Shared.Services;
|
|
using OI.Metrology.Viewer.Models;
|
|
|
|
public class AttachmentsService : IAttachmentsService
|
|
{
|
|
private readonly AppSettings _AppSettings;
|
|
|
|
private readonly IMetrologyRepo _Repo;
|
|
|
|
public AttachmentsService(AppSettings appSettings, IMetrologyRepo repo)
|
|
{
|
|
_AppSettings = appSettings;
|
|
_Repo = repo;
|
|
}
|
|
|
|
protected Stream GetAttachmentStream(string tableName, Guid attachmentId, string filename)
|
|
{
|
|
if (attachmentId.Equals(Guid.Empty))
|
|
throw new Exception("No attachments found");
|
|
|
|
DateTime insertDate = Convert.ToDateTime(_Repo.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);
|
|
}
|
|
|
|
public Stream GetAttachmentStreamByTitle(ToolType toolType, bool header, string title, string filename)
|
|
{
|
|
if (toolType == null)
|
|
throw new Exception("Invalid tool type");
|
|
Guid attachmentId;
|
|
string tableName;
|
|
if (header)
|
|
{
|
|
tableName = toolType.HeaderTableName;
|
|
attachmentId = _Repo.GetHeaderAttachmentIDByTitle(toolType.ID, title);
|
|
}
|
|
else
|
|
{
|
|
tableName = toolType.DataTableName;
|
|
attachmentId = _Repo.GetDataAttachmentIDByTitle(toolType.ID, title);
|
|
}
|
|
return GetAttachmentStream(tableName, attachmentId, filename);
|
|
}
|
|
|
|
public Stream GetAttachmentStreamByAttachmentId(ToolType toolType, bool header, Guid attachmentId, string filename)
|
|
{
|
|
if (toolType == null)
|
|
throw new Exception("Invalid tool type");
|
|
string tableName;
|
|
if (header)
|
|
tableName = toolType.HeaderTableName;
|
|
else
|
|
tableName = toolType.DataTableName;
|
|
return GetAttachmentStream(tableName, attachmentId, filename);
|
|
}
|
|
|
|
private void SaveAttachment(ToolType toolType, long headerId, string dataUniqueId, string filename, Microsoft.AspNetCore.Http.IFormFile uploadedFile)
|
|
{
|
|
if (toolType == null)
|
|
throw new Exception("Invalid tool type");
|
|
|
|
using System.Transactions.TransactionScope trans = _Repo.StartTransaction();
|
|
Guid attachmentId = Guid.Empty;
|
|
DateTime insertDate = new();
|
|
string tableName = "";
|
|
|
|
if (string.IsNullOrWhiteSpace(dataUniqueId))
|
|
{
|
|
attachmentId = _Repo.GetHeaderAttachmentID(toolType.ID, headerId);
|
|
insertDate = Convert.ToDateTime(_Repo.GetHeaderInsertDate(toolType.ID, headerId));
|
|
tableName = toolType.HeaderTableName;
|
|
|
|
}
|
|
else
|
|
{
|
|
attachmentId = _Repo.GetDataAttachmentID(toolType.ID, headerId, dataUniqueId);
|
|
insertDate = Convert.ToDateTime(_Repo.GetDataInsertDate(toolType.ID, headerId, dataUniqueId));
|
|
// Get Date for new directory name
|
|
tableName = toolType.DataTableName;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
if (Equals(attachmentId, Guid.Empty))
|
|
throw new Exception("Invalid attachment ID");
|
|
string directoryPathSecondary = Path.Combine(_AppSettings.AttachmentPath, tableName + "_", dateDir, attachmentId.ToString());
|
|
if (!Directory.Exists(directoryPathSecondary))
|
|
_ = Directory.CreateDirectory(directoryPathSecondary);
|
|
|
|
string fullPathSecondary = Path.Combine(directoryPathSecondary, filename);
|
|
|
|
using (FileStream s = new(fullPathSecondary, 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);
|
|
}
|
|
|
|
} |