public static IEnumerable<String> getContentTypes(AllowedContentTypes types) { HashSet<String> total = new HashSet<string>(); if ((types & AllowedContentTypes.XML) != 0) { total.UnionWith(ALLOWED_XML_CONTENT_TYPES); } if ((types & AllowedContentTypes.ATOM) != 0) { total.UnionWith(ALLOWED_ATOM_CONTENT_TYPES); } if ((types & AllowedContentTypes.JSON) != 0) { total.UnionWith(ALLOWED_JSON_CONTENT_TYPES); } if ((types & AllowedContentTypes.MULTIPART) != 0) { total.UnionWith(ALLOWED_MULTIPART_CONTENT_TYPES); } return total; }
public static IEnumerable <String> getContentTypes(AllowedContentTypes types) { HashSet <String> total = new HashSet <string>(); if ((types & AllowedContentTypes.XML) != 0) { total.UnionWith(ALLOWED_XML_CONTENT_TYPES); } if ((types & AllowedContentTypes.ATOM) != 0) { total.UnionWith(ALLOWED_ATOM_CONTENT_TYPES); } if ((types & AllowedContentTypes.JSON) != 0) { total.UnionWith(ALLOWED_JSON_CONTENT_TYPES); } if ((types & AllowedContentTypes.MULTIPART) != 0) { total.UnionWith(ALLOWED_MULTIPART_CONTENT_TYPES); } return(total); }
/// <summary> /// Validates that a required binary data have been send on the request /// </summary> public override void OnActionExecuting(ActionExecutingContext filterContext) { if (AllowedBlobMaxLength <= 0) { throw new ArgumentException("The AllowedBlobMaxLength can't be zero or less."); } if (AllowedMinimunFiles <= 0) { throw new ArgumentException("The AllowedMinimunFiles can't be zero or less."); } if (AllowedMaximunFiles <= 0) { throw new ArgumentException("The AllowedMaximunFiles can't be zero or less."); } var formFiles = filterContext.HttpContext.Request.Form.Files; var blobs = string.IsNullOrWhiteSpace(FormFieldName) ? formFiles : formFiles.Where(w => w.Name == FormFieldName); if (!blobs.Any()) { filterContext.Result = new ApiResponse <string>() { Status = ApiResponseStatus.Failed, Message = "FileUploadRequired" }.ToJson(); } else { if (blobs.Count() < AllowedMinimunFiles) { filterContext.Result = new ApiResponse <string>() { Status = ApiResponseStatus.Failed, Message = "TheCountOfFilesUploadedHasExceededTheMaximunAllowedFiles" }.ToJson(); } if (blobs.Count() > AllowedMaximunFiles) { filterContext.Result = new ApiResponse <string>() { Status = ApiResponseStatus.Failed, Message = "TheCountOfFilesUploadedIsLessThanTheMinimunAllowedFiles" }.ToJson(); } foreach (var blob in blobs) { if (blob == null || blob.Length == 0) { filterContext.Result = new ApiResponse <string>() { Status = ApiResponseStatus.Failed, Message = "FileUploadRequired", Data = blob.Name }.ToJson(); } else if (blob.Length > AllowedBlobMaxLength) { filterContext.Result = new ApiResponse <string>() { Status = ApiResponseStatus.Failed, Message = "TheFileHasExceededTheLargestSizeAllowed", Data = blob.Name }.ToJson(); } else if (AllowedContentTypes != null && AllowedContentTypes.Any() && !AllowedContentTypes.Contains(blob.ContentType)) { filterContext.Result = new ApiResponse <string>() { Status = ApiResponseStatus.Failed, Message = "TheFileDoesNotHaveTheExpectedContentType", Data = blob.Name }.ToJson(); } else if (AllowedExtensions != null && AllowedExtensions.Any() && !AllowedExtensions.Contains(Path.GetExtension(blob.FileName))) { filterContext.Result = new ApiResponse <string>() { Status = ApiResponseStatus.Failed, Message = "TheFileDoesNotHaveTheExpectedExtension", Data = blob.Name }.ToJson(); } else if (AllowedContentDispositions != null && AllowedContentDispositions.Any() && !AllowedContentDispositions.Contains(blob.ContentDisposition)) { filterContext.Result = new ApiResponse <string>() { Status = ApiResponseStatus.Failed, Message = "TheFileDoesNotHaveTheExpectedContentDisposition", Data = blob.Name }.ToJson(); } } } base.OnActionExecuting(filterContext); }