Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Promote IFormFile extension methods #534

Merged
merged 1 commit into from
Jan 13, 2016
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
38 changes: 37 additions & 1 deletion src/Microsoft.AspNet.Http.Abstractions/IFormFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNet.Http
{
Expand All @@ -10,18 +12,52 @@ namespace Microsoft.AspNet.Http
/// </summary>
public interface IFormFile
{
/// <summary>
/// Gets the raw Content-Type header of the uploaded file.
/// </summary>
string ContentType { get; }

/// <summary>
/// Gets the raw Content-Disposition header of the uploaded file.
/// </summary>
string ContentDisposition { get; }

/// <summary>
/// Gets the header dictionary of the uploaded file.
/// </summary>
IHeaderDictionary Headers { get; }

/// <summary>
/// Gets the file length in bytes.
/// </summary>
long Length { get; }

/// <summary>
/// Gets the name from the Content-Disposition header.
/// </summary>
string Name { get; }

/// <summary>
/// Gets the file name from the Content-Disposition header.
/// </summary>
string FileName { get; }

/// <summary>
/// Opens the request stream for reading the uploaded file.
/// </summary>
Stream OpenReadStream();

/// <summary>
/// Saves the contents of the uploaded file.
/// </summary>
/// <param name="path">The path of the file to create.</param>
void SaveAs(string path);

/// <summary>
/// Asynchronously saves the contents of the uploaded file.
/// </summary>
/// <param name="path">The path of the file to create.</param>
/// <param name="cancellationToken"></param>
Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken));
}
}
}
60 changes: 0 additions & 60 deletions src/Microsoft.AspNet.Http.Extensions/FormFileExtensions.cs

This file was deleted.

55 changes: 54 additions & 1 deletion src/Microsoft.AspNet.Http/Features/FormFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Http.Internal;

namespace Microsoft.AspNet.Http.Features.Internal
{
public class FormFile : IFormFile
{
// Stream.CopyTo method uses 80KB as the default buffer size.
private const int DefaultBufferSize = 80 * 1024;

private readonly Stream _baseStream;
private readonly long _baseStreamOffset;

Expand All @@ -20,29 +25,77 @@ public FormFile(Stream baseStream, long baseStreamOffset, long length, string na
FileName = fileName;
}

/// <summary>
/// Gets the raw Content-Disposition header of the uploaded file.
/// </summary>
public string ContentDisposition
{
get { return Headers["Content-Disposition"]; }
set { Headers["Content-Disposition"] = value; }
}

/// <summary>
/// Gets the raw Content-Type header of the uploaded file.
/// </summary>
public string ContentType
{
get { return Headers["Content-Type"]; }
set { Headers["Content-Type"] = value; }
}

/// <summary>
/// Gets the header dictionary of the uploaded file.
/// </summary>
public IHeaderDictionary Headers { get; set; }

/// <summary>
/// Gets the file length in bytes.
/// </summary>
public long Length { get; }

/// <summary>
/// Gets the name from the Content-Disposition header.
/// </summary>
public string Name { get; }

/// <summary>
/// Gets the file name from the Content-Disposition header.
/// </summary>
public string FileName { get; }

/// <summary>
/// Opens the request stream for reading the uploaded file.
/// </summary>
public Stream OpenReadStream()
{
return new ReferenceReadStream(_baseStream, _baseStreamOffset, Length);
}

/// <summary>
/// Saves the contents of the uploaded file.
/// </summary>
/// <param name="path">The path of the file to create.</param>
public void SaveAs(string path)
{
using (var fileStream = File.Create(path, DefaultBufferSize))
{
var inputStream = OpenReadStream();
inputStream.CopyTo(fileStream, DefaultBufferSize);
}
}

/// <summary>
/// Asynchronously saves the contents of the uploaded file.
/// </summary>
/// <param name="path">The path of the file to create.</param>
/// <param name="cancellationToken"></param>
public async Task SaveAsAsync(string path, CancellationToken cancellationToken = default(CancellationToken))
{
using (var fileStream = File.Create(path, DefaultBufferSize, FileOptions.Asynchronous))
{
var inputStream = OpenReadStream();
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
}
}
}
}
}