public static byte[] ToBytes(this HttpFileBase attachment)
 {
     using (var memoryStream = new MemoryStream())
     {
         attachment.InputStream.CopyTo(memoryStream);
         return(memoryStream.ToArray());
     }
 }
        public void HandleNotHttpFileType_Throws_UnhandledParameterException(Type type)
        {
            var httpFileBase = new HttpFileBase("attachment-001.jpg", new MemoryStream(), "image/jpg");
            var httpFileModelBinderService = new HttpFileModelBinderService();

            Assert.CatchAsync <UnhandledParameterException>(() => httpFileModelBinderService
                                                            .BuildModelAsync(type, httpFileBase));
        }
        public override async Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
        {
            var type = context.ModelType;

            // Type is invalid.
            if (type == null)
            {
                throw new ArgumentNullException($"{nameof(context)}.{nameof(context.ModelType)} cannot be null.");
            }

            try
            {
                // load multipart data into memory
                var httpContents = await context.HttpContext.Request.ReadFormAsync();

                // Create an instance from specific type.
                var instance = Activator.CreateInstance(type);

                foreach (var httpContent in httpContents)
                {
                    // Find parameter from content deposition.
                    var contentParameter = httpContent.GetContentDispositionName();
                    var parameterParts   = FindContentDispositionParameters(contentParameter);

                    // Content is a parameter, not a file.
                    var value = await httpContent.ReadAsStringAsync();
                    await BuildRequestModelAsync(instance, parameterParts, value);
                }

                foreach (var httpFile in httpContents.Files)
                {
                    // Content is a file.
                    // File retrieved from client-side.
                    var contentParameter = httpFile.Name;
                    var parameterParts   = FindContentDispositionParameters(contentParameter);

                    var file = new HttpFileBase(
                        httpFile.GetFileName(),
                        await httpFile.ReadAsStreamAsync(),
                        httpFile.GetContentType());

                    await BuildRequestModelAsync(instance, parameterParts, file);
                }

                return(InputFormatterResult.Success(instance));
            }
            catch (Exception e)
            {
                // TODO: Implement logger.
                //if (logger == null)
                //    throw;
                //logger.LogError(string.Empty, e);
                var defaultValue = GetDefaultValueForType(type);
                return(InputFormatterResult.Success(defaultValue));
            }
        }
        public virtual Task <HttpFileBase> LoadSampleAttachmentAsync()
        {
            var encodedAttachment =
                "iVBORw0KGgoAAAANSUhEUgAAAIAAAAC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gQqghrAxGx6ER/COodlRWNcyFUAN4mVZOOqT4VgBHgQqgBomrHoQBnxCXg8CFYAFaHHIigehL4cHgQrAQnSq8yB0b/IgoADK08dMMSbk8baHiAehjsOkUYT/A9goFRCT5lXBAAAAAElFTkSuQmCC";

            var bytes      = Convert.FromBase64String(encodedAttachment);
            var attachment = new HttpFileBase("attachment-001.jpg", new MemoryStream(bytes), "image/jpg");

            return(Task.FromResult(attachment));
        }
        public static ByteArrayContent ToByteArrayContent(this HttpFileBase attachment, string parameter)
        {
            var memoryStream = new MemoryStream();

            attachment.InputStream.CopyTo(memoryStream);
            var content = new ByteArrayContent(memoryStream.ToArray());

            content.Headers.ContentType                 = MediaTypeHeaderValue.Parse(attachment.ContentType);
            content.Headers.ContentDisposition          = ContentDispositionHeaderValue.Parse("attachment");
            content.Headers.ContentDisposition.FileName = attachment.FileName;
            content.Headers.ContentDisposition.Name     = parameter;

            return(content);
        }
        /// <summary>
        ///     Read data from incoming stream.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="stream"></param>
        /// <param name="content"></param>
        /// <param name="logger"></param>
        /// <returns></returns>
        public override async Task <object> ReadFromStreamAsync(Type type, Stream stream, HttpContent content,
                                                                IFormatterLogger logger)
        {
            // Stream is invalid.
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            // Type is invalid.
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            try
            {
                // load multipart data into memory
                var multipartProvider = await content.ReadAsMultipartAsync();

                var httpContents = multipartProvider.Contents;

                // Create an instance from specific type.
                var instance = Activator.CreateInstance(type);

                foreach (var httpContent in httpContents)
                {
                    // Find parameter from content deposition.
                    var contentParameter = httpContent.GetContentDispositionName();
                    var parameterParts   = FindContentDispositionParameters(contentParameter);

                    // Content is a parameter, not a file.
                    if (!httpContent.IsAttachment())
                    {
                        var value = await httpContent.ReadAsStringAsync();
                        await BuildRequestModelAsync(instance, parameterParts, value);

                        continue;
                    }

                    // Content is a file.
                    // File retrieved from client-side.
                    HttpFileBase file = null;

                    // set null if no content was submitted to have support for [Required]
                    if (httpContent.Headers.ContentLength.GetValueOrDefault() > 0)
                    {
                        file = new HttpFileBase(
                            httpContent.GetFileName(),
                            await httpContent.ReadAsStreamAsync(),
                            httpContent.GetContentType());
                    }

                    await BuildRequestModelAsync(instance, parameterParts, file);
                }

                return(instance);
            }
            catch (Exception e)
            {
                // TODO: Implement logger.
                //if (logger == null)
                //    throw;
                //logger.LogError(string.Empty, e);
                var defaultValue = GetDefaultValueForType(type);
                return(defaultValue);
            }
        }
 public HttpFileBaseResponseViewModel(HttpFileBase model)
 {
     FileName      = model.FileName;
     ContentType   = model.ContentType;
     ContentLength = model.ContentLength;
 }
        /// <summary>
        ///     Read data from incoming stream.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="stream"></param>
        /// <param name="content"></param>
        /// <param name="formatterLogger"></param>
        /// <returns></returns>
        public override async Task <object> ReadFromStreamAsync(Type type, Stream stream, HttpContent content,
                                                                IFormatterLogger formatterLogger)
        {
            // Type is invalid.
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // Stream is invalid.
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            // Find dependency resolver.
            var dependencyResolver = GlobalConfiguration.Configuration.DependencyResolver;

            if (dependencyResolver == null)
            {
                throw new ArgumentException("Dependency resolver is required.");
            }

            using (var dependencyScope = dependencyResolver.BeginScope())
            {
                try
                {
                    // load multipart data into memory
                    var multipartProvider = await content.ReadAsMultipartAsync();

                    var httpContents = multipartProvider.Contents;

                    // Create an instance from specific type.
                    var instance = Activator.CreateInstance(type);

                    foreach (var httpContent in httpContents)
                    {
                        // Find parameter from content deposition.
                        var contentParameter = httpContent.Headers.ContentDisposition.Name.Trim('"');
                        var parameterParts   = FindContentDispositionParameters(contentParameter);

                        // Content is a parameter, not a file.
                        if (string.IsNullOrEmpty(httpContent.Headers.ContentDisposition.FileName))
                        {
                            var value = await httpContent.ReadAsStringAsync();
                            await BuildRequestModelAsync(instance, parameterParts, value, dependencyScope);

                            continue;
                        }

                        // Content is a file.
                        // File retrieved from client-side.

                        HttpFileBase file = null;

                        // set null if no content was submitted to have support for [Required]
                        if (httpContent.Headers.ContentLength.GetValueOrDefault() > 0)
                        {
                            if (IsStreamingRequested(instance, contentParameter))
                            {
                                file = new StreamedHttpFile(
                                    httpContent.Headers.ContentDisposition.FileName.Trim('"'),
                                    httpContent.Headers.ContentType.MediaType,
                                    await httpContent.ReadAsStreamAsync());
                            }
                            else
                            {
                                file = new HttpFile(
                                    httpContent.Headers.ContentDisposition.FileName.Trim('"'),
                                    httpContent.Headers.ContentType.MediaType,
                                    await httpContent.ReadAsByteArrayAsync());
                            }
                        }

                        await BuildRequestModelAsync(instance, parameterParts, file, dependencyScope);
                    }

                    return(instance);
                }
                catch (Exception e)
                {
                    if (formatterLogger == null)
                    {
                        throw;
                    }
                    formatterLogger.LogError(string.Empty, e);
                    return(GetDefaultValueForType(type));
                }
            }
        }