internal bool ValidateBusinessRules(FlowValidationRules rules, out List <string> errorMessages) { errorMessages = new List <string>(); if (rules.MaxFileSize.HasValue && TotalSize > rules.MaxFileSize.Value) { errorMessages.Add(rules.MaxFileSizeMessage ?? "size"); } if (rules.AcceptedExtensions.Count > 0 && rules.AcceptedExtensions.SingleOrDefault(x => x.ToUpperInvariant() == FileName.Split('.').Last().ToUpperInvariant()) == null) { errorMessages.Add(rules.AcceptedExtensionsMessage ?? "type"); } return(errorMessages.Count == 0); }
private FlowJsPostChunkResponse PostChunkBase(HttpRequest request, string folder, FlowValidationRules validationRules) { var chunk = new FlowChunk(); if (!chunk.ParseForm(request.Form)) { var errResponse = new FlowJsPostChunkResponse(); errResponse.Status = PostChunkStatus.Error; errResponse.ErrorMessages.Add("damaged"); } List <string> errorMessages = null; var file = request.Files[0]; var response = new FlowJsPostChunkResponse { FileName = chunk.FileName, Size = chunk.TotalSize }; var chunkIsValid = true; if (validationRules != null) { chunkIsValid = chunk.ValidateBusinessRules(validationRules, out errorMessages); } if (!chunkIsValid) { response.Status = PostChunkStatus.Error; response.ErrorMessages = errorMessages; return(response); } string root = HttpContext.Current.Server.MapPath("~/Images"); var chunkFullPathName = GetChunkFilename(chunk.Number, chunk.Identifier, root); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } file.SaveAs(chunkFullPathName); for (int i = 1, l = chunk.TotalChunks; i <= l; i++) { var chunkNameToTest = GetChunkFilename(i, chunk.Identifier, root); if (!File.Exists(chunkNameToTest)) { response.Status = PostChunkStatus.PartlyDone; return(response); } } // if we are here, all chunks are uploaded var fileAry = new List <string>(); for (int i = 1, l = chunk.TotalChunks; i <= l; i++) { fileAry.Add("flow-" + chunk.Identifier + "." + i); } MultipleFilesToSingleFile(root, fileAry, chunk.FileName); for (int i = 0, l = fileAry.Count; i < l; i++) { try { File.Delete(Path.Combine(root, fileAry[i])); } catch (Exception) { } } response.Status = PostChunkStatus.Done; return(response); }
public FlowJsPostChunkResponse PostChunk(HttpRequest request, string folder, FlowValidationRules validationRules) { return(PostChunkBase(request, folder, validationRules)); }