Beispiel #1
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var flowJs          = new FlowJsRepo();
            var request         = filterContext.HttpContext.Request;
            var validationRules = new FlowValidationRules();

            validationRules.AcceptedExtensions.AddRange(Extensions);
            validationRules.MaxFileSize = Size;
            var status = flowJs.PostChunk(request, Path.GetTempPath(), validationRules);

            if (status.Status == PostChunkStatus.Done)
            {
                var filepath = Path.Combine(Path.GetTempPath(), status.FileName);
                var p        = filterContext.ActionDescriptor.GetParameters()
                               .FirstOrDefault(x => x.ParameterType == typeof(FlowFile));

                if (filepath != null)
                {
                    filterContext.ActionParameters[p.ParameterName] = new FlowFile
                    {
                        flowFilename = status.FileName,
                        path         = filepath
                    };
                    return;
                }
            }

            filterContext.Result = new HttpStatusCodeResult(HttpStatusCode.Accepted);

            base.OnActionExecuting(filterContext);
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var flowJs = new FlowJsRepo();
                var request = filterContext.HttpContext.Request;
                var validationRules = new FlowValidationRules();
                validationRules.AcceptedExtensions.AddRange(Extensions);
                validationRules.MaxFileSize = Size;
                var status = flowJs.PostChunk(request, Path.GetTempPath(),validationRules);

                if (status.Status == PostChunkStatus.Done)
                {
                    var filepath = Path.Combine(Path.GetTempPath(), status.FileName);
                    var p = filterContext.ActionDescriptor.GetParameters()
                        .FirstOrDefault(x => x.ParameterType == typeof (FlowFile));

                    if (filepath != null)
                    {
                        filterContext.ActionParameters[p.ParameterName] = new FlowFile
                        {
                            flowFilename = status.FileName,
                            path = filepath
                        };
                        return;
                    }
                }

                filterContext.Result = new HttpStatusCodeResult(HttpStatusCode.Accepted);

                base.OnActionExecuting(filterContext);
        }
        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 == FileName.Split('.').Last()) == null)
                errorMessages.Add(rules.AcceptedExtensionsMessage ?? "type");

            return errorMessages.Count == 0;
        }
Beispiel #4
0
        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 == FileName.Split('.').Last()) == null)
            {
                errorMessages.Add(rules.AcceptedExtensionsMessage ?? "type");
            }

            return(errorMessages.Count == 0);
        }
Beispiel #5
0
        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(HttpRequestBase request, string folder, FlowValidationRules validationRules)
        {
            var chunk = new FlowChunk();
            var requestIsSane = chunk.ParseForm(request.Form);
            if (!requestIsSane)
            {
                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;
            }

            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);

            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(HttpRequestBase request, string folder, FlowValidationRules validationRules)
 {
     return PostChunkBase(request, folder, validationRules);
 }
        private FlowJsPostChunkResponse PostChunkBase(HttpRequestBase request, string folder, FlowValidationRules validationRules)
        {
            var chunk         = new FlowChunk();
            var requestIsSane = chunk.ParseForm(request.Form);

            if (!requestIsSane)
            {
                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);
            }

            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);

            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(HttpRequestBase request, string folder, FlowValidationRules validationRules)
 {
     return(PostChunkBase(request, folder, validationRules));
 }