Skip to content

Commit 5116958

Browse files
committed
Added configuration sourcing through web.config
1 parent e48d775 commit 5116958

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/UmbracoFileSystemProviders.Azure/AzureBlobFileSystem.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,32 @@ namespace Our.Umbraco.FileSystemProviders.Azure
1212
using System.IO;
1313

1414
using global::Umbraco.Core.IO;
15-
15+
using System.Configuration;
1616
/// <summary>
1717
/// The azure file system.
1818
/// </summary>
1919
public class AzureBlobFileSystem : IFileSystem
2020
{
21+
/// <summary>
22+
/// The configuration key for disabling the virtual path provider.
23+
/// </summary>
24+
private const string ConnectionStringKey = Constants.Configuration.ConnectionStringKey;
25+
26+
/// <summary>
27+
/// The configuration key for disabling the virtual path provider.
28+
/// </summary>
29+
private const string ContainerNameKey = Constants.Configuration.ContainerNameKey;
30+
31+
/// <summary>
32+
/// The configuration key for disabling the virtual path provider.
33+
/// </summary>
34+
private const string RootUrlKey = Constants.Configuration.RootUrlKey;
35+
36+
/// <summary>
37+
/// The configuration key for disabling the virtual path provider.
38+
/// </summary>
39+
private const string MaxDaysKey = Constants.Configuration.MaxDaysKey;
40+
2141
/// <summary>
2242
/// Initializes a new instance of the <see cref="AzureBlobFileSystem"/> class.
2343
/// </summary>
@@ -41,6 +61,45 @@ public AzureBlobFileSystem(string containerName, string rootUrl, string connecti
4161
this.FileSystem = AzureFileSystem.GetInstance(containerName, rootUrl, connectionString, maxDays);
4262
}
4363

64+
/// <summary>
65+
/// Initializes a new instance of the <see cref="AzureBlobFileSystem"/> class from values in web.config through ConfigurationManager.
66+
/// </summary>
67+
public AzureBlobFileSystem()
68+
{
69+
70+
string connectionString = ConfigurationManager.AppSettings[ConnectionStringKey] as string;
71+
if (!string.IsNullOrWhiteSpace(connectionString))
72+
{
73+
string rootUrl = ConfigurationManager.AppSettings[RootUrlKey] as string;
74+
if (string.IsNullOrWhiteSpace(rootUrl))
75+
{
76+
throw new InvalidOperationException("Azure Storage Root URL is not defined in web.config. The " + RootUrlKey + " property was not defined or is empty.");
77+
}
78+
79+
string containerName = ConfigurationManager.AppSettings[ContainerNameKey] as string;
80+
81+
if (string.IsNullOrWhiteSpace(containerName))
82+
{
83+
containerName = "media";
84+
}
85+
86+
string maxDays = ConfigurationManager.AppSettings[MaxDaysKey] as string;
87+
88+
if (string.IsNullOrWhiteSpace(maxDays))
89+
{
90+
maxDays = "365";
91+
}
92+
93+
this.FileSystem = AzureFileSystem.GetInstance(containerName, rootUrl, connectionString, maxDays);
94+
}
95+
else
96+
{
97+
throw new InvalidOperationException("Unable to retreive the Azure Storage configuration from web.config. " + ConnectionStringKey + " was not defined or is empty.");
98+
}
99+
100+
101+
}
102+
44103
/// <summary>
45104
/// Gets a singleton instance of the <see cref="AzureFileSystem"/> class.
46105
/// </summary>

src/UmbracoFileSystemProviders.Azure/Constants.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ public static class Configuration
2929
/// The configuration key for enabling the storage emulator.
3030
/// </summary>
3131
public const string UseStorageEmulatorKey = "AzureBlobFileSystem.UseStorageEmulator";
32+
33+
/// <summary>
34+
/// The configuration key for providing the connection string via the web.config
35+
/// </summary>
36+
public const string ConnectionStringKey = "AzureBlobFileSystem.ConnectionString";
37+
38+
/// <summary>
39+
/// The configuration key for providing the Azure Blob Container Name via the web.config
40+
/// </summary>
41+
public const string ContainerNameKey = "AzureBlobFileSystem.ContainerName";
42+
43+
/// <summary>
44+
/// The configuration key for providing the Storage Root URL via the web.config
45+
/// </summary>
46+
public const string RootUrlKey = "AzureBlobFileSystem.RootUrl";
47+
48+
/// <summary>
49+
/// The configuration key for providing the Maximum Days Cache value via the web.config
50+
/// </summary>
51+
public const string MaxDaysKey = "AzureBlobFileSystem.MaxDays";
3252
}
3353
}
3454
}

0 commit comments

Comments
 (0)