修改上传
This commit is contained in:
@@ -136,56 +136,55 @@ namespace IRaCIS.Core.API.Controllers
|
||||
public virtual async Task DicomFileUploadAsync(Func<string, Stream, int, Task> filePathFunc, string boundary)
|
||||
{
|
||||
|
||||
var fileCount = 0;
|
||||
var fileCount = 0;
|
||||
|
||||
//var boundary = HeaderUtilities.RemoveQuotes(MediaTypeHeaderValue.Parse(Request.ContentType).Boundary).Value;
|
||||
//var boundary = mediaTypeHeader.Boundary.Value;
|
||||
var reader = new MultipartReader(boundary, HttpContext.Request.Body);
|
||||
|
||||
var reader = new MultipartReader(boundary, HttpContext.Request.Body);
|
||||
var section = await reader.ReadNextSectionAsync();
|
||||
|
||||
var section = await reader.ReadNextSectionAsync();
|
||||
|
||||
while (section != null)
|
||||
{
|
||||
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
|
||||
|
||||
if (hasContentDispositionHeader)
|
||||
while (section != null)
|
||||
{
|
||||
var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition);
|
||||
|
||||
var fileName = contentDisposition.FileName.Value ?? String.Empty;
|
||||
|
||||
string mediaType = section.ContentType ?? String.Empty;
|
||||
|
||||
|
||||
//处理压缩文件
|
||||
if (fileName.Contains(".Zip", StringComparison.OrdinalIgnoreCase) || fileName.Contains(".rar", StringComparison.OrdinalIgnoreCase))
|
||||
if (hasContentDispositionHeader)
|
||||
{
|
||||
var archive = ArchiveFactory.Open(section.Body);
|
||||
|
||||
foreach (var entry in archive.Entries)
|
||||
var fileName = contentDisposition.FileName.Value ?? String.Empty;
|
||||
|
||||
string mediaType = section.ContentType ?? String.Empty;
|
||||
|
||||
|
||||
//处理压缩文件
|
||||
if (fileName.Contains(".Zip", StringComparison.OrdinalIgnoreCase) || fileName.Contains(".rar", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (!entry.IsDirectory)
|
||||
var archive = ArchiveFactory.Open(section.Body);
|
||||
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
if (!entry.IsDirectory)
|
||||
{
|
||||
++fileCount;
|
||||
|
||||
await filePathFunc(entry.Key, entry.OpenEntryStream(), fileCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
//普通单个文件
|
||||
else
|
||||
{
|
||||
if (mediaType.Contains("octet-stream") || mediaType.Contains("dicom"))
|
||||
{
|
||||
++fileCount;
|
||||
|
||||
await filePathFunc(entry.Key, entry.OpenEntryStream(), fileCount);
|
||||
await filePathFunc(fileName, section.Body, fileCount);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
//普通单个文件
|
||||
else
|
||||
{
|
||||
if (mediaType.Contains("octet-stream") || mediaType.Contains("dicom"))
|
||||
{
|
||||
++fileCount;
|
||||
|
||||
await filePathFunc(fileName, section.Body, fileCount);
|
||||
}
|
||||
|
||||
}
|
||||
section = await reader.ReadNextSectionAsync();
|
||||
}
|
||||
section = await reader.ReadNextSectionAsync();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -240,9 +239,6 @@ namespace IRaCIS.Core.API.Controllers
|
||||
|
||||
//_logger.LogError("请求到达接口");
|
||||
|
||||
// validation of Content-Type
|
||||
// 1. first, it must be a form-data request
|
||||
// 2. a boundary should be found in the Content-Type
|
||||
if (!HttpContext.Request.HasFormContentType ||
|
||||
!MediaTypeHeaderValue.TryParse(HttpContext.Request.ContentType, out var mediaTypeHeader) ||
|
||||
string.IsNullOrEmpty(mediaTypeHeader.Boundary.Value))
|
||||
@@ -257,13 +253,13 @@ namespace IRaCIS.Core.API.Controllers
|
||||
|
||||
var startTime = DateTime.Now;
|
||||
|
||||
if (_provider.Exists("StudyUid_" + archiveStudyCommand.StudyInstanceUid))
|
||||
if (_provider.Exists($"StudyUid_{trialId}_{archiveStudyCommand.StudyInstanceUid}" ))
|
||||
{
|
||||
return ResponseOutput.NotOk("当前已有人正在上传和归档该检查!");
|
||||
}
|
||||
else
|
||||
{
|
||||
_provider.Set("StudyUid_" + archiveStudyCommand.StudyInstanceUid, _userInfo.Id, TimeSpan.FromMinutes(30));
|
||||
_provider.Set($"StudyUid_{trialId}_{archiveStudyCommand.StudyInstanceUid}", _userInfo.Id, TimeSpan.FromMinutes(30));
|
||||
}
|
||||
|
||||
var (archiveResult, archivedStudyIds) = (new DicomArchiveResult(), new List<Guid>());
|
||||
@@ -279,42 +275,55 @@ namespace IRaCIS.Core.API.Controllers
|
||||
|
||||
var savedInfo = _studyService.GetSaveToDicomInfo(archiveStudyCommand.SubjectVisitId);
|
||||
|
||||
await DicomFileUploadAsync(async (fileName, fileStream, receivedCount) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await fileStream.CopyToAsync(memoryStream);
|
||||
await DicomFileUploadAsync(async (fileName, fileStream, receivedCount) =>
|
||||
{
|
||||
|
||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
try
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
await fileStream.CopyToAsync(memoryStream);
|
||||
|
||||
var (studyId, studyCode) = await _dicomArchiveService.ArchiveDicomStreamAsync(memoryStream, savedInfo, seriesInstanceUidList, sopInstanceUidList);
|
||||
if (!archivedStudyIds.Contains(studyId))
|
||||
{
|
||||
archivedStudyIds.Add(studyId);
|
||||
archiveResult.ArchivedDicomStudies.Add(new DicomStudyBasicDTO() { StudyCode = studyCode, Id = studyId });
|
||||
}
|
||||
}
|
||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
//await _uploadHub.Clients.All.ReceivProgressAsync(archiveStudyCommand.StudyInstanceUid, receivedCount);
|
||||
var (studyId, studyCode) = await _dicomArchiveService.ArchiveDicomStreamAsync(memoryStream, savedInfo, seriesInstanceUidList, sopInstanceUidList);
|
||||
if (!archivedStudyIds.Contains(studyId))
|
||||
{
|
||||
archivedStudyIds.Add(studyId);
|
||||
archiveResult.ArchivedDicomStudies.Add(new DicomStudyBasicDTO() { StudyCode = studyCode, Id = studyId });
|
||||
}
|
||||
}
|
||||
|
||||
//await _uploadHub.Clients.All.ReceivProgressAsync(archiveStudyCommand.StudyInstanceUid, receivedCount);
|
||||
|
||||
|
||||
await _uploadHub.Clients.User(_userInfo.Id.ToString()).ReceivProgressAsync(archiveStudyCommand.StudyInstanceUid, receivedCount);
|
||||
await _uploadHub.Clients.User(_userInfo.Id.ToString()).ReceivProgressAsync(archiveStudyCommand.StudyInstanceUid, receivedCount);
|
||||
|
||||
archiveResult.ReceivedFileCount = receivedCount;
|
||||
archiveResult.ReceivedFileCount = receivedCount;
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e.Message + e.StackTrace);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e.Message + e.StackTrace);
|
||||
|
||||
archiveResult.ErrorFiles.Add(fileName);
|
||||
}
|
||||
archiveResult.ErrorFiles.Add(fileName);
|
||||
}
|
||||
|
||||
|
||||
}, mediaTypeHeader.Boundary.Value);
|
||||
}, mediaTypeHeader.Boundary.Value);
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_provider.Remove($"StudyUid_{trialId}_{archiveStudyCommand.StudyInstanceUid}");
|
||||
|
||||
throw new BusinessValidationFailedException("请求异常,请重试!");
|
||||
}
|
||||
|
||||
|
||||
var studyMonitor = new StudyMonitor()
|
||||
{
|
||||
@@ -336,7 +345,6 @@ namespace IRaCIS.Core.API.Controllers
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
if (archivedStudyIds.Count > 0) // 上传成功,处理逻辑
|
||||
{
|
||||
|
||||
@@ -346,16 +354,13 @@ namespace IRaCIS.Core.API.Controllers
|
||||
|
||||
archiveResult.ReuploadNewStudyId = archivedStudyIds[0] == archiveStudyCommand.AbandonStudyId ? archivedStudyIds[0] : Guid.Empty;
|
||||
|
||||
|
||||
studyMonitor.IsSuccess = true;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
studyMonitor.IsSuccess = false;
|
||||
studyMonitor.Note = JsonConvert.SerializeObject(archiveResult);
|
||||
_provider.Remove("StudyUid_" + archiveStudyCommand.StudyInstanceUid);
|
||||
|
||||
studyMonitor.Note = JsonConvert.SerializeObject(archiveResult);
|
||||
}
|
||||
|
||||
|
||||
@@ -372,7 +377,7 @@ namespace IRaCIS.Core.API.Controllers
|
||||
}
|
||||
finally
|
||||
{
|
||||
_provider.Remove("StudyUid_" + archiveStudyCommand.StudyInstanceUid);
|
||||
_provider.Remove($"StudyUid_{trialId}_{archiveStudyCommand.StudyInstanceUid}");
|
||||
|
||||
studyMonitor.StudyId = archiveResult.ArchivedDicomStudies.FirstOrDefault()?.Id ?? Guid.Empty;
|
||||
studyMonitor.StudyCode = archiveResult.ArchivedDicomStudies.FirstOrDefault()?.StudyCode;
|
||||
|
||||
Reference in New Issue
Block a user