Beispiel #1
0
        private bool DownloadAndSaveDocument(PDFApi pdfApi, ImageApi imageApi, string fileId, int workerNumber, OperationsWorkflow.SaveOperationType saveOperationType, object saveOperationConfiguration, ProgressDelegate downloadOperationStartEventHandler, string inputFileAbsolutePath, out string downloadedDocumentFileName)
        {
            downloadedDocumentFileName = Path.GetTempFileName();

            using (FileStream outputFileStream = new FileStream(downloadedDocumentFileName, FileMode.Open))
            {
                try
                {
                    if (saveOperationType == OperationsWorkflow.SaveOperationType.SavePDF)
                    {
                        PassportPDFRequestsUtilities.DownloadPDF(pdfApi, new PdfSaveDocumentParameters(fileId), workerNumber, inputFileAbsolutePath, outputFileStream, downloadOperationStartEventHandler);
                    }
                    else if (saveOperationType == OperationsWorkflow.SaveOperationType.SaveImageAsPDFMRC)
                    {
                        PassportPDFRequestsUtilities.DownloadImageAsPDFMRC(imageApi, PassportPDFParametersUtilities.GetImageSaveAsPDFMRCParameters((ImageSaveAsPDFMRCActionConfiguration)saveOperationConfiguration, fileId),
                                                                           workerNumber, inputFileAbsolutePath, outputFileStream, downloadOperationStartEventHandler);
                    }

                    return(true);
                }
                catch (Exception ex)
                {
                    OnError(LogMessagesUtils.ReplaceMessageSequencesAndReferences(FrameworkGlobals.MessagesLocalizer.GetString("message_output_file_download_failure", FrameworkGlobals.ApplicationLanguage), inputFileAbsolutePath, additionalMessage: ex.Message));
                    return(false);
                }
            }
        }
Beispiel #2
0
        public static void DownloadPDF(PDFApi apiInstance, PdfSaveDocumentParameters saveDocumentParameters, int workerNumber, string inputFilePath, Stream destinationStream, OperationsManager.ProgressDelegate downloadOperationStartEventHandler)
        {
            Exception e       = null;
            int       pausems = 5000;

            for (int i = 0; i < FrameworkGlobals.MAX_RETRYING_REQUESTS; i++)
            {
                downloadOperationStartEventHandler.Invoke(workerNumber, inputFilePath, i);

                try
                {
                    apiInstance.SaveDocumentToFile(saveDocumentParameters, destinationStream);
                    return;
                }
                catch (Exception ex)
                {
                    if (i < FrameworkGlobals.MAX_RETRYING_REQUESTS - 1)
                    {
                        Thread.Sleep(pausems); //marking a pause in case of cnx temporarily out and to avoid overhead.
                        pausems += 2000;
                    }
                    else
                    {//last iteration
                        e = ex;
                    }
                }
            }

            throw e;
        }
Beispiel #3
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);
        }
Beispiel #4
0
        private PdfLoadDocumentResponse HandleLoadPDF(PDFApi pdfApiInstance, PdfVersion outputVersion, FileToProcess fileToProcess, int workerNumber)
        {
            FileStream inputFileStream = null;

            try
            {
                PassportPDFParametersUtilities.GetLoadDocumentMultipartParameters(fileToProcess.FileAbsolutePath, outputVersion, out inputFileStream, out PdfConformance conformance, 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.SendLoadPDFMultipartRequest(pdfApiInstance, workerNumber, fileToProcess.FileAbsolutePath, fileName, conformance, fileToProcess.Password, tmpFile, ContentEncoding.Gzip, UploadOperationStartEventHandler));
                }
            }
            catch
            {
                if (inputFileStream != null)
                {
                    inputFileStream.Dispose();
                }
                throw;
            }
        }
 public void Init()
 {
     Configuration.Default.Username = "******";
     Configuration.Default.Password = "******";
     Configuration.Default.BasePath = "http://api.docspring.local:31337/api/v1";
     instance = new PDFApi();
 }
Beispiel #6
0
        public static PdfGetInfoResponse SendGetInfoRequest(PDFApi apiInstance, PdfGetInfoParameters getInfoParameters, int workerNumber, string inputFilePath, OperationsManager.ProgressDelegate getInfoOperationStartEventHandler)
        {
            Exception e       = null;
            int       pausems = 5000;

            for (int i = 0; i < FrameworkGlobals.MAX_RETRYING_REQUESTS; i++)
            {
                getInfoOperationStartEventHandler.Invoke(workerNumber, inputFilePath, i);
                try
                {
                    return(apiInstance.GetInfo(getInfoParameters));
                }
                catch (Exception ex)
                {
                    if (i < FrameworkGlobals.MAX_RETRYING_REQUESTS - 1)
                    {
                        Thread.Sleep(pausems); //marking a pause in case of cnx temporarily out and to avoid overhead.
                        pausems += 2000;
                    }
                    else
                    {//last iteration
                        e = ex;
                    }
                }
            }

            throw e;
        }
Beispiel #7
0
        public static PdfOCRResponse SendOCRRequest(PDFApi apiInstance, PdfOCRParameters ocrParameters, int workerNumber, string inputFilePath, string pageRange, int pageCount, OperationsManager.ChunkProgressDelegate chunkProgressEventHandler)
        {
            Exception e       = null;
            int       pausems = 5000;

            for (int i = 0; i < FrameworkGlobals.MAX_RETRYING_REQUESTS; i++)
            {
                chunkProgressEventHandler.Invoke(workerNumber, inputFilePath, pageRange, pageCount, i);
                try
                {
                    PdfOCRResponse response = apiInstance.OCR(ocrParameters);

                    return(response);
                }
                catch (Exception ex)
                {
                    if (i < FrameworkGlobals.MAX_RETRYING_REQUESTS - 1)
                    {
                        Thread.Sleep(pausems); //marking a pause in case of cnx temporarily out and to avoid overhead.
                        pausems += 2000;
                    }
                    else
                    {//last iteration
                        e = ex;
                    }
                }
            }

            throw e;
        }
Beispiel #8
0
        public static string[] GetPdfApiSupportedFileExtensions()
        {
            PDFApi apiInstance = new PDFApi(FrameworkGlobals.PassportPdfApiUri);

            Exception e       = null;
            int       pauseMs = 5000;

            for (int i = 0; i < FrameworkGlobals.MAX_RETRYING_REQUESTS; i++)
            {
                try
                {
                    return(apiInstance.GetPDFImportSupportedFileExtensions().Value.ToArray());
                }
                catch (Exception ex)
                {
                    if (i < FrameworkGlobals.MAX_RETRYING_REQUESTS - 1)
                    {
                        Thread.Sleep(pauseMs); //marking a pause in case of cnx temporarily out and to avoid overhead.
                        pauseMs += 2000;
                    }
                    else
                    {//last iteration
                        e = ex;
                    }
                }
            }

            throw e;
        }
Beispiel #9
0
        private void Process(PDFApi pdfApi, ImageApi imageApi, int workerNumber, FileProductionRules fileProductionRules, OperationsWorkflow workflow, string destinationFolder, bool fileSizeReductionIsIntended)
        {
            while (PickFile(out FileToProcess fileToProcess))
            {
                if (_cancellationPending)
                {
                    break;
                }

                try
                {
                    long inputFileSize = FileUtils.GetFileSize(fileToProcess.FileAbsolutePath);
                    bool inputIsPDF    = Path.GetExtension(fileToProcess.FileAbsolutePath).ToUpper() == ".PDF";

                    if (!CheckInputFileSizeValidity(inputFileSize, fileToProcess.FileAbsolutePath))
                    {
                        continue;
                    }

                    WorkflowProcessingResult workFlowProcessingResult = ProcessWorkflow(pdfApi, imageApi, workflow, fileToProcess, workerNumber);

                    if (workFlowProcessingResult != null)
                    {
                        string outputFileAbsolutePath = destinationFolder + fileToProcess.FileRelativePath;

                        bool fileSuccesfullyProcessed = HandleOutputFileProduction(fileToProcess, workFlowProcessingResult.FileID, workerNumber, workflow.SaveOperation, workflow.SaveOperationConfiguration, pdfApi, imageApi, fileProductionRules, workFlowProcessingResult, fileSizeReductionIsIntended, inputIsPDF, inputFileSize, outputFileAbsolutePath);

                        TryCloseDocumentAsync(pdfApi, workFlowProcessingResult.FileID);

                        if (fileSuccesfullyProcessed)
                        {
                            OnFileSuccesfullyProcessed(new FileOperationsResult(fileToProcess.FileAbsolutePath, inputFileSize, FileUtils.GetFileSize(outputFileAbsolutePath), !inputIsPDF), workFlowProcessingResult.WarningMessages);
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                catch (Exception exception)
                {
                    OnError(ErrorManager.GetMessageFromException(exception, fileToProcess.FileAbsolutePath));
                }

                if (_workPaused && !_cancellationPending)
                {
                    // If pause has been requested, wait for resume signal
                    WorkerPauseEventHandler.Invoke(workerNumber);
                    _waitHandle.WaitOne();
                }
            }

            OnWorkerWorkCompletion(workerNumber);
        }
Beispiel #10
0
        private void InitializeApiInstances(out PDFApi pdfApiInstance, out ImageApi imageApiInstance, string apiKey)
        {
            pdfApiInstance = new PDFApi(apiKey)
            {
                BasePath = FrameworkGlobals.PassportPdfApiUri
            };

            imageApiInstance = new ImageApi(apiKey)
            {
                BasePath = FrameworkGlobals.PassportPdfApiUri
            };

            PassportPDF.Client.GlobalConfiguration.Timeout = FrameworkGlobals.PassportPDFConfiguration.SuggestedClientTimeout;
        }
Beispiel #11
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);
        }
Beispiel #12
0
        public static PdfLoadDocumentResponse SendLoadDocumentRequest(PDFApi apiInstance, int workerNumber, string inputFilePath, string fileName, PdfConformance conformance, string password, Stream fileStream, ContentEncoding contentEncoding, OperationsManager.ProgressDelegate uploadOperationStartEventHandler)
        {
            Exception e       = null;
            int       pausems = 5000;

            if (fileStream.Length > int.MaxValue)
            {
                throw new OutOfMemoryException();
            }

            for (int i = 0; i < FrameworkGlobals.MAX_RETRYING_REQUESTS; i++)
            {
                uploadOperationStartEventHandler.Invoke(workerNumber, inputFilePath, i);
                try
                {
                    fileStream.Seek(0, SeekOrigin.Begin);

                    byte[] data = new byte[fileStream.Length];

                    fileStream.Read(data, 0, (int)fileStream.Length);

                    PdfLoadDocumentFromByteArrayParameters pdfLoadDocumentFromByteArrayParameters = new PdfLoadDocumentFromByteArrayParameters(data)
                    {
                        FileName        = fileName,
                        Password        = password,
                        Conformance     = conformance,
                        ContentEncoding = contentEncoding
                    };
                    PdfLoadDocumentResponse response = apiInstance.LoadDocumentAsPDF(pdfLoadDocumentFromByteArrayParameters);

                    return(response);
                }
                catch (Exception ex)
                {
                    if (i < FrameworkGlobals.MAX_RETRYING_REQUESTS - 1)
                    {
                        Thread.Sleep(pausems); //marking a pause in case of cnx temporarily out and to avoid overhead.
                        pausems += 2000;
                    }
                    else
                    {//last iteration
                        e = ex;
                    }
                }
            }

            throw e;
        }
Beispiel #13
0
        private static async void TryCloseDocumentAsync(PDFApi pdfApiInstance, string fileID)
        {
            if (string.IsNullOrWhiteSpace(fileID))
            {
                throw new ArgumentNullException("FileID");
            }

            PdfCloseDocumentParameters closeDocumentParameters = new PdfCloseDocumentParameters(fileID);

            try
            {
                await pdfApiInstance.ClosePDFAsync(closeDocumentParameters); //we do not want to stop the process by waiting such response.
            }
            catch
            {
                return;
            }
        }
Beispiel #14
0
        public static PdfLoadDocumentResponse SendLoadPDFMultipartRequest(PDFApi apiInstance, int workerNumber, string inputFilePath, string fileName, PdfConformance conformance, string password, Stream fileStream, ContentEncoding contentEncoding, OperationsManager.ProgressDelegate uploadOperationStartEventHandler)
        {
            Exception e       = null;
            int       pausems = 5000;

            for (int i = 0; i < FrameworkGlobals.MAX_RETRYING_REQUESTS; i++)
            {
                uploadOperationStartEventHandler.Invoke(workerNumber, inputFilePath, i);
                try
                {
                    fileStream.Seek(0, SeekOrigin.Begin);

                    PdfLoadDocumentResponse response = apiInstance.LoadDocumentAsPDFMultipart(fileStream,
                                                                                              new PdfLoadDocumentParameters()
                    {
                        ContentEncoding = contentEncoding,
                        Conformance     = conformance,
                        Password        = password,
                        FileName        = fileName
                    });

                    return(response);
                }
                catch (Exception ex)
                {
                    if (i < FrameworkGlobals.MAX_RETRYING_REQUESTS - 1)
                    {
                        Thread.Sleep(pausems); //marking a pause in case of cnx temporarily out and to avoid overhead.
                        pausems += 2000;
                    }
                    else
                    {//last iteration
                        e = ex;
                    }
                }
            }

            throw e;
        }
Beispiel #15
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);
        }
Beispiel #16
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));
        }