Promote IFormFile extension methods to IFormFile

This commit is contained in:
Kristian Hellang 2016-01-13 12:34:00 +01:00
parent fa8c5cbb3a
commit 4b64d187c2
3 changed files with 91 additions and 62 deletions

View File

@ -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
{
@ -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));
}
}
}

View File

@ -1,60 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Http
{
/// <summary>
/// Extension methods for <see cref="IFormFile"/>.
/// </summary>
public static class FormFileExtensions
{
// Stream.CopyTo method uses 80KB as the default buffer size.
private static int DefaultBufferSize = 80 * 1024;
/// <summary>
/// Saves the contents of an uploaded file.
/// </summary>
/// <param name="formFile">The <see cref="IFormFile"/>.</param>
/// <param name="filename">The name of the file to create.</param>
public static void SaveAs(this IFormFile formFile, string filename)
{
if (formFile == null)
{
throw new ArgumentNullException(nameof(formFile));
}
using (var fileStream = new FileStream(filename, FileMode.Create))
{
var inputStream = formFile.OpenReadStream();
inputStream.CopyTo(fileStream);
}
}
/// <summary>
/// Asynchronously saves the contents of an uploaded file.
/// </summary>
/// <param name="formFile">The <see cref="IFormFile"/>.</param>
/// <param name="filename">The name of the file to create.</param>
public async static Task SaveAsAsync(
this IFormFile formFile,
string filename,
CancellationToken cancellationToken = default(CancellationToken))
{
if (formFile == null)
{
throw new ArgumentNullException(nameof(formFile));
}
using (var fileStream = new FileStream(filename, FileMode.Create))
{
var inputStream = formFile.OpenReadStream();
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
}
}
}
}

View File

@ -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;
@ -20,29 +25,77 @@ namespace Microsoft.AspNet.Http.Features.Internal
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);
}
}
}
}
}