public ParseFileResultWithAttachments ParseFile(byte[] dataBytes) { // Many plugins, like FlowTracker2, accept a ZIP archive that also matches the Zip-with-attachments pattern. // So always try the plugin first with the actual data if (TryParseFile(dataBytes, out var directAttempt) && IsSuccessfulParse(directAttempt.Result)) { return new ParseFileResultWithAttachments { Result = directAttempt.Result } } ; ParseFileResultWithAttachments zipAttempt = null; if (TryParse(dataBytes, out var zipWithAttachments) && TryParseFile(zipWithAttachments.PluginDataBytes, out zipAttempt) && IsSuccessfulParse(zipAttempt.Result)) { return new ParseFileResultWithAttachments { Result = zipAttempt.Result, Attachments = zipWithAttachments.Attachments } } ; if (zipWithAttachments != null) { if (zipAttempt == null) { throw new ArgumentNullException(nameof(zipAttempt)); } // Something failed inside the Zip parser return(new ParseFileResultWithAttachments { Result = zipAttempt.Result, Attachments = zipWithAttachments.Attachments, }); } return(new ParseFileResultWithAttachments { Result = directAttempt.Result, }); }
private bool TryParseFile(byte[] dataBytes, out ParseFileResultWithAttachments result) { result = new ParseFileResultWithAttachments(); try { using (var stream = new MemoryStream(dataBytes)) { result.Result = LocationInfo != null ? Plugin.ParseFile(stream, LocationInfo, Appender, Logger) : Plugin.ParseFile(stream, Appender, Logger); } return(true); } catch (Exception exception) { result.Result = ParseFileResult.CannotParse(exception); return(false); } }