public ProcessingResult(ProcessingResultType type, string message, FileInfo[] outputFiles)
 {
     Type        = type;
     Message     = message;
     OutputFiles = outputFiles ?? throw new System.ArgumentNullException(nameof(outputFiles),
                                                                         "outputFiles must not be null");
 }
Beispiel #2
0
        protected internal override ProcessingResult Process(FileInfo file, string[] values, CancellationToken token)
        {
            string directoryPath            = ReplaceUtil.Replace(m_Parameters.DirectoryPath, file);
            ProcessingResultType resultType = ProcessingResultType.Failure;
            string message = "Failed to create directory";

            try
            {
                if (Directory.Exists(directoryPath))
                {
                    resultType = ProcessingResultType.Success;
                    message    = "Directory already exists";
                }
                else if (!File.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                    resultType = ProcessingResultType.Success;
                    message    = "Directory created";
                }
            }
            catch (Exception ex)
            {
                RunInfo.ExceptionInfos.Enqueue(new ExceptionInfo(ex, file));
                message = ex.Message;
            }
            return(new ProcessingResult(resultType, message, new FileInfo[0]));
        }
Beispiel #3
0
        public override ProcessingResult Process(FileInfo originalFile,
                                                 MatchResultType matchResultType, string[] values,
                                                 FileInfo[] generatedFiles, ProcessInput whatToProcess,
                                                 CancellationToken token)
        {
            StringBuilder        message     = new StringBuilder();
            List <FileInfo>      outputFiles = new List <FileInfo>();
            ProcessingResultType resultType  = ProcessingResultType.Success;

            foreach (IProcessor processor in Processors)
            {
                token.ThrowIfCancellationRequested();
                try
                {
                    ProcessInput what = whatToProcess;
                    if (processor.InputFileSource == InputFileSource.OriginalFile)
                    {
                        what = ProcessInput.OriginalFile;
                    }
                    ProcessingResult result = processor?.Process(originalFile, matchResultType, values,
                                                                 generatedFiles ?? new FileInfo[0], what, token);
                    if (result != null)
                    {
                        if (result.OutputFiles != null)
                        {
                            outputFiles.AddRange(result.OutputFiles);
                        }
                        if (result.Type == ProcessingResultType.Failure)
                        {
                            resultType = ProcessingResultType.Failure;
                        }
                        if (result.Message != null)
                        {
                            if (message.Length > 0)
                            {
                                message.Append(" | ");
                            }
                            message.Append(result.Message);
                        }
                    }
                }
                catch (Exception ex) when(!(ex is OperationCanceledException))
                {
                    resultType = ProcessingResultType.Failure;
                    if (message.Length > 0)
                    {
                        message.Append(" | ");
                    }
                    message.Append(ex.Message);
                    RunInfo.ExceptionInfos.Enqueue(new ExceptionInfo(ex, originalFile));
                }
            }
            if (message.Length == 0)
            {
                message.Append(resultType.ToString());
            }
            return(new ProcessingResult(resultType, message.ToString(), outputFiles.ToArray()));
        }
        protected internal override ProcessingResult Process(FileInfo file, string[] values,
                                                             CancellationToken token)
        {
            ProcessingResultType type = ProcessingResultType.Failure;
            string message            = "Success";

            try
            {
                System.Diagnostics.Process.Start(file.FullName);
                type = ProcessingResultType.Success;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                RunInfo.ExceptionInfos.Enqueue(new ExceptionInfo(ex, file));
            }
            return(new ProcessingResult(type, message, new FileInfo[] { file }));
        }
        protected internal override ProcessingResult Process(FileInfo file, string[] values, CancellationToken token)
        {
            ProcessingResultType resultType = ProcessingResultType.Success;
            string message = "Success";
            string newPath = ReplaceUtil.Replace(m_Parameters.FileName, file);

            FileInfo[] resultFiles = new FileInfo[0];
            if (File.Exists(newPath))
            {
                if (m_Parameters.UpdateModificationDateIfExists)
                {
                    try
                    {
                        File.SetLastWriteTime(newPath, DateTime.Now);
                        resultFiles = new FileInfo[] { new FileInfo(newPath) };
                        message     = "Successfully updated file modification date";
                    }
                    catch (Exception ex)
                    {
                        resultType = ProcessingResultType.Failure;
                        message    = $"Failed to update file modification date: {ex.Message}";
                        RunInfo.ExceptionInfos.Enqueue(new ExceptionInfo(ex, file));
                    }
                }
            }
            else
            {
                try
                {
                    File.Create(newPath).Close();
                    resultFiles = new FileInfo[] { new FileInfo(newPath) };
                    message     = "Successfully created file";
                }
                catch (Exception ex)
                {
                    resultType = ProcessingResultType.Failure;
                    message    = $"Failed to create file: {ex.Message}";
                    RunInfo.ExceptionInfos.Enqueue(new ExceptionInfo(ex, file));
                }
            }
            return(new ProcessingResult(resultType, message, resultFiles));
        }
Beispiel #6
0
        public override string ContentAsString()
        {
            string result;

            if (ProcessingResultType == DocumentProcessingResultType.AddFailed ||
                ProcessingResultType == DocumentProcessingResultType.DeleteFailed ||
                ProcessingResultType == DocumentProcessingResultType.UpdateFailed ||
                ProcessingResultType == DocumentProcessingResultType.GetFailed)
            {
                result = $"{Regex.Replace(ProcessingResultType.ToString(), "([A-Z])", " $1").Trim()}.";
            }
            else
            {
                result = $"Document {ProcessingResultType}.";
            }
            if (!string.IsNullOrEmpty(Description))
            {
                result += $" {Description}";
            }
            return(result);
        }
Beispiel #7
0
        public override ProcessingResult Process(FileInfo originalFile,
                                                 MatchResultType matchResultType, string[] values,
                                                 FileInfo[] generatedFiles, ProcessInput whatToProcess,
                                                 CancellationToken token)
        {
            StringBuilder        message     = new StringBuilder();
            List <FileInfo>      resultFiles = new List <FileInfo>();
            ProcessingResultType resultType  = ProcessingResultType.Success;

            if (whatToProcess == ProcessInput.GeneratedFiles)
            {
                if (generatedFiles != null)
                {
                    foreach (FileInfo f in generatedFiles)
                    {
                        token.ThrowIfCancellationRequested();
                        try
                        {
                            ProcessingResult result = Process(f, values, token);
                            if (result != null)
                            {
                                if (result.Type == ProcessingResultType.Failure)
                                {
                                    resultType = ProcessingResultType.Failure;
                                }
                                if (result.OutputFiles != null && result.OutputFiles.Length > 0)
                                {
                                    resultFiles.AddRange(result.OutputFiles);
                                }
                                else
                                {
                                    resultFiles.Add(f);
                                }
                                if (message.Length > 0)
                                {
                                    message.Append(" | ");
                                }
                                message.Append(result.Message);
                            }
                        }
                        catch (Exception ex) when(!(ex is OperationCanceledException))
                        {
                            RunInfo.ExceptionInfos.Enqueue(new ExceptionInfo(ex, f));
                            if (message.Length > 0)
                            {
                                message.Append(" | ");
                            }
                            message.Append(ex.Message);
                            resultType = ProcessingResultType.Failure;
                        }
                    }
                }
            }
            else
            {
                token.ThrowIfCancellationRequested();
                try
                {
                    ProcessingResult tmp = Process(originalFile, values, token);
                    if (tmp.Type == ProcessingResultType.Failure)
                    {
                        resultType = ProcessingResultType.Failure;
                    }
                    if (tmp.OutputFiles != null)
                    {
                        resultFiles.AddRange(tmp.OutputFiles);
                    }
                    message.Append(tmp.Message);
                }
                catch (Exception ex) when(!(ex is OperationCanceledException))
                {
                    resultType = ProcessingResultType.Failure;
                    RunInfo.ExceptionInfos.Enqueue(new ExceptionInfo(ex, originalFile));
                    message.Append(ex.Message);
                }
            }
            if (message.Length == 0)
            {
                message.Append(resultType.ToString());
            }
            return(new ProcessingResult(resultType, message.ToString(), resultFiles.ToArray()));
        }
Beispiel #8
0
 public ProcessingResult(ProcessingResultType type, object data)
 {
     Type = type; Data = data;
 }
Beispiel #9
0
 public ProcessingResult(ProcessingResultType type)
 {
     Type = type;
 }
        protected internal override ProcessingResult Process(FileInfo file, string[] values,
                                                             CancellationToken token)
        {
            int    width;
            int    height;
            bool   isBitmap = false;
            string outPath  = null;

            FileInfo[] generatedFiles = new FileInfo[0];

            ProcessingResultType resultType = ProcessingResultType.Failure;
            string message = "Success";
            Bitmap b       = null;

            try
            {
                b        = new Bitmap(file.FullName);
                isBitmap = true;
            }
            catch (Exception ex)
            {
                resultType = ProcessingResultType.NotApplicable;
            }
            if (isBitmap)
            {
                try
                {
                    if (m_Parameters.ResizeBy == MediaDimension.Width)
                    {
                        width  = m_Parameters.Size;
                        height = (int)Math.Round((double)b.Height * width / b.Width);
                    }
                    else
                    {
                        height = m_Parameters.Size;
                        width  = (int)Math.Round((double)b.Width * height / b.Height);
                    }
                    using (Bitmap bout = new Bitmap(b, new Size(width, height)))
                    {
                        outPath = ReplaceUtil.Replace(m_Parameters.NewPath, file);
                        ImageFormat format = ImageFormat.Jpeg;
                        switch (m_Parameters.Format)
                        {
                        case ImageSaveFormat.Bitmap:
                            format = ImageFormat.Bmp;
                            break;

                        case ImageSaveFormat.Exif:
                            format = ImageFormat.Exif;
                            break;

                        case ImageSaveFormat.Gif:
                            format = ImageFormat.Gif;
                            break;

                        case ImageSaveFormat.Jpeg:
                            format = ImageFormat.Jpeg;
                            break;

                        case ImageSaveFormat.Png:
                            format = ImageFormat.Png;
                            break;

                        case ImageSaveFormat.Tiff:
                            format = ImageFormat.Tiff;
                            break;
                        }
                        if (m_Parameters.Overwrite || !File.Exists(outPath))
                        {
                            string outDirPath = Path.GetDirectoryName(outPath);
                            Directory.CreateDirectory(outDirPath);
                            bout.Save(outPath, format);
                            resultType     = ProcessingResultType.Success;
                            generatedFiles = new FileInfo[] { new FileInfo(outPath) };
                        }
                        else
                        {
                            resultType     = ProcessingResultType.Failure;
                            message        = "Target file exists";
                            generatedFiles = new FileInfo[0];
                        }
                    }
                }
                catch (Exception ex)
                {
                    RunInfo.ExceptionInfos.Enqueue(new ExceptionInfo(ex, file));
                    message    = ex.Message;
                    resultType = ProcessingResultType.Failure;
                }
                finally
                {
                    b?.Dispose();
                }
            }
            return(new ProcessingResult(resultType, message, generatedFiles));
        }