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)); } }
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 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)); } }
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)); } }
/// <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); }
/// <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); }
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); }
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); }