string DownloadLocalCopy(String tenantId, String jobId) { Logger.DebugFormat("Downloaded {0}", _inputFileName); if (IsUnzippedHtmlFile()) { return(_inputFileName); } var workingFolder = Path.GetDirectoryName(_inputFileName); ZipFile.ExtractToDirectory(_inputFileName, workingFolder); Logger.DebugFormat("Extracted zip to {0}", workingFolder); var htmlFile = Path.ChangeExtension(_inputFileName, "html"); if (File.Exists(htmlFile)) { Logger.DebugFormat("Html file is {0}", htmlFile); return(htmlFile); } htmlFile = Path.ChangeExtension(_inputFileName, "htm"); if (File.Exists(htmlFile)) { Logger.DebugFormat("Html file is {0}", htmlFile); return(htmlFile); } var msg = string.Format("Html file not found for {0}!", jobId); Logger.Error(msg); throw new Exception(msg); }
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); }
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); }
public LibreOfficeUnoConversion(JobsHostConfiguration config) { //Needed by UNO SDK5. //http://stackoverflow.com/questions/31856025/bootstrap-uno-api-libreoffice-exception //look at comments of funbit. We need to set UNO_PATH and soffice should be in the PATH //of the system. var sofficePath = config.GetPathToLibreOffice(); var unoPath = Path.GetDirectoryName(sofficePath); Environment.SetEnvironmentVariable("UNO_PATH", unoPath, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + @";" + unoPath, EnvironmentVariableTarget.Process); _config = config; Logger = NullLogger.Instance; }
string DownloadLocalCopy(String jobId) { Logger.DebugFormat("Downloaded {0}", _inputFileName); if (IsUnzippedHtmlFile()) { return(_inputFileName); } var workingFolder = Path.GetDirectoryName(_inputFileName); ZipFile.ExtractToDirectory(_inputFileName, workingFolder); Logger.DebugFormat("Extracted zip to {0}", workingFolder); var originalFileWithoutExtension = Path.GetFileNameWithoutExtension(_inputFileName); var extractedFiles = Directory.GetFiles(workingFolder, "*.htm*"); String htmlFile = null; if (extractedFiles.Length == 1) { htmlFile = extractedFiles[0]; } else if (extractedFiles.Length > 1) { htmlFile = extractedFiles .FirstOrDefault(f => Path.GetFileNameWithoutExtension(f).TrimEnd('.') == originalFileWithoutExtension) ?? extractedFiles[0]; } if (htmlFile != null) { Logger.Debug($"Extracted html from {_inputFileName} is {htmlFile}"); return(htmlFile); } var msg = $"Html file not found for {jobId} {_inputFileName}"; Logger.Error(msg); throw new Exception(msg); }
private void BuildContainer(JobsHostConfiguration config) { _container = new WindsorContainer(); ContainerAccessor.Instance = _container; _container.Register(Component.For <JobsHostConfiguration>().Instance(config)); _container.Kernel.Resolver.AddSubResolver(new CollectionResolver(_container.Kernel, true)); _container.Kernel.Resolver.AddSubResolver(new ArrayResolver(_container.Kernel, true)); _container.AddFacility <LoggingFacility>(config.CreateLoggingFacility); _container.AddFacility <StartableFacility>(); _container.AddFacility <TypedFactoryFacility>(); _container.Register( Component.For <IClientPasswordSet>() .ImplementedBy <EnvironmentVariableClientPasswordSet>(), //Register from this application Classes.FromAssemblyInThisApplication() .BasedOn <IPollerJob>() .WithServiceFirstInterface(), Classes.FromAssemblyInThisApplication() .BasedOn <IPollerTest>() .WithServiceFirstInterface(), //Rgister from dll that contains Jobs in name. Classes.FromAssemblyInDirectory(new AssemblyFilter(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "*.Jobs.*.*")) .BasedOn <IPollerJob>() .WithServiceFirstInterface(), Classes.FromAssemblyInDirectory(new AssemblyFilter(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "*.Jobs.*.*")) .BasedOn <IPollerTest>() .WithServiceFirstInterface() ); _container.Install( FromAssembly.InDirectory( new AssemblyFilter(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "*.Jobs.*.*"))); _logger = _container.Resolve <ILogger>(); }
public string Run(string sourceFile, string outType) { Logger.DebugFormat("DIRECT SOFFICE.EXE CONVERSION: Starting conversion of blobId {0} to {1}", sourceFile, outType); string pathToLibreOffice = _config.GetPathToLibreOffice(); var outputFile = Path.ChangeExtension(sourceFile, outType); string arguments = string.Format("--headless -convert-to {2} -outdir \"{0}\" \"{1}\" ", Path.GetDirectoryName(sourceFile), sourceFile, outType ); var psi = new ProcessStartInfo(pathToLibreOffice, arguments) { UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Minimized }; Logger.DebugFormat("Command: {0} {1}", pathToLibreOffice, arguments); using (var p = Process.Start(psi)) { Logger.Debug("Process started"); p.WaitForExit(); Logger.Debug("Process ended"); } if (!File.Exists(outputFile)) { throw new Exception("Conversion failed"); } return(outputFile); }
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); }