Пример #1
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var response = await base.SendAsync(request, cancellationToken);

            if (response.Content != null && response.Content is ObjectContent)
            {
                var kFileInfo = default(KFileInfo);

                var objectContent = (ObjectContent)response.Content;

                if (objectContent.ObjectType == typeof(KFileInfo))
                {
                    kFileInfo = (KFileInfo)objectContent.Value;
                }
                else if (objectContent.ObjectType == typeof(byte[]))
                {
                    kFileInfo = KFileInfo.FromBytes((byte[])objectContent.Value);
                }
                else if (objectContent.ObjectType == typeof(Stream))
                {
                    kFileInfo = KFileInfo.FromStream((Stream)objectContent.Value);
                }
                else if (objectContent.ObjectType == typeof(FileInfo))
                {
                    kFileInfo = KFileInfo.FromFileInfo((FileInfo)objectContent.Value);
                }

                if (kFileInfo != null)
                {
                    response = kFileInfo.AsResponse(true);
                }
            }

            return(response);
        }
Пример #2
0
        public override async Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            if (actionContext.ActionDescriptor.GetCustomAttributes <KMethod>().Any())
            {
                var value = Descriptor.DefaultValue;

                try
                {
                    ResetStream(await actionContext.Request.Content.ReadAsStreamAsync());

                    // TYPE : query string

                    var queryValues = actionContext.Request.GetQueryNameValuePairs().Where(x => x.Key.Equals(Descriptor.ParameterName, StringComparison.OrdinalIgnoreCase));
                    if (queryValues.Any())
                    {
                        value = ConvertValueToParameterType(queryValues.First().Value);
                    }

                    // TYPE : multipart/form-data

                    if (actionContext.Request.Content.IsMimeMultipartContent())
                    {
                        var provider = await actionContext.Request.Content.ReadAsMultipartAsync();

                        try
                        {
                            var httpContent = provider.Contents.FirstOrDefault(hc => hc.Headers.ContentDisposition.Name.Equals(Descriptor.ParameterName, StringComparison.OrdinalIgnoreCase) || hc.Headers.ContentDisposition.Name.Equals($"\"{Descriptor.ParameterName}\"", StringComparison.OrdinalIgnoreCase));
                            if (httpContent != null)
                            {
                                if (string.IsNullOrWhiteSpace(httpContent.Headers.ContentDisposition.FileName) && string.IsNullOrWhiteSpace(httpContent.Headers.ContentDisposition.FileNameStar))
                                {
                                    value = ConvertValueToParameterType(await httpContent.ReadAsStringAsync());
                                }
                                else
                                {
                                    if (Descriptor.ParameterType == typeof(KFileInfo))
                                    {
                                        value = KFileInfo.FromStream(await httpContent.ReadAsStreamAsync(), string.IsNullOrWhiteSpace(httpContent.Headers.ContentDisposition.FileName.Trim(' ', '"')) ? httpContent.Headers.ContentDisposition.FileNameStar.Trim(' ', '"') : httpContent.Headers.ContentDisposition.FileName.Trim(' ', '"'), httpContent.Headers.ContentType.MediaType, httpContent.Headers.ContentLength);
                                    }
                                    else if (Descriptor.ParameterType == typeof(byte[]))
                                    {
                                        value = await httpContent.ReadAsByteArrayAsync();
                                    }
                                    else if (Descriptor.ParameterType == typeof(Stream))
                                    {
                                        value = await httpContent.ReadAsStreamAsync();
                                    }
                                    else if (Descriptor.ParameterType == typeof(FileInfo))
                                    {
                                        var tempFileName = Path.GetTempFileName();
                                        File.WriteAllBytes(tempFileName, await httpContent.ReadAsByteArrayAsync());
                                        value = new FileInfo(tempFileName);
                                    }
                                    else if (Descriptor.ParameterType == typeof(string))
                                    {
                                        value = Convert.ToBase64String(await httpContent.ReadAsByteArrayAsync());
                                    }
                                }
                            }
                        }
                        finally
                        {
                            ResetStream(await actionContext.Request.Content.ReadAsStreamAsync());
                        }
                    }

                    // TYPE : application/x-www-form-urlencode

                    if (actionContext.Request.Content.IsFormData())
                    {
                        var formData = await actionContext.Request.Content.ReadAsFormDataAsync();

                        if (formData != null && formData.AllKeys.Any(x => x.Equals(Descriptor.ParameterName, StringComparison.OrdinalIgnoreCase)))
                        {
                            value = ConvertValueToParameterType(formData[Descriptor.ParameterName]);
                        }
                    }

                    // TYPE : application/json

                    if (actionContext.Request.Content?.Headers?.ContentType?.MediaType?.ToLower() == "application/json")
                    {
                        var json = await actionContext.Request.Content.ReadAsStringAsync();

                        if (!string.IsNullOrWhiteSpace(json))
                        {
                            var jObject = JsonConvert.DeserializeObject <JObject>(json, KHelper.JsonSerializerSettings);                            // we expect a json object, must blow up otherwise
                            value = ConvertValueToParameterType(jObject.Properties().Where(x => x.Name.Equals(Descriptor.ParameterName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault());
                        }
                    }
                }
                catch (Exception ex)
                {
                    value = Descriptor.DefaultValue;
                }
                finally
                {
                    ResetStream(await actionContext.Request.Content.ReadAsStreamAsync());
                    SetValue(actionContext, value);
                }
            }
        }