private void SetFilter(WinForms.FileDialog dialog) { if (AllowedFileTypes != null) { var f = string.Join("; ", AllowedFileTypes.Select(s => "*." + s)); dialog.Filter = f + '|' + f; } }
/// <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('.')); }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var file = value as HttpPostedFileBase; if (file != null) { ErrorMessage = string.Format("File: {0} is not the required format!\n", validationContext.DisplayName); if (!AllowedFileTypes.AllowedFileTypesValidation(file, _methodName)) { return(new ValidationResult(ErrorMessage, new List <string>() { validationContext.DisplayName })); } } return(null); }
private void TextBox1_TextChanged(object sender, EventArgs e) { string file = TextBox1.Text; if (File.Exists(file) && !LockFileFoundEvent) { if (string.IsNullOrWhiteSpace(AllowedFileTypes) || (AllowedFileTypes.Contains(",") && AllowedFileTypes.Split(',').Any(x => file.ToLower().EndsWith(x.ToLower()))) || file.EndsWith(AllowedFileTypes)) { fileFound(this, new FileFoundEventArgs(file)); } else { TextBox1.Text = ""; } } }
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); }
public override void RecursivelyResolve(Dictionary <string, DataDefinition> local, Dictionary <string, DataDefinition> global, Dictionary <string, Dictionary <string, DataDefinition> > referenceableDefinitions) { if (ResourceType != null) { var key = ResourceType.ToLower(); Dictionary <string, DataDefinition> defs = null; if (local.ContainsKey(key)) { defs = local; } else if (global.ContainsKey(key)) { defs = global; } if (defs != null) { var def = defs[key]; if (def.IsRootLevel) { ResourceDataType = def; AllowedFileTypes.Clear(); AllowedFileTypes.Add(ResourceDataType.Extension); } else { Message.Show("Resource " + ResourceType + " is not a root level resource!", "Reference Resolve Failed", "Ok"); } } else { Message.Show("Failed to find key " + ResourceType + "!", "Reference Resolve Failed", "Ok"); } } base.RecursivelyResolve(local, global, referenceableDefinitions); }
public JsonResult UploadPhotoAdd(FormDataUpload form) { if (Response.IsClientConnected) { if (form.Photos == null || form.StringId == null) { return(Json(new { success = false })); } var dir = HostingEnvironment.MapPath("~/Photos/" + form.StringId.Substring(2, 4) + "/" + form.StringId.Substring(0, 4)); Directory.CreateDirectory(dir); List <UploadPhoto> phos = new List <UploadPhoto>(); int _curr = form.CurrentPhotoCount; int _max = form.MaxPhotoCount; for (int i = 0; i < form.Photos.Count && form.CurrentPhotoCount <= form.MaxPhotoCount && i < _max - _curr; i++, form.CurrentPhotoCount++) { HttpPostedFileBase photo = form.Photos.ElementAt(i); if (AllowedFileTypes.AllowedFileTypesValidation(photo, "CreateAd")) { var p = new UploadPhoto() { Original_FileName = photo.FileName }; // Compress image for ad list thumbnail PhotoEditing.CompressUploadPhoto(dir, true, false, photo, ref p); // Compress image for ad details thumbnail PhotoEditing.CompressUploadPhoto(dir, false, true, photo, ref p); // Compress imgae PhotoEditing.DefaultCompressionJpegUpload(dir, photo, ref p); // Set display src p.Src = "/Images/" + form.StringId.Substring(2, 4) + "/" + form.StringId.Substring(0, 4) + "/" + p.AdList_FileName; phos.Add(p); } } return(Json(new { success = true, photocount = new { current = form.CurrentPhotoCount }, photos = JsonConvert.SerializeObject(phos) })); } return(Json(new { success = false })); }
public async Task ChangeListedDirectory(IDirectory directory) { if (!directory.Exist) { return; } try { var directories = directory.GetDirectories(); var filesList = new List <KeyValuePair <FileType, string> > { new KeyValuePair <FileType, string>(FileType.Directory, AppSettings.ParentDirectory) }; filesList.AddRange( directories.Select(dir => new KeyValuePair <FileType, string>(FileType.Directory, dir.Name))); var files = (await directory.GetFilesAsync()) .Where(file => AllowedFileTypes.Any(allowedFileType => file.NameWithExtension.EndsWith(allowedFileType))); _currentDirectory = directory; filesList.AddRange(files.Select(file => new KeyValuePair <FileType, string>(FileType.File, file.NameWithExtension))); FilesList.Clear(); foreach (var keyValuePair in filesList) { FilesList.Add(keyValuePair); } } catch (Exception) { // ignored } }
protected override void DoRecursivelyResolve(Dictionary <string, DataDefinition> local, Dictionary <string, DataDefinition> global, Dictionary <string, Dictionary <string, DataDefinition> > referenceableDefinitions) { if (ResourceType != null) { var key = ResourceType.ToLower(); Dictionary <string, DataDefinition> defs = null; if (local.ContainsKey(key)) { defs = local; } else if (global.ContainsKey(key)) { defs = global; } if (defs != null) { var def = defs[key]; if (def.IsRootLevel) { ResourceDataType = def; AllowedFileTypes.Clear(); AllowedFileTypes.Add(ResourceDataType.Extension); } else { throw new Exception("Resource " + ResourceType + " is not a root level resource!"); } } else { throw new Exception("Failed to find key " + ResourceType + "!"); } } }