// 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.Collections.Generic;
using Microsoft.AspNet.Builder;
namespace Microsoft.AspNet.StaticFiles
{
///
/// Extension methods for the SendFileMiddleware
///
public static class SendFileExtensions
{
///
/// Provide a SendFile fallback if another component does not.
///
///
///
public static IBuilder UseSendFileFallback(this IBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException("builder");
}
/* TODO builder.GetItem(typeof(ISendFile))
// Check for advertised support
if (IsSendFileSupported(builder.Properties))
{
return builder;
}
// Otherwise, insert a fallback SendFile middleware and advertise support
SetSendFileCapability(builder.Properties);
*/
return builder.Use(next => new SendFileMiddleware(next).Invoke);
}
private static bool IsSendFileSupported(IDictionary properties)
{
object obj;
if (properties.TryGetValue(Constants.ServerCapabilitiesKey, out obj))
{
var capabilities = (IDictionary)obj;
if (capabilities.TryGetValue(Constants.SendFileVersionKey, out obj)
&& Constants.SendFileVersion.Equals((string)obj, StringComparison.Ordinal))
{
return true;
}
}
return false;
}
private static void SetSendFileCapability(IDictionary properties)
{
object obj;
if (properties.TryGetValue(Constants.ServerCapabilitiesKey, out obj))
{
var capabilities = (IDictionary)obj;
capabilities[Constants.SendFileVersionKey] = Constants.SendFileVersion;
}
}
}
}