/// <summary> /// Create a set of rules to limit invalid parse transactions (and reduce parsing costs). /// </summary> /// <param name="maxBatchSize"> /// The maximum amount of files allowed in a batch parse. If a directory contains more valid files, an error is thrown. /// This is important to keep users from unknowingly consuming large numbers of parsing credits. /// </param> /// <param name="disallowedFileTypes"> /// File types to skip. Use the <see cref="DefaultDisallowedFileTypes"/> unless you have a specific use case. /// </param> /// <param name="allowedFileTypes"> /// File types to exclusively allow. ANY value in here will mean the <see cref="DisallowedFileTypes"/> /// property is ignored and only types in this list are allowed. /// </param> /// <param name="shouldProcessFn"> /// A custom function to decide whether or not a file should be parsed. It should return <see langword="true"/> to parse the file. /// This could be used, for example, to check if you have already parsed a particular /// file in your system before spending credits to parse it again. /// NOTE: If defined, this will be called only AFTER a file passes the other 'file type' checks. /// </param> public BatchParsingRules(int maxBatchSize, IEnumerable <string> disallowedFileTypes = null, IEnumerable <string> allowedFileTypes = null, Func <string, bool> shouldProcessFn = null) { MaxBatchSize = maxBatchSize; DisallowedFileTypes = disallowedFileTypes ?? DefaultDisallowedFileTypes; ShouldProcessFile = shouldProcessFn; AllowedFileTypes = allowedFileTypes ?? new List <string>(); //remove any leading periods DisallowedFileTypes = DisallowedFileTypes.Select(s => s.TrimStart('.')); AllowedFileTypes = AllowedFileTypes.Select(s => s.TrimStart('.')); }
internal bool FileIsAllowed(string file) { string fileExt = Path.GetExtension(file); if (AllowedFileTypes.Any() && !AllowedFileTypes.Contains(fileExt, StringComparer.OrdinalIgnoreCase)) { return(false); } if (DisallowedFileTypes.Any() && DisallowedFileTypes.Contains(fileExt, StringComparer.OrdinalIgnoreCase)) { return(false); } if (ShouldProcessFile != null) { return(ShouldProcessFile(file)); } return(true); }