Exemplo n.º 1
0
        /// <summary>
        /// Handle file processing message.
        /// Actor will count one file.
        /// </summary>
        /// <param name="message"></param>
        public void Handle(FileToProcess message)
        {
            IncrementMessagesReceived();

            lineCount      = 0;
            linesProcessed = 0;
            result         = 0;
            m_sw.Start();
            fileName = message.FileName;
            router   = Context.ActorOf(new RoundRobinPool(8).Props(StringCounterActor.GetProps()), String.Format("liner{0}", message.Fileno));
            //var router = Context.ActorOf( StringCounterActor.GetProps(), String.Format( "liner{0}", message.Fileno ) );
            try
            {
                foreach (var line in File.ReadLines(fileName))
                {
                    lineCount++;
                    router.Tell(new ProcessLine(line, lineCount));
                }
            }
            catch (Exception ex)
            {
                Sender.Tell(new FailureMessage(ex, Self));
            }

            // handle when file is empty
            if (lineCount == 0)
            {
                Sender.Tell(new CompletedFile(fileName, result, lineCount, 0));
            }
        }
Exemplo n.º 2
0
        private ImageLoadResponse HandleLoadImage(ImageApi imageApiInstance, FileToProcess fileToProcess, int workerNumber)
        {
            FileStream inputFileStream = null;

            try
            {
                PassportPDFParametersUtilities.GetLoadImageMultipartParameters(fileToProcess.FileAbsolutePath, out inputFileStream, out string fileName);

                using (FileStream tmpFile = File.Create(Path.GetTempFileName(), 4096, FileOptions.DeleteOnClose))
                {
                    using (GZipStream dataStream = new GZipStream(tmpFile, CompressionLevel.Optimal, true))
                    {
                        inputFileStream.CopyTo(dataStream);
                        inputFileStream.Dispose();
                        inputFileStream = null;
                    }

                    tmpFile.Seek(0, SeekOrigin.Begin);

                    return(PassportPDFRequestsUtilities.SendLoadImageMultipartRequest(imageApiInstance, workerNumber, fileToProcess.FileAbsolutePath, fileName, tmpFile, ContentEncoding.Gzip, UploadOperationStartEventHandler));
                }
            }
            catch
            {
                if (inputFileStream != null)
                {
                    inputFileStream.Dispose();
                }
                throw;
            }
        }
Exemplo n.º 3
0
        private void handleFile(FileToProcess file)
        {
            var fileName = file.FileInfo.Name;

            var child = GetChildForFile(fileName);

            child.Tell(file);
        }
Exemplo n.º 4
0
        private bool HandleOutputFileProduction(FileToProcess fileToProcess, string fileId, int workerNumber, OperationsWorkflow.SaveOperationType saveOperationType, object saveOperationConfiguration, PDFApi pdfApi, ImageApi imageApi, FileProductionRules fileProductionRules, WorkflowProcessingResult workflowProcessingResult, bool fileSizeReductionIsIntended, bool inputIsPDF, long inputFileSize, string outputFileAbsolutePath)
        {
            if (!DownloadAndSaveDocument(pdfApi, imageApi, fileId, workerNumber, saveOperationType, saveOperationConfiguration, DownloadOperationStartEventHandler, fileToProcess.FileAbsolutePath, out string downloadedDocumentFileName))
            {
                return(false);
            }

            bool keepProducedFile = MustProducedFileBeKept(workflowProcessingResult, fileSizeReductionIsIntended, inputIsPDF, inputFileSize, FileUtils.GetFileSize(downloadedDocumentFileName));
            bool outputIsInput    = FileUtils.AreSamePath(fileToProcess.FileAbsolutePath, outputFileAbsolutePath);

            if (keepProducedFile)
            {
                if (fileProductionRules.DeleteOriginalFileOnSuccess && !outputIsInput)
                {
                    try
                    {
                        FileUtils.DeleteFile(fileToProcess.FileAbsolutePath);
                    }
                    catch (Exception exception)
                    {
                        OnWarning(LogMessagesUtils.ReplaceMessageSequencesAndReferences(FrameworkGlobals.MessagesLocalizer.GetString("message_original_file_deletion_failure", FrameworkGlobals.ApplicationLanguage), fileName: fileToProcess.FileAbsolutePath, additionalMessage: exception.Message));
                    }
                }

                FileUtils.MoveFile(downloadedDocumentFileName, outputFileAbsolutePath);
                File.SetCreationTime(outputFileAbsolutePath, File.GetCreationTime(fileToProcess.FileAbsolutePath));
            }
            else
            {
                if (!outputIsInput)
                {
                    FileUtils.CopyFile(fileToProcess.FileAbsolutePath, outputFileAbsolutePath);
                }

                if (fileSizeReductionIsIntended)
                {
                    // Inform file size reduction failure
                    workflowProcessingResult.WarningMessages.Add(LogMessagesUtils.GetWarningStatustext(new ReduceWarningInfo()
                    {
                        WarningCode = ReduceWarningCode.FileSizeReductionFailure
                    }, fileToProcess.FileAbsolutePath));
                }

                FileUtils.DeleteFile(downloadedDocumentFileName);
            }


            if (fileProductionRules.KeepWriteAndAccessTime)
            {
                FileUtils.SetOriginalLastAccessTime(fileToProcess.FileAbsolutePath, outputFileAbsolutePath);
            }

            return(true);
        }
Exemplo n.º 5
0
 private bool PickFile(out FileToProcess file)
 {
     lock (_locker)
     {
         if (_filesToProcess.Count > 0)
         {
             file = _filesToProcess[0];
             _filesToProcess.RemoveAt(0);
             return(true);
         }
         else
         {
             file = default;
             return(false);
         }
     }
 }
Exemplo n.º 6
0
        private void ReadFile(FileToProcess fileToProcess)
        {
            var  fileName = fileToProcess.FileInfo.FullName;
            long lineCount;

            if (File.Exists(fileName)) // Sometimes files get deleted before they're processed
            {
                using (var inStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    lineCount = inStream.CountLines();
                }

                _db.Tell(new WatchedFile(fileToProcess.FileInfo, lineCount, fileToProcess.ShouldWriteResultToConsole),
                         Self);
            }

            Self.Tell(PoisonPill.Instance);
        }
Exemplo n.º 7
0
        private void FillPanel()
        {
            pnlMain.Controls.Clear();
            const int lineHeight       = 23;
            int       width            = pnlMain.Width - 30;
            int       labelWidth       = (int)(pnlMain.Width * 0.70);
            int       progressBtnWidth = (int)(pnlMain.Width * 0.14);
            int       loc = 0;

            foreach (string path in _files)
            {
                var panel    = new Panel();
                var label    = new Label();
                var progress = new ProgressBar();
                var button   = new Button();
                panel.SetBounds(0, loc, width, lineHeight);


                progress.Maximum = 100;

                label.Text = $"...{path.Substring(path.LastIndexOf("\\"))}";


                label.SetBounds(0, 0, labelWidth, lineHeight);
                button.SetBounds(panel.Width - progressBtnWidth, 0, progressBtnWidth, lineHeight);
                progress.SetBounds(button.Location.X - progressBtnWidth, 0, progressBtnWidth, lineHeight);

                var f = new FileToProcess(path, progress, button);
                button.Text    = "Preview";
                button.Enabled = false;
                button.Click  += delegate { ShowPreview(path); };

                _filesToProcess.Add(f);

                panel.Controls.Add(label);
                panel.Controls.Add(progress);
                panel.Controls.Add(button);

                pnlMain.Controls.Add(panel);

                loc += panel.Height;
            }
        }
Exemplo n.º 8
0
        private PdfOCRResponse HandleOCRPDF(PDFApi pdfApiInstance, PDFOCRActionConfiguration actionConfiguration, FileToProcess fileToProcess, string fileID, int workerNumber)
        {
            // First get the number of page of the PDF
            PdfGetInfoResponse getInfoResponse = PassportPDFRequestsUtilities.SendGetInfoRequest(pdfApiInstance, new PdfGetInfoParameters(fileID), workerNumber, fileToProcess.FileAbsolutePath, FileOperationStartEventHandler);// todo: use appropriate event handler

            if (getInfoResponse.Error != null)
            {
                return(null);
            }

            PdfOCRParameters ocrParameters = PassportPDFParametersUtilities.GetOCRParameters(actionConfiguration, fileID);

            int pageCount   = getInfoResponse.PageCount;
            int chunkLength = Math.Min(getInfoResponse.PageCount, FrameworkGlobals.PAGE_CHUNK_LENGTH_FOR_OCR_ACTION);
            int chunkCount  = getInfoResponse.PageCount > FrameworkGlobals.PAGE_CHUNK_LENGTH_FOR_OCR_ACTION ? (int)Math.Ceiling((double)getInfoResponse.PageCount / FrameworkGlobals.PAGE_CHUNK_LENGTH_FOR_OCR_ACTION) : 1;

            PdfOCRResponse ocrResponse = null;

            for (int chunkNumber = 1; chunkNumber <= chunkCount; chunkNumber++)
            {
                ocrParameters.PageRange = PassportPDFParametersUtilities.GetChunkProcessingPageRange(pageCount, chunkLength, chunkNumber, chunkCount);

                ocrResponse = PassportPDFRequestsUtilities.SendOCRRequest(pdfApiInstance, ocrParameters, workerNumber, fileToProcess.FileAbsolutePath, ocrParameters.PageRange, pageCount, FileChunkProcessingProgressEventHandler);

                if (_cancellationPending || ocrResponse == null)
                {
                    return(ocrResponse);
                }
            }

            return(ocrResponse);
        }
Exemplo n.º 9
0
        private PdfReduceResponse HandleReducePDF(PDFApi pdfApiInstance, PDFReduceActionConfiguration actionConfiguration, FileToProcess fileToProcess, string fileID, int workerNumber, List <string> warnings)
        {
            PdfReduceParameters reduceParameters = PassportPDFParametersUtilities.GetReduceParameters(actionConfiguration, fileID);
            PdfReduceResponse   reduceResponse   = PassportPDFRequestsUtilities.SendReduceRequest(pdfApiInstance, reduceParameters, workerNumber, fileToProcess.FileAbsolutePath, FileOperationStartEventHandler);

            if (reduceResponse.WarningsInfo != null)
            {
                foreach (ReduceWarningInfo warning in reduceResponse.WarningsInfo)
                {
                    warnings.Add(LogMessagesUtils.GetWarningStatustext(warning, fileToProcess.FileAbsolutePath));
                }
            }

            return(reduceResponse);
        }
Exemplo n.º 10
0
        private WorkflowProcessingResult ProcessWorkflow(PDFApi pdfApiInstance, ImageApi imageApiInstance, OperationsWorkflow workflow, FileToProcess fileToProcess, int workerNumber)
        {
            List <string> warningMessages = new List <string>();
            bool          contentRemoved  = false;
            bool          versionChanged  = false;
            bool          linearized      = false;
            string        fileID          = null;

            foreach (Operation operation in workflow.OperationsToBePerformed)
            {
                Error           actionError     = null;
                ReduceErrorInfo reduceErrorInfo = null;
                long            remainingTokens = 0;

                if (_cancellationPending)
                {
                    return(null);
                }

                switch (operation.Type)
                {
                case Operation.OperationType.LoadPDF:
                    PdfVersion outputVersion = (PdfVersion)operation.Parameters;
                    PdfLoadDocumentResponse loadDocumentResponse = HandleLoadPDF(pdfApiInstance, outputVersion, fileToProcess, workerNumber);
                    if (loadDocumentResponse == null)
                    {
                        OnError(LogMessagesUtils.ReplaceMessageSequencesAndReferences(FrameworkGlobals.MessagesLocalizer.GetString("message_invalid_response_received", FrameworkGlobals.ApplicationLanguage), actionName: "Load"));
                        return(null);
                    }
                    remainingTokens = loadDocumentResponse.RemainingTokens;
                    actionError     = loadDocumentResponse.Error;
                    fileID          = loadDocumentResponse.FileId;
                    break;

                case Operation.OperationType.LoadImage:
                    ImageLoadResponse imageLoadResponse = HandleLoadImage(imageApiInstance, fileToProcess, workerNumber);
                    if (imageLoadResponse == null)
                    {
                        OnError(LogMessagesUtils.ReplaceMessageSequencesAndReferences(FrameworkGlobals.MessagesLocalizer.GetString("message_invalid_response_received", FrameworkGlobals.ApplicationLanguage), actionName: "Load"));
                        return(null);
                    }
                    remainingTokens = imageLoadResponse.RemainingTokens;
                    actionError     = imageLoadResponse.Error;
                    fileID          = imageLoadResponse.FileId;
                    break;

                case Operation.OperationType.ReducePDF:
                    PDFReduceActionConfiguration reduceActionConfiguration = (PDFReduceActionConfiguration)operation.Parameters;
                    PdfReduceResponse            reduceResponse            = HandleReducePDF(pdfApiInstance, reduceActionConfiguration, fileToProcess, fileID, workerNumber, warningMessages);
                    if (reduceResponse == null)
                    {
                        OnError(LogMessagesUtils.ReplaceMessageSequencesAndReferences(FrameworkGlobals.MessagesLocalizer.GetString("message_invalid_response_received", FrameworkGlobals.ApplicationLanguage), actionName: "Reduce"));
                        return(null);
                    }
                    remainingTokens = reduceResponse.RemainingTokens;
                    contentRemoved  = reduceResponse.ContentRemoved;
                    versionChanged  = reduceResponse.VersionChanged;
                    actionError     = reduceResponse.Error;
                    reduceErrorInfo = reduceResponse.ErrorInfo;
                    linearized      = reduceActionConfiguration.FastWebView;
                    break;

                case Operation.OperationType.OCRPDF:
                    PDFOCRActionConfiguration ocrActionConfiguration = (PDFOCRActionConfiguration)operation.Parameters;
                    PdfOCRResponse            ocrResponse            = HandleOCRPDF(pdfApiInstance, ocrActionConfiguration, fileToProcess, fileID, workerNumber);
                    if (ocrResponse == null)
                    {
                        OnError(LogMessagesUtils.ReplaceMessageSequencesAndReferences(FrameworkGlobals.MessagesLocalizer.GetString("message_invalid_response_received", FrameworkGlobals.ApplicationLanguage), actionName: "OCR"));
                        return(null);
                    }
                    remainingTokens = ocrResponse.RemainingTokens;
                    actionError     = ocrResponse.Error;
                    break;
                }

                if (actionError != null)
                {
                    string errorMessage = reduceErrorInfo != null && reduceErrorInfo.ErrorCode != ReduceErrorCode.OK ? ErrorManager.GetMessageFromReduceActionError(reduceErrorInfo, fileToProcess.FileAbsolutePath) : ErrorManager.GetMessageFromPassportPDFError(actionError, operation.Type, fileToProcess.FileAbsolutePath);
                    OnError(errorMessage);
                    return(null);
                }
                else
                {
                    RemainingTokensUpdateEventHandler.Invoke(remainingTokens);
                }
            }


            return(new WorkflowProcessingResult(contentRemoved, versionChanged, linearized, fileID, warningMessages));
        }