// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.StaticFiles;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.HttpFeature;
namespace Microsoft.Owin
{
///
/// Provides extensions for HttpResponse exposing the SendFile extension.
///
public static class SendFileResponseExtensions
{
///
/// Checks if the SendFile extension is supported.
///
///
/// True if sendfile.SendAsync is defined in the environment.
public static bool SupportsSendFile(this HttpResponse response)
{
if (response == null)
{
throw new ArgumentNullException("response");
}
return response.HttpContext.GetFeature() != null;
}
///
/// Sends the given file using the SendFile extension.
///
///
///
///
public static Task SendFileAsync(this HttpResponse response, string fileName)
{
if (response == null)
{
throw new ArgumentNullException("response");
}
return response.SendFileAsync(fileName, 0, null, CancellationToken.None);
}
///
/// Sends the given file using the SendFile extension.
///
///
/// The full or relative path to the file.
/// The offset in the file.
/// The number of types to send, or null to send the remainder of the file.
///
///
public static Task SendFileAsync(this HttpResponse response, string fileName, long offset, long? count, CancellationToken cancellationToken)
{
if (response == null)
{
throw new ArgumentNullException("response");
}
IHttpSendFile sendFile = response.HttpContext.GetFeature();
if (sendFile == null)
{
throw new NotSupportedException(Resources.Exception_SendFileNotSupported);
}
return sendFile.SendFileAsync(fileName, offset, count, cancellationToken);
}
}
}