private static async Task <bool> InnerGetPdf(string workingFolder, DocumentStoreServiceClient client, List <FileToComposeData> files, string handle, DocumentHandle documentHandle, bool pdfExists) { var pdfData = client.OpenRead(documentHandle, DocumentFormats.Pdf); var tempFile = Path.Combine(workingFolder, Guid.NewGuid() + ".pdf"); using (var downloaded = new FileStream(tempFile, FileMode.OpenOrCreate, FileAccess.Write)) { var stream = await pdfData.OpenStream(); stream.CopyTo(downloaded); } files.Add(FileToComposeData.FromDownloadedPdf(tempFile, handle)); pdfExists = true; return(pdfExists); }
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); }