protected async override Task <ProcessResult> OnPolling(
            Shared.Jobs.PollerJobParameters parameters,
            string workingFolder)
        {
            Logger.DebugFormat("Downloaded file {0} to be converted to pdf", parameters.FileName);
            var converter = Converters.FirstOrDefault(c => c.CanConvert(parameters.FileName));

            if (converter == null)
            {
                Logger.InfoFormat("No converter for extension {0}", Path.GetExtension(parameters.FileName));
                return(ProcessResult.Ok);
            }

            //Download file only if we have one converter that can generate pdf.
            string pathToFile = await DownloadBlob(parameters.TenantId, parameters.JobId, parameters.FileName, workingFolder).ConfigureAwait(false);

            string outFile = Path.Combine(workingFolder, Guid.NewGuid() + ".pdf");

            if (!converter.Convert(pathToFile, outFile))
            {
                Logger.ErrorFormat("Error converting file {0} to pdf", pathToFile);
                return(ProcessResult.Fail($"Error converting file {pathToFile} to pdf"));
            }

            await AddFormatToDocumentFromFile(
                parameters.TenantId,
                parameters.JobId,
                new DocumentFormat(DocumentFormats.Pdf),
                outFile,
                new Dictionary <string, object>()).ConfigureAwait(false);

            return(ProcessResult.Ok);
        }
        private static int GetRequeueCount(Shared.Jobs.PollerJobParameters parameters)
        {
            Int32  requeueCount;
            String requeueCountParameterValue;

            if (parameters.All.TryGetValue(ReQueueCountParameterName, out requeueCountParameterValue))
            {
                requeueCount = Int32.Parse(requeueCountParameterValue);
            }
            else
            {
                requeueCount = 0;
            }

            return(requeueCount);
        }
        protected async override Task <ProcessResult> OnPolling(
            Shared.Jobs.PollerJobParameters parameters,
            string workingFolder)
        {
            var client              = GetDocumentStoreClient(parameters.TenantId);
            var handles             = parameters.All["documentList"].Split('|');
            var destinationHandle   = parameters.All["resultingDocumentHandle"];
            var destinationFileName = parameters.All["resultingDocumentFileName"];

            if (!destinationFileName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, ".pdf");
            }
            List <FileToComposeData> files = new List <FileToComposeData>();

            foreach (var handle in handles)
            {
                var     documentHandle = new DocumentHandle(handle);
                Boolean pdfExists      = false;
                try
                {
                    pdfExists = await InnerGetPdf(workingFolder, client, files, handle, documentHandle, pdfExists);
                }
                catch (System.Net.WebException ex)
                {
                    Logger.WarnFormat("Handle {0} has no PDF format", handle);
                }

                if (!pdfExists)
                {
                    int requeueCount = GetRequeueCount(parameters);
                    if (requeueCount <= 3) //first 3 times, always retry (lets DS the time to generate jobs)
                    {
                        return(GenerateRequeueProcessResult(requeueCount));
                    }

                    //need to check if this file has some job pending that can generate pdf.
                    var pendingJobs = await client.GetJobsAsync(documentHandle);

                    var fileName = await GetfileNameFromHandle(client, documentHandle);

                    Boolean needWaitForJobToRun = CheckIfSomeJobCanStillProducePdfFormat(pendingJobs, fileName, requeueCount);

                    //need to check if queue that can convert the document are still running. We need to wait for the queue to be stable.
                    if (needWaitForJobToRun)
                    {
                        return(GenerateRequeueProcessResult(requeueCount));
                    }
                    else
                    {
                        //This file has no pdf format, mark as missing pdf.
                        Logger.WarnFormat("Handle {0} has no pdf format, status of queue is {1}", handle, String.Join(",", pendingJobs.Select(j => String.Format("{0}[Executed:{1} Success:{2}]", j.QueueName, j.Executed, j.Success))));
                        files.Add(FileToComposeData.NoPdfFormat(handle, fileName));
                    }
                }
            }
            //now compose everything.
            PdfManipulator manipulator = new PdfManipulator(Logger); //Create a manipulator

            foreach (var fileToCompose in files)
            {
                String pdfFileToAppend = fileToCompose.PdfFileName;
                if (!fileToCompose.HasPdfFormat)
                {
                    pdfFileToAppend = GeneratePlaceholderFile(workingFolder, fileToCompose.FileName, fileToCompose.DocumentHandle);
                }

                var error = manipulator.AppendDocumentAtEnd(pdfFileToAppend);
                if (!String.IsNullOrEmpty(error))
                {
                    throw new ApplicationException(String.Format("Unable to compose file {0} error {1}", fileToCompose.DocumentHandle, error));
                }
            }

            manipulator.AddPageNumber();

            String outputDirectory = Path.Combine(workingFolder, Guid.NewGuid().ToString());

            Directory.CreateDirectory(outputDirectory);
            var finalFileName = Path.Combine(outputDirectory, destinationFileName);

            manipulator.Save(finalFileName);

            var result = await client.UploadAsync(finalFileName, new DocumentHandle(destinationHandle));

            return(ProcessResult.Ok);
        }
Exemple #4
0
        protected async override Task <ProcessResult> OnPolling(Shared.Jobs.PollerJobParameters parameters, string workingFolder)
        {
            string localFile = await DownloadBlob(
                parameters.TenantId,
                parameters.JobId,
                parameters.FileName,
                workingFolder);

            String[] permittedExtension = null;
            if (parameters.All.ContainsKey("extensions"))
            {
                var extensionsPermitted = parameters.All["extensions"];
                if (extensionsPermitted != "*")
                {
                    permittedExtension = extensionsPermitted.Split('|');
                }
            }

            var extension          = Path.GetExtension(localFile);
            var unzippingDirectory = new DirectoryInfo(Path.Combine(workingFolder, Guid.NewGuid().ToString())).FullName;

            if (!Directory.Exists(unzippingDirectory))
            {
                Directory.CreateDirectory(unzippingDirectory);
            }
            if (extension == ".zip")
            {
                //we can handle unzipping everything.
                ZipFile.ExtractToDirectory(localFile, unzippingDirectory);
                IEnumerable <String> files = Directory.EnumerateFiles(unzippingDirectory, "*.*", SearchOption.AllDirectories);
                Int32 uploadCount          = await UploadAttachmentListToDocumentStore(parameters, permittedExtension, unzippingDirectory, files);

                Logger.DebugFormat("Uploaded {0} attachments", uploadCount);
            }
            else if (extension == ".eml")
            {
                using (var stream = File.Open(localFile, FileMode.Open, FileAccess.Read))
                {
                    var    message  = MsgReader.Mime.Message.Load(stream);
                    var    bodyPart = message.HtmlBody ?? message.TextBody;
                    String body     = "";
                    if (bodyPart != null)
                    {
                        body = bodyPart.GetBodyAsText();
                    }
                    foreach (MsgReader.Mime.MessagePart attachment in message.Attachments.OfType <MsgReader.Mime.MessagePart>())
                    {
                        if (!String.IsNullOrEmpty(attachment.ContentId) &&
                            body.Contains(attachment.ContentId))
                        {
                            if (Logger.IsDebugEnabled)
                            {
                                Logger.DebugFormat("Attachment cid {0} name {1} discharded because it is inline", attachment.ContentId, attachment.FileName);
                                continue;
                            }
                        }

                        String fileName = Path.Combine(unzippingDirectory, attachment.FileName);
                        File.WriteAllBytes(fileName, attachment.Body);
                        await AddAttachmentToHandle(
                            parameters.TenantId,
                            parameters.JobId,
                            fileName,
                            "attachment_email",
                            attachment.FileName,
                            new Dictionary <string, object>() { }
                            );
                    }
                }
            }
            else if (extension == ".msg")
            {
                using (var stream = File.Open(localFile, FileMode.Open, FileAccess.Read))
                    using (var message = new Storage.Message(stream))
                    {
                        foreach (Storage.Attachment attachment in message.Attachments.OfType <Storage.Attachment>())
                        {
                            if (attachment.IsInline)
                            {
                                continue; //no need to uncompress inline attqach
                            }
                            String fileName = Path.Combine(unzippingDirectory, attachment.FileName);
                            File.WriteAllBytes(fileName, attachment.Data);

                            await AddAttachmentToHandle(
                                parameters.TenantId,
                                parameters.JobId,
                                fileName,
                                "attachment_email",
                                attachment.FileName,
                                new Dictionary <string, object>() { }
                                );
                        }
                    }
            }
            else if (extension == ".7z" || extension == ".7zip" || extension == ".rar")
            {
                //we can handle unzipping everything.
                var   extracted   = _sevenZipExtractorFunctions.ExtractTo(localFile, unzippingDirectory);
                Int32 uploadCount = await UploadAttachmentListToDocumentStore(parameters, permittedExtension, unzippingDirectory, extracted);

                Logger.DebugFormat("Uploaded {0} attachments", uploadCount);
            }


            return(ProcessResult.Ok);
        }