Skip to content

added support for streaming as per issue #5 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/back-end/ApiBackEnd/ApiBackEnd.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\NotImplementedMultipartFormDataModelBinderService.cs" />
<Compile Include="Startup.cs" />
<Compile Include="ViewModels\BasicUploadStreamedViewModel.cs" />
<Compile Include="ViewModels\BasicUploadViewModel.cs" />
<Compile Include="ViewModels\ClientResponseViewModel.cs" />
<Compile Include="ViewModels\StudentViewModel.cs" />
Expand Down
24 changes: 24 additions & 0 deletions example/back-end/ApiBackEnd/Controllers/ApiUploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ public IHttpActionResult BasicUpload(BasicUploadViewModel info)
return Ok(new ClientResponseViewModel(messages));
}

/// <summary>
/// Upload attachment to service end-point using streaming.
/// </summary>
/// <returns></returns>
[Route("basic-upload-streamed")]
[HttpPost]
public IHttpActionResult BasicUploadStreamed(BasicUploadStreamedViewModel info)
{
if (info == null)
{
info = new BasicUploadStreamedViewModel();
Validate(info);
}

if (!ModelState.IsValid)
return BadRequest(ModelState);

var messages = new List<string>();
messages.Add($"Author information: {info.Author.FullName}");
messages.Add($"Attachment information: (Mime) {info.StreamedAttachment.MediaType} - (File name) {info.StreamedAttachment.Name}");

return Ok(new ClientResponseViewModel(messages));
}

/// <summary>
/// Upload a list of attachment with author information.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.ComponentModel.DataAnnotations;
using ApiBackEnd.Models;
using ApiMultiPartFormData.Models;

namespace ApiBackEnd.ViewModels
{
public class BasicUploadStreamedViewModel
{
#region Properties

public Guid Id { get; set; }

/// <summary>
/// Author information.
/// </summary>
[Required]
public User Author { get; set; }

/// <summary>
/// Attachment.
/// </summary>
[Required]
public StreamedHttpFile StreamedAttachment { get; set; }

#endregion
}
}
2 changes: 2 additions & 0 deletions lib/ApiMultipartFormDataFormatter/ApiMultiPartFormData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApiMultipartFormDataFormatter.cs" />
<Compile Include="Models\HttpFileBase.cs" />
<Compile Include="Models\StreamedHttpFile.cs" />
<Compile Include="Services\Implementations\BaseMultiPartFormDataModelBinderService.cs" />
<Compile Include="Services\Interfaces\IMultiPartFormDataModelBinderService.cs" />
<Compile Include="Models\HttpFile.cs" />
Expand Down
12 changes: 1 addition & 11 deletions lib/ApiMultipartFormDataFormatter/Models/HttpFile.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
namespace ApiMultiPartFormData.Models
{
public class HttpFile
public class HttpFile : HttpFileBase
{
#region Properties

/// <summary>
/// Name of file.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Type of file.
/// </summary>
public string MediaType { get; set; }

/// <summary>
/// Serialized byte stream of file.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions lib/ApiMultipartFormDataFormatter/Models/HttpFileBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace ApiMultiPartFormData.Models
{
public abstract class HttpFileBase
{
#region Properties

/// <summary>
/// Name of file.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Type of file.
/// </summary>
public string MediaType { get; set; }

#endregion
}
}
52 changes: 52 additions & 0 deletions lib/ApiMultipartFormDataFormatter/Models/StreamedHttpFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.IO;

namespace ApiMultiPartFormData.Models
{
public class StreamedHttpFile : HttpFileBase
{
#region Properties

/// <summary>
/// Stream containing file contents.
/// </summary>
public Stream Stream { get; set; }

/// <summary>
/// Length of file stream
/// </summary>
public long StreamLength
{
get
{
return Stream.Length;
}
}

#endregion

#region Constructors

/// <summary>
/// Initialize an instance of StreamedHttpFile without any setting.
/// </summary>
public StreamedHttpFile()
{
}

/// <summary>
/// Initialize an instance of StreamedHttpFile with parameters.
/// </summary>
/// <param name="fileName"></param>
/// <param name="mediaType"></param>
/// <param name="stream"></param>
public StreamedHttpFile(string fileName, string mediaType, Stream stream)
{
Name = fileName;
MediaType = mediaType;
Stream = stream;
}

#endregion
}
}
31 changes: 23 additions & 8 deletions lib/ApiMultipartFormDataFormatter/MultipartFormDataFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,27 @@ public override async Task<object> ReadFromStreamAsync(Type type, Stream stream,

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

HttpFileBase file = null;

// set null if no content was submitted to have support for [Required]
if (httpContent.Headers.ContentLength.GetValueOrDefault() > 0)
file = new HttpFile(
httpContent.Headers.ContentDisposition.FileName.Trim('"'),
httpContent.Headers.ContentType.MediaType,
await httpContent.ReadAsByteArrayAsync()
);
else
file = null;
{
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);
}
Expand Down Expand Up @@ -380,6 +390,11 @@ private PropertyInfo FindPropertyInfoFromPointer(object instance, string name)
.FirstOrDefault(x => name.Equals(x.Name, StringComparison.InvariantCultureIgnoreCase));
}

private bool IsStreamingRequested(object instance, string contentParameter)
{
return instance.GetType().GetProperty(contentParameter)?.PropertyType == typeof(StreamedHttpFile);
}

/// <summary>
/// Check whether text is only numeric or not.
/// </summary>
Expand Down