// 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;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.StaticFiles
{
///
/// Extension methods for the SendFileMiddleware
///
public static class SendFileExtensions
{
///
/// Provide a SendFile fallback if another component does not.
///
///
///
public static IApplicationBuilder UseSendFileFallback([NotNull] this IApplicationBuilder 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.UseMiddleware();
}
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;
}
}
}
}