internal bool ValidateBusinessRules(FlowValidationRules rules, out List <string> errorMessages) { var acceptedFileExtensions = new AcceptedFileExtensions(); errorMessages = new List <string>(); if (rules.MaxFileSize.HasValue && TotalSize > rules.MaxFileSize.Value) { errorMessages.Add(rules.MaxFileSizeMessage ?? "size"); } if (!acceptedFileExtensions.IsExtensionAllowed(rules.AcceptedExtensions, FileName)) { errorMessages.Add(rules.AcceptedExtensionsMessage ?? "type"); } return(errorMessages.Count == 0); }
private FlowJsPostChunkResponse PostChunkBase(HttpRequestMessage request, string folder, FlowValidationRules validationRules) { var chunk = new FlowChunk(); //var requestIsSane = chunk.ParseForm(request.Form); var requestIsSane = chunk.ParseForm((Dictionary <string, object>)request.Properties); if (!requestIsSane) { var errResponse = new FlowJsPostChunkResponse(); errResponse.Status = PostChunkStatus.Error; errResponse.ErrorMessages.Add("damaged"); } List <string> errorMessages = null; //var file = request.Files[0]; var file = File.Create(""); //var response = new FlowJsPostChunkResponse { FileName = chunk.FileName, Size = chunk.TotalSize }; var fileExtension = chunk.FileName.Substring(chunk.FileName.LastIndexOf('.')); var newFileName = Guid.NewGuid().ToString(); var newFullFileName = String.Format("{0}{1}", newFileName, fileExtension); var response = new FlowJsPostChunkResponse { Path = folder, OriginalFileName = chunk.FileName, FileName = newFileName, FileExtension = fileExtension, Size = chunk.TotalSize, CreationDate = DateTime.Now.ToString() }; var chunkIsValid = true; if (validationRules != null) { chunkIsValid = chunk.ValidateBusinessRules(validationRules, out errorMessages); } if (!chunkIsValid) { response.Status = PostChunkStatus.Error; response.ErrorMessages = errorMessages; return(response); } var chunkFullPathName = GetChunkFilename(chunk.Number, chunk.Identifier, folder); try { // create folder if it does not exist if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } // save file //file.SaveAs(chunkFullPathName); } catch (Exception) { throw; } // see if we have more chunks to upload. If so, return here for (int i = 1, l = chunk.TotalChunks; i <= l; i++) { var chunkNameToTest = GetChunkFilename(i, chunk.Identifier, folder); var exists = File.Exists(chunkNameToTest); if (!exists) { 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(folder, fileAry, chunk.FileName); MultipleFilesToSingleFile(folder, fileAry, newFullFileName); for (int i = 0, l = fileAry.Count; i < l; i++) { try { File.Delete(Path.Combine(folder, fileAry[i])); } catch (Exception) { } } response.Status = PostChunkStatus.Done; return(response); }
public FlowJsPostChunkResponse PostChunk(HttpRequestMessage request, string folder, FlowValidationRules validationRules) { return(PostChunkBase(request, folder, validationRules)); }