303 lines
12 KiB
C#
303 lines
12 KiB
C#
|
||
|
||
using IRaCIS.Core.Domain.Share;
|
||
using IRaCIS.Core.Infrastructure;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
|
||
namespace IRaCIS.Core.Application.Helper;
|
||
|
||
public static class FileStoreHelper
|
||
{
|
||
|
||
|
||
//处理文件名 压缩包,或者目录类的 会带上相对路径
|
||
public static (string TrustedFileNameForFileStorage, string RealName) GetStoreFileName(string fileName)
|
||
{
|
||
|
||
//带目录层级,需要后端处理前端的路径
|
||
if (fileName.Contains("\\"))
|
||
{
|
||
fileName = fileName.Split("\\").Last();
|
||
}
|
||
|
||
if (fileName.Contains("/"))
|
||
{
|
||
fileName = fileName.Split("/").Last();
|
||
}
|
||
|
||
var trustedFileNameForFileStorage = Guid.NewGuid().ToString() + fileName;
|
||
|
||
return (trustedFileNameForFileStorage, fileName);
|
||
}
|
||
|
||
//API vue 部署目录
|
||
public static string GetIRaCISRootPath(IWebHostEnvironment _hostEnvironment)
|
||
{
|
||
var rootPath = (Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\'))).IfNullThrowException().FullName;
|
||
return rootPath;
|
||
|
||
}
|
||
|
||
|
||
//获取环境存储根目录
|
||
public static string GetIRaCISRootDataFolder(IWebHostEnvironment _hostEnvironment)
|
||
{
|
||
var rootPath = GetIRaCISRootPath(_hostEnvironment);
|
||
|
||
var rootFolder = Path.Combine(rootPath, StaticData.IRaCISDataFolder);
|
||
|
||
return rootFolder;
|
||
}
|
||
|
||
//根据相对路径 获取具体文件物理地址
|
||
public static string GetPhysicalFilePath(IWebHostEnvironment _hostEnvironment, string relativePath)
|
||
{
|
||
var rootPath = GetIRaCISRootPath(_hostEnvironment);
|
||
|
||
var physicalFilePath = Path.Combine(rootPath, relativePath.Trim('/'));
|
||
|
||
return physicalFilePath;
|
||
}
|
||
|
||
|
||
|
||
//通过编码获取通用文档具体物理路径
|
||
|
||
public static async Task<(string PhysicalPath, string FileName)> GetCommonDocPhysicalFilePathAsync(IWebHostEnvironment _hostEnvironment, IRepository<CommonDocument> _commonDocumentRepository, string code)
|
||
{
|
||
var doc = await _commonDocumentRepository.FirstOrDefaultAsync(t => t.Code == code);
|
||
|
||
if (doc == null)
|
||
{
|
||
throw new BusinessValidationFailedException("数据库没有找到对应的数据模板文件,请联系系统运维人员。");
|
||
}
|
||
|
||
var filePath = FileStoreHelper.GetPhysicalFilePath(_hostEnvironment, doc.Path);
|
||
|
||
if (!System.IO.File.Exists(filePath))
|
||
{
|
||
throw new BusinessValidationFailedException("数据模板文件存储路径上未找对应文件,请联系系统运维人员。");
|
||
}
|
||
|
||
return (filePath, doc.Name.Trim('/'));
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 写文件导到磁盘
|
||
/// </summary>
|
||
/// <param name="stream">流</param>
|
||
/// <param name="path">文件保存路径</param>
|
||
/// <returns></returns>
|
||
public static async Task<int> WriteFileAsync(System.IO.Stream stream, string path)
|
||
{
|
||
const int FILE_WRITE_SIZE = 84975;//写出缓冲区大小
|
||
int writeCount = 0;
|
||
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write, FILE_WRITE_SIZE, true))
|
||
{
|
||
byte[] byteArr = new byte[FILE_WRITE_SIZE];
|
||
int readCount = 0;
|
||
while ((readCount = await stream.ReadAsync(byteArr, 0, byteArr.Length)) > 0)
|
||
{
|
||
await fileStream.WriteAsync(byteArr, 0, readCount);
|
||
writeCount += readCount;
|
||
}
|
||
}
|
||
return writeCount;
|
||
}
|
||
|
||
// 获取项目签名文档存放路径
|
||
|
||
public static (string PhysicalPath, string RelativePath) GetTrialSignDocPath(IWebHostEnvironment _hostEnvironment, Guid trialId, string fileName)
|
||
{
|
||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||
|
||
//文件类型路径处理
|
||
var uploadFolderPath = Path.Combine(rootPath, StaticData.TrialDataFolder, trialId.ToString(), StaticData.SignDocumentFolder);
|
||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||
|
||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||
|
||
var relativePath = $"/{StaticData.IRaCISDataFolder}/{StaticData.TrialDataFolder}/{trialId}/{StaticData.SignDocumentFolder}/{trustedFileNameForFileStorage}";
|
||
|
||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||
|
||
return (serverFilePath, relativePath);
|
||
}
|
||
|
||
// 获取系统签名文档存放路径
|
||
public static (string PhysicalPath, string RelativePath) GetSystemSignDocPath(IWebHostEnvironment _hostEnvironment, string fileName)
|
||
{
|
||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||
|
||
|
||
//文件类型路径处理
|
||
var uploadFolderPath = Path.Combine(rootPath, StaticData.SystemDataFolder, StaticData.SignDocumentFolder);
|
||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||
|
||
|
||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||
|
||
var relativePath = $"/{StaticData.IRaCISDataFolder}/{StaticData.SystemDataFolder}/{ StaticData.SignDocumentFolder}/{trustedFileNameForFileStorage}";
|
||
|
||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||
|
||
return (serverFilePath, relativePath);
|
||
}
|
||
|
||
// 获取通用文档存放路径(excel模板 )
|
||
|
||
public static (string PhysicalPath, string RelativePath) GetCommonDocPath(IWebHostEnvironment _hostEnvironment, string fileName)
|
||
{
|
||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||
|
||
//文件类型路径处理
|
||
var uploadFolderPath = Path.Combine(rootPath, StaticData.SystemDataFolder, StaticData.DataTemplate);
|
||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||
|
||
|
||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||
|
||
|
||
var relativePath = $"/{StaticData.IRaCISDataFolder}/{StaticData.SystemDataFolder}/{StaticData.DataTemplate}/{trustedFileNameForFileStorage}";
|
||
|
||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||
|
||
return (serverFilePath, relativePath);
|
||
}
|
||
|
||
//获取系统通知文档存放路径
|
||
|
||
public static (string PhysicalPath, string RelativePath) GetSystemNoticePath(IWebHostEnvironment _hostEnvironment, string fileName)
|
||
{
|
||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||
|
||
//文件类型路径处理
|
||
var uploadFolderPath = Path.Combine(rootPath, StaticData.SystemDataFolder, StaticData.NoticeAttachment);
|
||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||
|
||
|
||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||
|
||
|
||
|
||
var relativePath = $"/{StaticData.IRaCISDataFolder}/{StaticData.SystemDataFolder}/{StaticData.NoticeAttachment}/{trustedFileNameForFileStorage}";
|
||
|
||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||
|
||
return (serverFilePath, relativePath);
|
||
}
|
||
|
||
// 获取一致性核查路径
|
||
public static (string PhysicalPath, string RelativePath) GetTrialCheckFilePath(IWebHostEnvironment _hostEnvironment, string fileName,Guid trialId)
|
||
{
|
||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||
|
||
//上传根路径
|
||
string uploadFolderPath = Path.Combine(rootPath, StaticData.TrialDataFolder, trialId.ToString(), "CheckExcel");
|
||
|
||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||
|
||
|
||
//存放核对表
|
||
var (trustedFileNameForFileStorage, realFileName) = FileStoreHelper.GetStoreFileName(fileName);
|
||
|
||
|
||
var relativePath = $"/{StaticData.IRaCISDataFolder}/{StaticData.TrialDataFolder}/{trialId}/CheckExcel/{trustedFileNameForFileStorage}";
|
||
|
||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||
|
||
return (serverFilePath, relativePath);
|
||
}
|
||
|
||
//获取临床数据存放路径
|
||
public static (string PhysicalPath, string RelativePath,string FileRealName) GetClinicalDataPath(IWebHostEnvironment _hostEnvironment, string fileName,Guid trialId,Guid siteId,Guid subjectId,Guid subjectVisitId)
|
||
{
|
||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||
|
||
string uploadFolderPath = Path.Combine(rootPath, StaticData.TrialDataFolder, trialId.ToString(),siteId.ToString(), subjectId.ToString(), subjectVisitId.ToString(), StaticData.TreatmenthistoryFolder);
|
||
|
||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||
|
||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||
|
||
|
||
var relativePath = $"/{StaticData.IRaCISDataFolder}/{StaticData.TrialDataFolder}/{trialId}/{siteId}/{subjectId}/{subjectVisitId}/{StaticData.TreatmenthistoryFolder}/{trustedFileNameForFileStorage}";
|
||
|
||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||
|
||
return (serverFilePath, relativePath, fileRealName);
|
||
|
||
}
|
||
|
||
//获取非dicom文件存放路径
|
||
public static (string PhysicalPath, string RelativePath, string FileRealName) GetNoneDicomFilePath(IWebHostEnvironment _hostEnvironment, string fileName, Guid trialId, Guid siteId, Guid subjectId, Guid subjectVisitId)
|
||
{
|
||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||
|
||
string uploadFolderPath = Path.Combine(rootPath, StaticData.TrialDataFolder, trialId.ToString(), siteId.ToString(), subjectId.ToString(), subjectVisitId.ToString(), StaticData.NoneDicomFolder);
|
||
|
||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||
|
||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||
|
||
|
||
var relativePath = $"/{StaticData.IRaCISDataFolder}/{StaticData.TrialDataFolder}/{trialId}/{siteId}/{subjectId}/{subjectVisitId}/{StaticData.NoneDicomFolder}/{trustedFileNameForFileStorage}";
|
||
|
||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||
|
||
return (serverFilePath, relativePath, fileRealName);
|
||
}
|
||
|
||
|
||
|
||
|
||
// 获取医生通用文件存放路径
|
||
|
||
public static (string PhysicalPath, string RelativePath) GetDoctorOrdinaryFilePath(IWebHostEnvironment _hostEnvironment, string fileName,Guid doctorId,string attachmentType)
|
||
{
|
||
|
||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||
|
||
//文件类型路径处理
|
||
var uploadFolderPath = Path.Combine(rootPath, "UploadFile", doctorId.ToString(), attachmentType);
|
||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||
|
||
|
||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||
|
||
|
||
|
||
var relativePath = $"/{StaticData.IRaCISDataFolder}/UploadFile/{doctorId}/{attachmentType}/{trustedFileNameForFileStorage}";
|
||
|
||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||
|
||
return (serverFilePath, relativePath);
|
||
}
|
||
|
||
public static (string PhysicalPath, string RelativePath) GetNonDoctorFilePath(IWebHostEnvironment _hostEnvironment, string fileName, string attachmentType)
|
||
{
|
||
|
||
var rootPath = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
|
||
|
||
//文件类型路径处理
|
||
var uploadFolderPath = Path.Combine(rootPath, "UploadFile", attachmentType);
|
||
if (!Directory.Exists(uploadFolderPath)) Directory.CreateDirectory(uploadFolderPath);
|
||
|
||
|
||
var (trustedFileNameForFileStorage, fileRealName) = FileStoreHelper.GetStoreFileName(fileName);
|
||
|
||
|
||
|
||
var relativePath = $"/{StaticData.IRaCISDataFolder}/UploadFile/{attachmentType}/{trustedFileNameForFileStorage}";
|
||
|
||
var serverFilePath = Path.Combine(uploadFolderPath, trustedFileNameForFileStorage);
|
||
|
||
return (serverFilePath, relativePath);
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|