public Stream GetStream(byte[] bytes) { var temp = SharedTempFile.NewFile(); using (var fileStream = File.Open(temp, FileMode.Create, FileAccess.Write, FileShare.None)) { fileStream.Write(bytes, 0, bytes.Length); } return(File.Open(temp, FileMode.Open, FileAccess.Read, FileShare.Read)); }
private StreamWriter GetStreamWriter(object schemaId) { if (_writers.ContainsKey(schemaId)) { return(_writers[schemaId]); } var fileData = new SecureFileData(SharedTempFile.NewFile()); _result.TemporaryFiles.Add(schemaId, fileData); var streamWriter = fileData.GetStreamWriter(); _writers.Add(schemaId, streamWriter); return(streamWriter); }
public void StartGenerate() { fileData = new SecureFileData(SharedTempFile.NewFile()); fileData.OriginalFileName = _fileToGenerate.FileName; var streamWriter = fileData.GetStreamWriter(); try { errorType = string.Empty; _headerLines = (_fileToGenerate.HeaderLines.HasValue && _fileToGenerate.HeaderLines.Value > 0 ? _fileToGenerate.HeaderLines.Value : 0); _footerLines = (_fileToGenerate.FooterLines.HasValue && _fileToGenerate.FooterLines.Value > 0 ? _fileToGenerate.FooterLines.Value : 0); if (_fileToGenerate.IsFixedWidth) { _generator = new FixedWidthTextFileGenerator(streamWriter); int sourceColNum = 1; if (_fileToGenerate.IsMultiSchema) { foreach (FileType fileType in _fileToGenerate.FileTypes) { sourceColNum = 1; foreach (RowDefinition rowDefinition in fileType.RowDefinitions) { if (rowDefinition.ParseStartPosition.HasValue && rowDefinition.ParseStartPosition.Value > 0 && rowDefinition.ParseLength.HasValue && rowDefinition.ParseLength.Value > 0) { rowDefinition.SourceColumnNumber = sourceColNum; sourceColNum++; } else { rowDefinition.SourceColumnNumber = null; } } } } else { foreach (RowDefinition rowDefinition in _fileToGenerate.FileTypes[0].RowDefinitions) { if (rowDefinition.ParseStartPosition.HasValue && rowDefinition.ParseStartPosition.Value > 0 && rowDefinition.ParseLength.HasValue && rowDefinition.ParseLength.Value > 0) { rowDefinition.SourceColumnNumber = sourceColNum; sourceColNum++; } else { rowDefinition.SourceColumnNumber = null; } } } } else { _generator = new DelimitedTextFileGenerator(streamWriter); ((DelimitedTextFileGenerator)_generator).Delimiter = _fileToGenerate.Delimiter; ((DelimitedTextFileGenerator)_generator).TextQualifier = _fileToGenerate.TextQualifier; } string checkRowsErrors = string.Empty; if (_fileToGenerate.IsMultiSchema) { foreach (FileType checkFileType in _fileToGenerate.FileTypes) { foreach (RowDefinition checkRowDef in checkFileType.RowDefinitions) { if (checkRowDef.IsRequired && (!checkRowDef.SourceColumnNumber.HasValue || checkRowDef.SourceColumnNumber.Value <= 0)) { checkRowsErrors += checkRowDef.FieldDisplayName + " is a required field but has no definition in the profile." + Environment.NewLine; } } } } else { foreach (RowDefinition checkRowDef in _fileToGenerate.FileTypes[0].RowDefinitions) { if (checkRowDef.IsRequired && (!checkRowDef.SourceColumnNumber.HasValue || checkRowDef.SourceColumnNumber.Value <= 0)) { checkRowsErrors += checkRowDef.FieldDisplayName + " is a required field but has no definition in the profile." + Environment.NewLine; } } } checkRowsErrors = checkRowsErrors.Trim(); if (!string.IsNullOrWhiteSpace(checkRowsErrors)) { throw new Exception(checkRowsErrors); } if (_fileToGenerate.IsMultiSchema) { RowDefinition schemaDet = _fileToGenerate.SchemaDetector; schemaDet.IsRequired = false; schemaDet.SourceColumnNumber = -1; foreach (FileType checkFileType in _fileToGenerate.FileTypes) { List <RowDefinition> rowDefinitions = checkFileType.RowDefinitions.ToList(); rowDefinitions.Add(schemaDet); rowDefinitions = rowDefinitions.OrderBy(x => x.SourceColumnNumber).ToList(); checkFileType.RowDefinitions = rowDefinitions; foreach (RowDefinition checkRowDef in checkFileType.RowDefinitions) { if (checkRowDef.IsRequired && (!checkRowDef.SourceColumnNumber.HasValue || checkRowDef.SourceColumnNumber.Value <= 0)) { checkRowsErrors += checkRowDef.FieldDisplayName + " is a required field but has no definition in the profile." + Environment.NewLine; throw new Exception(checkRowsErrors); } } } } //skip header lines if defined if (_headerLines > 0) { short skipHeaderLines = 0; while (skipHeaderLines < _headerLines) { _generator.WriteLine(); skipHeaderLines++; _currentLineNumber++; } } } catch (Exception e) { if (!string.IsNullOrEmpty(errorType)) { throw new Exception(errorType + ": " + e.Message); } throw; } }