Beispiel #1
0
        protected async override Task <ProcessResult> OnPolling(PollerJobParameters parameters, string workingFolder)
        {
            string pathToFile = await DownloadBlob(parameters.TenantId, parameters.JobId, parameters.FileName, workingFolder).ConfigureAwait(false);

            String fileName = Path.Combine(Path.GetDirectoryName(pathToFile), parameters.All[JobKeys.FileName]);

            Logger.DebugFormat("Move blob id {0} to real filename {1}", pathToFile, fileName);
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            File.Copy(pathToFile, fileName);
            var converter = new HtmlToPdfConverterFromDiskFile(fileName, base.JobsHostConfiguration)
            {
                Logger = Logger
            };

            var pdfConvertedFileName = converter.Run(parameters.TenantId, parameters.JobId);

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

            return(ProcessResult.Ok);
        }
Beispiel #2
0
        private void TestPowerPointFile(
            List <PollerTestResult> retValue,
            String fileName,
            String type,
            Byte[] fileContent)
        {
            try
            {
                var tempFile = Path.Combine(Path.GetTempPath(), fileName);
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
                File.WriteAllBytes(tempFile, fileContent);

                var conversionError = PowerPointConverter.ConvertToPdf(tempFile, tempFile + ".pdf");
                if (!String.IsNullOrEmpty(conversionError))
                {
                    retValue.Add(new PollerTestResult(false, type + "Conversion with powerpoint converter failed:" + conversionError));
                }
                else
                {
                    retValue.Add(new PollerTestResult(true, type + "Conversion with powerpoint ok."));
                }
            }
            catch (Exception ex)
            {
                retValue.Add(new PollerTestResult(false, type + "Conversion with powerpoint converter failed: " + ex.Message));
            }
        }
        private void TestWordFile(
            List <PollerTestResult> retValue,
            String fileName,
            String type,
            Byte[] fileContent)
        {
            try
            {
                OfficeUtils.KillOfficeProcess("WINWORD");
                var tempFile = Path.Combine(Path.GetTempPath(), fileName);
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
                File.WriteAllBytes(tempFile, fileContent);

                var conversionError = WordConverter.ConvertToPdf(tempFile, tempFile + ".pdf");
                if (!String.IsNullOrEmpty(conversionError))
                {
                    retValue.Add(new PollerTestResult(false, type + "Conversion with word converter failed: " + conversionError));
                }
                else
                {
                    retValue.Add(new PollerTestResult(true, type + "Conversion with word ok."));
                }
            }
            catch (Exception ex)
            {
                retValue.Add(new PollerTestResult(false, type + "Conversion with word converter failed: " + ex.Message));
            }
        }
Beispiel #4
0
        private void TestFile(
            List <PollerTestResult> retValue,
            String fileName,
            String type,
            Byte[] fileContent)
        {
            String converter = Conversion.GetType().Name;

            try
            {
                var tempFile = Path.Combine(Path.GetTempPath(), fileName);
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
                File.WriteAllBytes(tempFile, fileContent);

                string content = Conversion.Run(tempFile, "pdf");
                if (!String.IsNullOrEmpty(content))
                {
                    retValue.Add(new PollerTestResult(true, type + " conversion with converter: " + converter));
                }
                else
                {
                    retValue.Add(new PollerTestResult(false, type + " conversion: wrong content with converter: " + converter));
                }
            }
            catch (Exception ex)
            {
                retValue.Add(new PollerTestResult(false, type + " conversion with converter " + converter + ex.Message));
            }
        }
        private static void TestFile(
            List <PollerTestResult> retValue,
            ITikaAnalyzer analyzer,
            String fileName,
            String type,
            String expected,
            Byte[] fileContent)
        {
            var tempFile = Path.Combine(Path.GetTempPath(), fileName);

            if (File.Exists(tempFile))
            {
                File.Delete(tempFile);
            }
            File.WriteAllBytes(tempFile, fileContent);
            try
            {
                string content = analyzer.GetHtmlContent(tempFile, "");
                if (content.Contains(expected))
                {
                    retValue.Add(new PollerTestResult(true, type + " conversion"));
                }
                else
                {
                    retValue.Add(new PollerTestResult(false, type + " conversion: wrong content"));
                }
            }
            catch (Exception ex)
            {
                retValue.Add(new PollerTestResult(false, type + " conversion: " + ex.Message));
            }
        }
        public void Verify_sanitize_of_single_mhtml_file()
        {
            var tempFile = Path.ChangeExtension(Path.GetTempFileName(), ".mht");

            File.Copy(TestConfig.PathToMht, tempFile);

            string      mhtml  = File.ReadAllText(tempFile);
            MHTMLParser parser = new MHTMLParser(mhtml)
            {
                OutputDirectory = Path.GetDirectoryName(tempFile),
                DecodeImageData = true
            };
            var outFile = Path.ChangeExtension(tempFile, ".html");

            File.WriteAllText(outFile, parser.getHTMLText());

            _sanitizer = new SafeHtmlConverter(outFile)
            {
                Logger = NullLogger.Instance
            };
            var result = _sanitizer.Run("jobtest");

            Assert.That(File.Exists(result), "Output pdf file not created");
            File.Delete(result);
        }
Beispiel #7
0
        /// <summary>
        /// Elaborate
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public String Run(String tenantId, String jobId)
        {
            Logger.DebugFormat("Converting {0} to pdf", jobId);
            var localFileName  = DownloadLocalCopy(tenantId, jobId);
            var outputFileName = localFileName + ".pdf";
            var uri            = new Uri(localFileName);

            var document = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ProduceOutline = ProduceOutline,
                    PaperSize      = PaperKind.A4, // Implicit conversion to PechkinPaperSize
                    Margins        =
                    {
                        All  =          1.375,
                        Unit = Unit.Centimeters
                    },
                    OutputFormat   = GlobalSettings.DocumentOutputFormat.PDF
                },
                Objects =
                {
                    new ObjectSettings
                    {
                        PageUrl     = uri.AbsoluteUri,
                        WebSettings = new WebSettings()
                        {
                            EnableJavascript = false,
                            PrintMediaType   = false
                        }
                    },
                }
            };
            //This is the thread safe converter
            //it seems sometimes to hang forever during tests.
            //https://github.com/tuespetre/TuesPechkin
            //IConverter converter =
            //     new ThreadSafeConverter(
            //         new PdfToolset(
            //             new Win32EmbeddedDeployment(
            //                 new TempFolderDeployment())));

            //Standard, non thread safe converter.
            IConverter converter =
                new StandardConverter(
                    new PdfToolset(
                        new Win32EmbeddedDeployment(
                            new TempFolderDeployment())));

            var pdf = converter.Convert(document);

            File.WriteAllBytes(outputFileName, pdf);

            Logger.DebugFormat("Deleting {0}", localFileName);
            File.Delete(localFileName);
            Logger.DebugFormat("Conversion of {0} to pdf done!", jobId);

            return(outputFileName);
        }
Beispiel #8
0
        public void Delete(BlobId blobId)
        {
            var fileName = _directoryManager.GetFileNameFromBlobId(blobId);

            File.Delete(fileName);

            _blobDescriptorCollection.RemoveById(blobId);
        }
        public void Delete(BlobId blobId)
        {
            var descriptorLocalFileName = _directoryManager.GetDescriptorFileNameFromBlobId(blobId);

            if (File.Exists(descriptorLocalFileName))
            {
                File.Delete(descriptorLocalFileName);
            }
        }
Beispiel #10
0
        public void processing_file_should_succeed(string blobId)
        {
            var s = new Stopwatch();

            s.Start();
            var fileName = _withLibreOfficeConversion.Run(_mapping[blobId], "pdf");

            s.Stop();
            File.Delete(fileName);
            Debug.WriteLine("{0} conversion took {1} ms", blobId, s.ElapsedMilliseconds);
        }
Beispiel #11
0
 public void parallel_conversion_should_not_throw_exceptions()
 {
     Parallel.ForEach(
         _mapping.Keys,
         //                new ParallelOptions() { MaxDegreeOfParallelism = 2 },
         k =>
     {
         var fileName = _unoConversion.Run(_mapping[k], "pdf");
         File.Delete(fileName);
     }
         );
 }
        public void Verify_preview_of_single_html_file()
        {
            var tempFile = Path.ChangeExtension(Path.GetTempFileName(), ".html");

            File.Copy(TestConfig.PathToSimpleHtmlFile, tempFile);
            _converter        = new HtmlToPdfConverterFromDiskFileOld(tempFile, _config);
            _converter.Logger = NullLogger.Instance;
            var result = _converter.Run("jobtest");

            Assert.That(File.Exists(result), "Output pdf file not created");
            File.Delete(result);
        }
Beispiel #13
0
        /// <summary>
        /// Elaborate
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public String Run(String jobId)
        {
            Logger.DebugFormat("Converting {0} to pdf", jobId);
            var localFileName = DownloadLocalCopy(jobId);

            var sanitizer = new SafeHtmlConverter(localFileName)
            {
                Logger = Logger
            };

            localFileName = sanitizer.Run(jobId);

            var outputFileName = localFileName + ".pdf";
            var uri            = new Uri(localFileName);

            var document = new HtmlToPdfDocument
            {
                GlobalSettings =
                {
                    ProduceOutline = ProduceOutline,
                    PaperSize      = PaperKind.A4,                // Implicit conversion to PechkinPaperSize
                    Margins        =
                    {
                        All  =          1.375,
                        Unit = Unit.Centimeters
                    },
                    OutputFormat   = GlobalSettings.DocumentOutputFormat.PDF
                },
                Objects =
                {
                    new ObjectSettings
                    {
                        PageUrl     = uri.AbsoluteUri,
                        WebSettings = new WebSettings()
                        {
                            EnableJavascript = false,
                            PrintMediaType   = false
                        }
                    },
                }
            };

            var converter = Factory.Create();
            var pdf       = converter.Convert(document);

            File.WriteAllBytes(outputFileName, pdf);

            Logger.DebugFormat("Deleting {0}", localFileName);
            File.Delete(localFileName);
            Logger.DebugFormat("Conversion of {0} to pdf done!", jobId);

            return(outputFileName);
        }
        public void Verify_sanitize_of_single_html_file()
        {
            var tempFile = Path.ChangeExtension(Path.GetTempFileName(), ".html");

            File.Copy(TestConfig.PathToSimpleHtmlFile, tempFile);
            _sanitizer = new SafeHtmlConverter(tempFile)
            {
                Logger = NullLogger.Instance
            };
            var result = _sanitizer.Run("jobtest");

            Assert.That(File.Exists(result), "HTML file sanitized");
            File.Delete(result);
        }
        public void Delete(BlobId blobId)
        {
            var descriptor = _mongodDbFileSystemBlobDescriptorStorage.FindOneById(blobId);

            if (descriptor == null)
            {
                throw new ArgumentException($"Descriptor for {blobId} not found in {_mongodDbFileSystemBlobDescriptorStorage.GetType().Name}");
            }

            var fileName = _directoryManager.GetFileNameFromBlobId(blobId, descriptor.FileNameWithExtension);

            File.Delete(fileName);

            _mongodDbFileSystemBlobDescriptorStorage.Delete(blobId);
        }
        private static void TestFile(
            List <PollerTestResult> retValue,
            CreateImageFromPdfTask task,
            String fileName,
            Byte[] fileContent)
        {
            var tempFile = Path.Combine(Path.GetTempPath(), fileName);

            if (File.Exists(tempFile))
            {
                File.Delete(tempFile);
            }
            File.WriteAllBytes(tempFile, fileContent);
            try
            {
                var convertParams = new CreatePdfImageTaskParams()
                {
                    Dpi      = 150,
                    FromPage = 1,
                    Pages    = 1,
                    Format   = CreatePdfImageTaskParams.ImageFormat.Jpg,
                };
                Boolean wasCalled = false;
                var     result    = task.Run(
                    tempFile,
                    convertParams,
                    (i, s) =>
                {
                    wasCalled = true;
                    return(Task.FromResult <Boolean>(true));
                }
                    );
                result.Wait();
                if (wasCalled)
                {
                    retValue.Add(new PollerTestResult(true, "Pdf to Jpg"));
                }
                else
                {
                    retValue.Add(new PollerTestResult(false, "Pdf to Jpg"));
                }
            }
            catch (Exception ex)
            {
                retValue.Add(new PollerTestResult(false, "Pdf to Jpg: " + ex.Message));
            }
        }
        public List <PollerTestResult> Execute()
        {
            List <PollerTestResult> retValue = new List <PollerTestResult>();
            String format        = "png";
            Int32  secondsOffset = 4;

            String vlcExecutable = Helper.GetExecutableLocation();

            if (vlcExecutable == null)
            {
                retValue.Add(new PollerTestResult(false, "Executable location, use app settings vlc_location"));
                return(retValue);
            }
            else
            {
                retValue.Add(new PollerTestResult(true, "Executable location, "));
            }

            try
            {
                var worker = new VlcCommandLineThumbnailCreator(vlcExecutable, format, NullLogger.Instance);

                var tempFile = Path.Combine(Path.GetTempPath(), "video.mp4");
                if (File.Exists(tempFile))
                {
                    File.Delete(tempFile);
                }
                File.WriteAllBytes(tempFile, TestFiles.video);

                var thumb = worker.CreateThumbnail(tempFile, Path.GetTempPath(), 4);
                retValue.Add(new PollerTestResult(
                                 !String.IsNullOrEmpty(tempFile), "video thumb extraction: "));
            }
            catch (Exception ex)
            {
                retValue.Add(new PollerTestResult(false, "video thumb extraction: " + ex.Message));
            }

            return(retValue);
        }
        public async Task <Boolean> Run(
            String pathToFile,
            CreatePdfImageTaskParams createPdfImageTaskParams,
            Func <int, Stream, Task <Boolean> > pageWriter)
        {
            String tempFileName = null;

            if (Passwords.Count > 0)
            {
                tempFileName =
                    Path.Combine(Path.GetDirectoryName(pathToFile),
                                 Path.GetFileNameWithoutExtension(pathToFile) + "_decrypted.pdf");
                if (Decryptor.DecryptFile(pathToFile, tempFileName, Passwords))
                {
                    pathToFile = tempFileName;
                }
            }
            using (var sourceStream = File.OpenRead(pathToFile))
            {
                var settings = new MagickReadSettings
                {
                    Density = new PointD(createPdfImageTaskParams.Dpi, createPdfImageTaskParams.Dpi)
                };
                settings.FrameIndex = 0; // First page
                settings.FrameCount = 1; // Number of pages
                MagickFormat imageFormat = TranslateFormat(createPdfImageTaskParams.Format);

                Logger.DebugFormat("Image format is {0}", imageFormat.ToString());
                using (var images = new MagickImageCollection())
                {
                    bool done = false;
                    if (!_firstDone)
                    {
                        lock (LockForInitializationIssue)
                        {
                            if (!_firstDone)
                            {
                                images.Read(sourceStream, settings);
                                done = true;
                            }
                        }
                    }

                    if (!done)
                    {
                        images.Read(sourceStream, settings);
                    }

                    var lastImage =
                        Math.Min(createPdfImageTaskParams.FromPage - 1 + createPdfImageTaskParams.Pages, images.Count) -
                        1;
                    for (int page = createPdfImageTaskParams.FromPage - 1; page <= lastImage; page++)
                    {
                        var image = images[page];
                        image.Format = imageFormat;

                        using (var ms = new MemoryStream())
                        {
                            image.Write(ms);
                            ms.Seek(0L, SeekOrigin.Begin);
                            await pageWriter(page + 1, ms).ConfigureAwait(false);
                        }
                    }
                }
            }
            if (!String.IsNullOrEmpty(tempFileName) && File.Exists(tempFileName))
            {
                File.Delete(tempFileName);
            }
            return(true);
        }
Beispiel #19
0
        public string Convert(String jobId, string pathToEml, string workingFolder)
        {
            Logger.DebugFormat("Coverting {0} in working folder {1}", pathToEml, workingFolder);

            var reader = new Reader();

            var outFolder = Path.Combine(workingFolder, jobId);

            Logger.DebugFormat("Creating message working folder is {0}", outFolder);

            Directory.CreateDirectory(outFolder);

            Logger.Debug("Extracting files");

            var files = reader.ExtractToFolder(pathToEml, outFolder);

            if (Logger.IsDebugEnabled)
            {
                foreach (var file in files)
                {
                    Logger.DebugFormat("\t{0}", Path.GetFileName(file));
                }
                Logger.DebugFormat("Total files {0}", files.Length);
            }
            var htmlFileName = files.FirstOrDefault(x => x.EndsWith(".htm", StringComparison.OrdinalIgnoreCase)) ??
                               files.FirstOrDefault(x => x.EndsWith(".html", StringComparison.OrdinalIgnoreCase));

            if (htmlFileName == null)
            {
                var textFile = files.FirstOrDefault(x => x.EndsWith(".txt", StringComparison.OrdinalIgnoreCase));
                if (textFile != null)
                {
                    htmlFileName = textFile + ".html";
                    var textcontent = File.ReadAllText(textFile);
                    File.WriteAllText(htmlFileName, String.Format("<html><body><pre>{0}</pre></body></html>", textcontent));
                }
                else
                {
                    htmlFileName = "contentmissing.html";
                    File.WriteAllText(htmlFileName, "<html>No content found in mail.</html>");
                }
            }
            var htmlNameWithoutExtension = Path.GetFileNameWithoutExtension(htmlFileName);

            var htmlContent     = File.ReadAllText(htmlFileName);
            var dirInfoFullName = new DirectoryInfo(outFolder).FullName;

            htmlContent = Regex.Replace(
                htmlContent,
                @"src=""(?<src>.+?)""",
                new MatchEvaluator((m) => NormalizeImgEvaluator(m, dirInfoFullName)),
                RegexOptions.IgnoreCase);
            File.WriteAllText(htmlFileName, htmlContent);

            var pathToZip = Path.Combine(workingFolder, htmlNameWithoutExtension + ".ezip");

            Logger.DebugFormat("New zip file is {0}", pathToZip);

            if (File.Exists(pathToZip))
            {
                Logger.DebugFormat("Deleting previous file: {0}", pathToZip);
                File.Delete(pathToZip);
            }

            Logger.DebugFormat("Creating new file: {0}", pathToZip);
            ZipFile.CreateFromDirectory(outFolder, pathToZip);

            Logger.DebugFormat("Deleting message working folder", outFolder);
            Directory.Delete(outFolder, true);

            Logger.DebugFormat(
                "Convesion done {0} => {1}",
                pathToEml,
                pathToZip
                );
            return(pathToZip);
        }