/
Configuration.cs
138 lines (116 loc) · 4.14 KB
/
Configuration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
namespace NuGetGallery
{
public class Configuration : IConfiguration
{
private static readonly Dictionary<string, Lazy<object>> _configThunks = new Dictionary<string, Lazy<object>>();
private readonly Lazy<string> _httpSiteRootThunk;
private readonly Lazy<string> _httpsSiteRootThunk;
public Configuration()
{
_httpSiteRootThunk = new Lazy<string>(GetHttpSiteRoot);
_httpsSiteRootThunk = new Lazy<string>(GetHttpsSiteRoot);
}
public static string ReadAppSettings(string key)
{
return ReadAppSettings(key, value => value);
}
public static T ReadAppSettings<T>(
string key,
Func<string, T> valueThunk)
{
if (!_configThunks.ContainsKey(key))
_configThunks.Add(key, new Lazy<object>(() =>
{
var value = ConfigurationManager.AppSettings[string.Format("Gallery:{0}", key)];
if (string.IsNullOrWhiteSpace(value))
value = null;
return valueThunk(value);
}));
return (T)_configThunks[key].Value;
}
public string AzureStorageAccessKey
{
get
{
return ReadAppSettings("AzureStorageAccessKey");
}
}
public string AzureStorageAccountName
{
get
{
return ReadAppSettings("AzureStorageAccountName");
}
}
public string AzureStorageBlobUrl
{
get
{
return ReadAppSettings("AzureStorageBlobUrl");
}
}
public string FileStorageDirectory
{
get
{
return ReadAppSettings(
"FileStorageDirectory",
value => value ?? HttpContext.Current.Server.MapPath("~/App_Data/Files"));
}
}
public PackageStoreType PackageStoreType
{
get
{
return ReadAppSettings(
"PackageStoreType",
value => (PackageStoreType)Enum.Parse(typeof(PackageStoreType), value ?? PackageStoreType.NotSpecified.ToString()));
}
}
public string AzureCdnHost
{
get
{
return ReadAppSettings("AzureCdnHost");
}
}
protected virtual string GetConfiguredSiteRoot()
{
return ReadAppSettings("SiteRoot");
}
protected virtual HttpRequestBase GetCurrentRequest()
{
return new HttpRequestWrapper(HttpContext.Current.Request);
}
public string GetSiteRoot(bool useHttps)
{
return useHttps ? _httpsSiteRootThunk.Value : _httpSiteRootThunk.Value;
}
private string GetHttpSiteRoot()
{
var request = GetCurrentRequest();
string siteRoot;
if (request.IsLocal)
siteRoot = request.Url.GetLeftPart(UriPartial.Authority) + '/';
else
siteRoot = GetConfiguredSiteRoot();
if (!siteRoot.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
&& !siteRoot.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("The configured site root must start with either http:// or https://.");
if (siteRoot.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
siteRoot = "http://" + siteRoot.Substring(8);
return siteRoot;
}
private string GetHttpsSiteRoot()
{
var siteRoot = _httpSiteRootThunk.Value;
if (!siteRoot.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("The configured HTTP site root must start with http://.");
return "https://" + siteRoot.Substring(7);
}
}
}