using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; using ADODB; using CDO; using ETR.REBT.BarcodeReader; using Stream = System.IO.Stream; namespace ETR.BusinessLogic.EBTBlanks { internal class EBTMailParser { private static readonly string[] SupportedExtensions = { "application/pdf" , "image/jpeg" , "image/bmp" , "image/png" , "image/tiff" }; private static readonly Regex AgencyCodePattern = new Regex(@"{\w*}"); public static EBTMailParseResult ParseEmail(Stream emlStream) { var result = new EBTMailParseResult { Success = true }; Message msg = new MessageClass(); ADODB.Stream stream = new StreamClass(); try { CopyStream(emlStream, stream); msg.DataSource.OpenObject(stream, "_Stream"); result.FromAddress = msg.From; result.SendDate = msg.SentOn; result.ReceivedDate = msg.ReceivedTime; string agentCode; if (TryParseAgencyCode(msg.Subject, out agentCode)) { result.AgencyCode = agentCode; } else { result.Status = " . , . , {12345}"; return result; } if (msg.Attachments.Count == 0) { result.Status = " "; return result; } result.Attachments = ParseAttachments(msg.Attachments).ToList(); return result; } catch (Exception ex) { //If we get unknown error - we don't mark letter as parsed an try next time result.Success = false; result.Status = " ."; result.ExceptionMessage = ex.Message; return result; } finally { stream.Close(); } } private static bool TryParseAgencyCode(string subject, out string result) { var allMatchResults = AgencyCodePattern.Matches(subject); if (allMatchResults.Count != 1 || false == allMatchResults[0].Success) { result = null; return false; } result = allMatchResults[0].Value.Substring(1, allMatchResults[0].Value.Length - 2); return true; } private static void CopyStream(Stream netStream, ADODB.Stream adoStream) { //adoStream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty); adoStream.Type = StreamTypeEnum.adTypeBinary; adoStream.Open(); netStream.Position = 0; var buffer = new byte[1024]; while (netStream.Read(buffer, 0, buffer.Length) != 0) { adoStream.Write(buffer); } adoStream.Flush(); } private static void CopyStream(ADODB.Stream adoStream, Stream netStream) { while (!adoStream.EOS) { var bytes = (byte[])adoStream.Read(1024); netStream.Write(bytes, 0, bytes.Length); } netStream.Flush(); } private static IEnumerable<EBTAttachmentParseResult> ParseAttachments(IBodyParts attachments) { var barcodeReader = new BarcodeReader(true); for (var i = 1; i <= attachments.Count; i++) { var attachment = attachments[i]; var fileResult = new EBTAttachmentParseResult { FileName = attachment.FileName }; if (false == SupportedExtensions.Any(ct => ct == attachment.ContentMediaType)) { fileResult.Status = String.Format(" {0} ", attachment.ContentMediaType); yield return fileResult; } var stream = attachment.GetDecodedContentStream(); try { var memoryStream = new MemoryStream(); CopyStream(stream, memoryStream); memoryStream.Position = 0; var parseResult = barcodeReader.Decode(memoryStream, attachment.FileName); fileResult.Status = parseResult.AllPages > parseResult.RecognizedPages ? String.Format(" {0} {1} ", parseResult.RecognizedPages, parseResult.AllPages) : fileResult.Status; fileResult.Stream = memoryStream; if (parseResult.ResultList != null && parseResult.ResultList.Count > 0) { fileResult.BarcodeNumbers = parseResult.ResultList.Select(b => ParseBarcode(b.Text)).ToList(); } } catch (Exception ex) { fileResult.Status = " ."; fileResult.ExceptionMessage = ex.Message; } finally { stream.Close(); } yield return fileResult; } } private static EBTBarcodeParseResult ParseBarcode(string barCode) { if (barCode.Length != 15) return new EBTBarcodeParseResult { BarcodeNumber = barCode, Status = " " }; return new EBTBarcodeParseResult { BarcodeNumber = barCode, CouponNumber = barCode.Substring(1, 13).Insert(3, " ") }; } } }
Source: https://habr.com/ru/post/211669/
All Articles