Skip to content

Commit

Permalink
Merge pull request #820 from Project-MONAI/AC-2255
Browse files Browse the repository at this point in the history
fix for metadata extraction
  • Loading branch information
neildsouth authored Jun 1, 2023
2 parents 77fb6b8 + d8164ba commit 4d143b7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
58 changes: 37 additions & 21 deletions src/TaskManager/Plug-ins/Email/EmailPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ public override async Task<ExecutionStatus> ExecuteTask(CancellationToken cancel
var metadata = new Dictionary<string, string>();
if (Event.Inputs.Any())
{
// only process the first input for now
metadata = await GetRawMetaFromFile($"{Event.Inputs.First().RelativeRootPath}/{Event.Inputs.First().Name}", Event.Inputs.First().Bucket);
foreach (var input in Event.Inputs)
{
metadata = await AddRawMetaFromFile(metadata, $"{input.RelativeRootPath}", input.Bucket);
}
}

var emailRequest = GenerateEmailRequestEventMessage(metadata);
Expand All @@ -143,39 +145,53 @@ public override async Task<ExecutionStatus> ExecuteTask(CancellationToken cancel
}
}

private async Task<Dictionary<string, string>?> GetRawMetaFromFile(string path, string bucketName)
private async Task<Dictionary<string, string>> AddRawMetaFromFile(Dictionary<string, string> metadata, string path, string bucketName)
{
if (_includeMetadata is null || _includeMetadata.Count() == 0)
{
_logger.NoMetaDataRequested();
return null;
return metadata;
}
// load file from Minio !
Guard.Against.NullOrWhiteSpace(bucketName);
Guard.Against.NullOrWhiteSpace(path);

var fileStream = await _storageService.GetObjectAsync(bucketName, path);
var dcmFile = DicomFile.Open(fileStream);

var output = new Dictionary<string, string>();
foreach (var item in _includeMetadata)
var allFiles = await _storageService.ListObjectsAsync(bucketName, path, true);
foreach (var file in allFiles)
{
DicomTag tag;
if (file.FilePath.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase)) continue;
Guard.Against.NullOrWhiteSpace(bucketName);
Guard.Against.NullOrWhiteSpace(path);

// load file from Minio !
var fileStream = await _storageService.GetObjectAsync(bucketName, $"{file.FilePath}");
try
{
tag = DicomDictionary.Default[item];
}
catch (Exception)
{
tag = DicomTag.Parse(item);
var dcmFile = DicomFile.Open(fileStream);

foreach (var item in _includeMetadata)
{
if (metadata.ContainsKey(item)) continue;

DicomTag tag;
try
{
tag = DicomDictionary.Default[item];
}
catch (Exception)
{
tag = DicomTag.Parse(item);
}
if (tag is not null)
{
metadata.Add(item, dcmFile.Dataset.GetString(tag));
}
}
}
if (tag is not null)
catch (Exception ex)
{
output.Add(item, dcmFile.Dataset.GetString(tag));
_logger?.ErrorGettingMetaData($"{file.FilePath}/{file.Filename}", ex.Message);
}
}

return output;
return metadata;
}

private JsonMessage<EmailRequestEvent> GenerateEmailRequestEventMessage(Dictionary<string, string>? metadata)
Expand Down
3 changes: 3 additions & 0 deletions src/TaskManager/Plug-ins/Email/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ public static partial class Log

[LoggerMessage(EventId = 6, Level = LogLevel.Debug, Message = "No Metadata requested in event")]
public static partial void NoMetaDataRequested(this ILogger logger);

[LoggerMessage(EventId = 7, Level = LogLevel.Debug, Message = "Error Getting Metadata requested for file: {fileName} message:{message} ")]
public static partial void ErrorGettingMetaData(this ILogger logger, string fileName, string message);
}
}

0 comments on commit 4d143b7

Please sign in to comment.