// Copyright (c) Microsoft Open Technologies, Inc. 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.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.StaticFiles
{
///
/// 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([NotNull] this HttpResponse response)
{
return response.HttpContext.GetFeature() != null;
}
///
/// Sends the given file using the SendFile extension.
///
///
///
///
public static Task SendFileAsync([NotNull] this HttpResponse response, [NotNull] string fileName)
{
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([NotNull] this HttpResponse response, [NotNull] string fileName, long offset, long? count, CancellationToken cancellationToken)
{
var sendFile = response.HttpContext.GetFeature();
if (sendFile == null)
{
throw new NotSupportedException(Resources.Exception_SendFileNotSupported);
}
return sendFile.SendFileAsync(fileName, offset, count, cancellationToken);
}
}
}