ImageFilePreviewer
using kCura.WinEDDS.Api;
using kCura.WinEDDS.Helpers;
using Microsoft.VisualBasic.CompilerServices;
using Relativity.DataExchange.Data;
using Relativity.DataExchange.Io;
using Relativity.DataExchange.Media;
using Relativity.DataExchange.Process;
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
namespace kCura.WinEDDS
{
public class ImageFilePreviewer : DelimitedFileImporter2
{
private enum Columns
{
DocumentArtifactID = 0,
FileLocation = 2,
MultiPageIndicator = 3
}
public delegate void StatusMessageEventHandler (StatusEventArgs args);
public delegate void FatalErrorEventEventHandler (string message, Exception ex);
private int _fileLineCount;
private bool _continue;
[CompilerGenerated]
[AccessedThroughProperty("_processContext")]
private ProcessContext __processContext;
private readonly IFilePathHelper _filePathHelper;
[CompilerGenerated]
private IImageValidator __imageValidator;
[CompilerGenerated]
private ITiffValidator __tiffValidator;
[CompilerGenerated]
private IFileInspector __fileInspector;
[CompilerGenerated]
private StatusMessageEventHandler StatusMessageEvent;
[CompilerGenerated]
private FatalErrorEventEventHandler FatalErrorEventEvent;
private virtual ProcessContext _processContext {
[CompilerGenerated]
get {
return __processContext;
}
[MethodImpl(MethodImplOptions.Synchronized)]
[CompilerGenerated]
set {
EventHandler<CancellationRequestEventArgs> value2 = _processObserver_CancelImport;
ProcessContext _processContext = __processContext;
if (_processContext != null)
_processContext.CancellationRequest -= value2;
__processContext = value;
_processContext = __processContext;
if (_processContext != null)
_processContext.CancellationRequest += value2;
}
}
private IImageValidator _imageValidator { get; set; }
private ITiffValidator _tiffValidator { get; set; }
private IFileInspector _fileInspector { get; set; }
private bool Continue {
get {
if (!base.HasReachedEof)
return _continue;
return false;
}
}
public event StatusMessageEventHandler StatusMessage {
[CompilerGenerated]
add {
StatusMessageEventHandler statusMessageEventHandler = StatusMessageEvent;
StatusMessageEventHandler statusMessageEventHandler2;
do {
statusMessageEventHandler2 = statusMessageEventHandler;
StatusMessageEventHandler value2 = (StatusMessageEventHandler)Delegate.Combine(statusMessageEventHandler2, value);
statusMessageEventHandler = Interlocked.CompareExchange(ref StatusMessageEvent, value2, statusMessageEventHandler2);
} while ((object)statusMessageEventHandler != statusMessageEventHandler2);
}
[CompilerGenerated]
remove {
StatusMessageEventHandler statusMessageEventHandler = StatusMessageEvent;
StatusMessageEventHandler statusMessageEventHandler2;
do {
statusMessageEventHandler2 = statusMessageEventHandler;
StatusMessageEventHandler value2 = (StatusMessageEventHandler)Delegate.Remove(statusMessageEventHandler2, value);
statusMessageEventHandler = Interlocked.CompareExchange(ref StatusMessageEvent, value2, statusMessageEventHandler2);
} while ((object)statusMessageEventHandler != statusMessageEventHandler2);
}
}
public event FatalErrorEventEventHandler FatalErrorEvent {
[CompilerGenerated]
add {
FatalErrorEventEventHandler fatalErrorEventEventHandler = FatalErrorEventEvent;
FatalErrorEventEventHandler fatalErrorEventEventHandler2;
do {
fatalErrorEventEventHandler2 = fatalErrorEventEventHandler;
FatalErrorEventEventHandler value2 = (FatalErrorEventEventHandler)Delegate.Combine(fatalErrorEventEventHandler2, value);
fatalErrorEventEventHandler = Interlocked.CompareExchange(ref FatalErrorEventEvent, value2, fatalErrorEventEventHandler2);
} while ((object)fatalErrorEventEventHandler != fatalErrorEventEventHandler2);
}
[CompilerGenerated]
remove {
FatalErrorEventEventHandler fatalErrorEventEventHandler = FatalErrorEventEvent;
FatalErrorEventEventHandler fatalErrorEventEventHandler2;
do {
fatalErrorEventEventHandler2 = fatalErrorEventEventHandler;
FatalErrorEventEventHandler value2 = (FatalErrorEventEventHandler)Delegate.Remove(fatalErrorEventEventHandler2, value);
fatalErrorEventEventHandler = Interlocked.CompareExchange(ref FatalErrorEventEvent, value2, fatalErrorEventEventHandler2);
} while ((object)fatalErrorEventEventHandler != fatalErrorEventEventHandler2);
}
}
public ImageFilePreviewer(ProcessContext context, bool doRetryLogic)
: base(',', doRetryLogic)
{
_filePathHelper = new ConfigurableFilePathHelper();
_imageValidator = new ImageValidator();
_tiffValidator = new TiffValidator();
_fileInspector = new FileInspector();
_processContext = context;
_continue = true;
}
public override object ReadFile(string path)
{
try {
_fileLineCount = FileSystem.Instance.File.CountLinesInFile(path);
base.Reader = new StreamReader(path);
RaiseStatusEvent(EventType2.Progress, "Begin Image Upload");
while (Continue) {
try {
DoFileUpload();
} catch (Exception ex) {
ProjectData.SetProjectError(ex);
Exception ex2 = ex;
string text = ex2.ToString().ToLower();
text = ((text.IndexOf("ix_") == -1 || text.IndexOf("duplicate") == -1) ? ex2.Message : "Error creating document - identifier field isn't being properly filled. Please choose a different 'key' field.");
RaiseStatusEvent(EventType2.Error, text);
ProjectData.ClearProjectError();
}
}
base.Reader.Close();
RaiseStatusEvent(EventType2.Progress, "End Image Upload");
} catch (Exception ex3) {
ProjectData.SetProjectError(ex3);
Exception ex4 = ex3;
RaiseStatusEvent(EventType2.Error, ex4.Message);
RaiseFatalError(ex4);
ProjectData.ClearProjectError();
}
return null;
}
private void DoFileUpload()
{
string[] array = GetLine();
while (array != null && _continue) {
ImageRecord imageRecord = CreateImageRecord(array);
string filePathAndValidate = GetFilePathAndValidate(imageRecord);
if (Operators.CompareString(imageRecord.BatesNumber, "", false) == 0)
RaiseStatusEvent(EventType2.Progress, $"""{base.CurrentLineNumber}""");
else if (Operators.CompareString(filePathAndValidate, "", false) == 0) {
RaiseStatusEvent(EventType2.Progress, $"""{imageRecord.BatesNumber}""");
} else {
RaiseStatusEvent(EventType2.Progress, $"""{imageRecord.BatesNumber}""");
}
array = ((!base.HasReachedEof) ? GetLine() : null);
}
}
private ImageRecord CreateImageRecord(string[] valuearray)
{
return new ImageRecord {
OriginalIndex = base.CurrentLineNumber,
IsNewDoc = (Operators.CompareString(GetValue(valuearray, 3).ToLower(), "Y", false) == 0),
FileLocation = GetValue(valuearray, 2),
BatesNumber = GetValue(valuearray, 0)
};
}
private string GetFilePathAndValidate(ImageRecord record)
{
string fileLocation = BulkImageFileImporter.GetFileLocation(record);
if (Operators.CompareString(fileLocation, string.Empty, false) != 0) {
string existingFilePath = _filePathHelper.GetExistingFilePath(fileLocation);
if (!string.IsNullOrEmpty(existingFilePath)) {
if (!string.Equals(fileLocation, existingFilePath))
RaiseStatusEvent(EventType2.Warning, $"""{fileLocation}""{existingFilePath}""");
if (ValidateImage(existingFilePath))
return existingFilePath;
return string.Empty;
}
RaiseStatusEvent(EventType2.Error, $"""{fileLocation}""");
return string.Empty;
}
return fileLocation;
}
private string GetValue(string[] valuearray, int index)
{
try {
return valuearray[index];
} catch (IndexOutOfRangeException ex) {
ProjectData.SetProjectError(ex);
IndexOutOfRangeException ex2 = ex;
RaiseStatusEvent(EventType2.Error, $"""{base.CurrentLineNumber}""{index}""");
string result = "";
ProjectData.ClearProjectError();
return result;
}
}
private bool ValidateImage(string path)
{
try {
ImageValidationResult imageValidationResult = _imageValidator.IsImageValid(path, _tiffValidator, _fileInspector);
if (!imageValidationResult.IsValid)
throw new ImageFileValidationException(imageValidationResult.Message);
return true;
} catch (Exception ex) {
ProjectData.SetProjectError(ex);
Exception ex2 = ex;
RaiseStatusEvent(EventType2.Error, ex2.Message);
bool result = false;
ProjectData.ClearProjectError();
return result;
}
}
private void RaiseFatalError(Exception ex)
{
FatalErrorEventEvent?.Invoke("Error processing line: " + base.CurrentLineNumber.ToString(), ex);
}
private void RaiseStatusEvent(EventType2 et, string line)
{
StatusMessageEvent?.Invoke(new StatusEventArgs(et, base.CurrentLineNumber, _fileLineCount, line, null, null));
}
private void _processObserver_CancelImport(object sender, CancellationRequestEventArgs e)
{
_continue = false;
}
}
}