示例#1
0
        public SourceAccess([NotNull] string fileName, bool isReading = true, [CanBeNull] string id = null,
                            [CanBeNull] string recipient = null, bool keepEnencyrpted = false)
        {
            FullPath        = fileName;
            Reading         = isReading;
            Identifier      = id ?? FileSystemUtils.GetShortDisplayFileName(fileName, 40);
            Recipient       = recipient ?? string.Empty;
            KeepEnencyrpted = keepEnencyrpted;
            LeaveOpen       = false;
            FileType        = FromExtension(fileName);
            switch (FileType)
            {
            case FileTypeEnum.Zip when !isReading:
                IdentifierInContainer = FileSystemUtils.GetFileName(fileName).ReplaceCaseInsensitive(".zip", "");
                break;

            // for PGP we need a password/ passphrase for Zip we might need one later
            case FileTypeEnum.Pgp when isReading:
                EncryptedPassphrase = FunctionalDI.GetEncryptedPassphraseForFile(fileName);
                break;
            }
            if (!isReading && KeepEnencyrpted)
            {
                // remove entension
                var split = FileSystemUtils.SplitPath(fileName);
                var fn    = Path.Combine(split.DirectoryName, split.FileNameWithoutExtension);
                m_OpenStream = GetOpenStreamFunc(fn, false);
            }
            else
            {
                m_OpenStream = GetOpenStreamFunc(fileName, isReading);
            }
        }
示例#2
0
        public static string GetIdFromFileName([NotNull] this string path)
        {
            var fileName = FileSystemUtils.GetFileName(path).ProcessByCategory(x =>
                                                                               x == UnicodeCategory.UppercaseLetter || x == UnicodeCategory.LowercaseLetter ||
                                                                               x == UnicodeCategory.OtherLetter ||
                                                                               x == UnicodeCategory.ConnectorPunctuation || x == UnicodeCategory.DashPunctuation ||
                                                                               x == UnicodeCategory.OtherPunctuation ||
                                                                               x == UnicodeCategory.DecimalDigitNumber);

            const string c_TimeSep = @"(:|-|_)?";
            const string c_DateSep = @"(\/|\.|-|_)?";

            const string c_Hour   = @"(2[0-3]|((0|1)\d))"; // 00-09 10-19 20-23
            const string c_MinSec = @"([0-5][0-9])";       // 00-59
            const string c_AmPm   = @"((_| )?(AM|PM))?";

            const string c_Year  = @"((19\d{2})|(2\d{3}))";  // 1900 - 2999
            const string c_Month = @"(0[1-9]|1[012])";       // 01-12
            const string c_Day   = @"(0[1-9]|[12]\d|3[01])"; // 01 - 31

            // Replace Dates YYYYMMDD / MMDDYYYY / DDMMYYYY
            fileName = Regex.Replace(fileName,
                                     "(" + c_DateSep + c_Year + c_DateSep + c_Month + c_DateSep + c_Day + ")|(" + c_DateSep + c_Month + c_DateSep +
                                     c_Day + c_DateSep + c_Year + ")|(" + c_DateSep + c_Day + c_DateSep + c_Month + c_DateSep + c_Year + ")",
                                     string.Empty, RegexOptions.Singleline);

            // Replace Times 3_53_34_AM
            fileName = Regex.Replace(fileName,
                                     c_DateSep + c_Hour + c_TimeSep + c_MinSec + c_TimeSep + c_MinSec + "?" + c_AmPm, string.Empty,
                                     RegexOptions.IgnoreCase | RegexOptions.Singleline);

            return(fileName.Trim('_', '-', ' ', '\t').Replace("__", "_").Replace("__", "_").Replace("--", "-")
                   .Replace("--", "-"));
        }
        protected BaseFileWriter([NotNull] string id,
                                 [NotNull] string fullPath, bool hasFieldHeader, [CanBeNull] IValueFormat valueFormatGeneral = null, [CanBeNull] IFileFormat fileFormat = null, [CanBeNull] string recipient = null,
                                 bool unencrypted          = false, [CanBeNull] string identifierInContainer = null,
                                 [CanBeNull] string footer = null, [CanBeNull] string header                 = null,
                                 [CanBeNull] IEnumerable <IColumn> columnDefinition = null, [NotNull] string fileSettingDisplay = null,
                                 [CanBeNull] IProcessDisplay processDisplay         = null)
        {
            m_FullPath = fullPath;
            var fileName = FileSystemUtils.GetFileName(fullPath);

            ColumnHeader = hasFieldHeader;
            if (valueFormatGeneral != null)
            {
                ValueFormatGeneral = new ImmutableValueFormat(valueFormatGeneral.DataType, valueFormatGeneral.DateFormat, valueFormatGeneral.DateSeparator,
                                                              valueFormatGeneral.TimeSeparator, valueFormatGeneral.NumberFormat, valueFormatGeneral.GroupSeparator, valueFormatGeneral.DecimalSeparator, valueFormatGeneral.True,
                                                              valueFormatGeneral.False, valueFormatGeneral.DisplayNullAs);
            }
            else
            {
                ValueFormatGeneral = new ImmutableValueFormat();
            }

            if (fileFormat != null)
            {
                FileFormat = new ImmutableFileFormat(fileFormat.IsFixedLength, fileFormat.QualifyAlways,
                                                     fileFormat.QualifyOnlyIfNeeded, fileFormat.EscapeChar, fileFormat.FieldDelimiterChar, fileFormat.DelimiterPlaceholder,
                                                     fileFormat.FieldQualifierChar, fileFormat.QuotePlaceholder, fileFormat.NewLine, fileFormat.NewLinePlaceholder);
            }
            else
            {
                FileFormat = new ImmutableFileFormat();
            }

            ColumnDefinition = columnDefinition?.Select(col => col is ImmutableColumn immutableColumn ? immutableColumn : new ImmutableColumn(col.Name, col.ValueFormat, col.ColumnOrdinal, col.Convert, col.DestinationName, col.Ignore, col.Part, col.PartSplitter, col.PartToEnd, col.TimePart, col.TimePartFormat, col.TimeZonePart)).ToList() ??
                               new List <ImmutableColumn>();
            NewLine = fileFormat.NewLine.NewLineString();
            if (!string.IsNullOrEmpty(header))
            {
                Header = ReplacePlaceHolder(StringUtils.HandleCRLFCombinations(header, NewLine), fileFormat.FieldDelimiterChar.GetDescription(),
                                            fileName, id);
            }
            else
            {
                Header = string.Empty;
            }
            if (!string.IsNullOrEmpty(footer))
            {
                m_Footer = ReplacePlaceHolder(StringUtils.HandleCRLFCombinations(footer, NewLine), fileFormat.FieldDelimiterChar.GetDescription(),
                                              fileName, id);
            }
            else
            {
                m_Footer = string.Empty;
            }
            m_FileSettingDisplay    = fileSettingDisplay ?? string.Empty;
            m_Recipient             = recipient ?? string.Empty;
            m_KeepUnencrypted       = unencrypted;
            m_IdentifierInContainer = identifierInContainer ?? string.Empty;

            Logger.Debug("Created Writer for {filesetting}", m_FileSettingDisplay);
            if (processDisplay == null)
            {
                return;
            }
            m_ReportProgress = t => processDisplay.SetProcess(t, 0, true);
            if (!(processDisplay is IProcessDisplayTime processDisplayTime))
            {
                return;
            }
            processDisplayTime.Maximum = 0;
            m_SetMaxProcess            = l => processDisplayTime.Maximum = l;
        }