public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var flowJs          = new FlowJsRepo();
            var request         = actionContext.Request;
            var validationRules = new FlowValidationRules();

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

            if (status.Status == PostChunkStatus.Done)
            {
                var filepath = Path.Combine(Path.GetTempPath(), status.FileName);

                var p = actionContext.ActionDescriptor.GetParameters()
                        .FirstOrDefault(x => x.ParameterType == typeof(FlowFile));

                if (filepath != null)
                {
                    actionContext.ActionArguments[p.ParameterName] = new FlowFile
                    {
                        originalFilename = status.OriginalFileName,
                        flowFilename     = status.FileName,
                        path             = filepath,
                        Identifier       = status.Identifier
                    };
                    return;
                }
            }


            actionContext.Response = new System.Net.Http.HttpResponseMessage(HttpStatusCode.Accepted);

            base.OnActionExecuting(actionContext);
        }
Beispiel #2
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 #3
0
        private FlowJsPostChunkResponse PostChunkBase(HttpRequest 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];

            chunk.FileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(chunk.FileName); //chunk.FileName.Split('.')[1];

            var response = new FlowJsPostChunkResponse {
                FileName = chunk.FileName, Identifier = chunk.Identifier, Size = chunk.TotalSize, OriginalFileName = file.FileName
            };

            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);
        }
Beispiel #4
0
 public FlowJsPostChunkResponse PostChunk(HttpRequest request, string folder, FlowValidationRules validationRules)
 {
     return(PostChunkBase(request, folder, validationRules));
 }