Skip to content

Commit

Permalink
Add OrchardCoreConstants (#13438)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco authored Mar 16, 2024
1 parent 2dbbfcd commit a2d529e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
14 changes: 14 additions & 0 deletions src/OrchardCore/OrchardCore.Abstractions/OrchardCoreConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace OrchardCore;

public class OrchardCoreConstants
{
public class Shell
{
public const string TenantsFileName = "tenants.json";
}

public class Configuration
{
public const string ApplicationSettingsFileName = "appsettings.json";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public async Task RemoveAsync(string tenant)
private async Task<bool> TryMigrateFromFileAsync(string tenant, JsonObject configurations)
{
var tenantFolder = Path.Combine(_container, tenant);
var appsettings = Path.Combine(tenantFolder, "appsettings.json");
var appsettings = Path.Combine(tenantFolder, OrchardCoreConstants.Configuration.ApplicationSettingsFileName);

if (!File.Exists(appsettings))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public DatabaseShellsSettingsSources(

_shellContextFactory = shellContextFactory;

_tenants = Path.Combine(shellOptions.Value.ShellsApplicationDataPath, "tenants.json");
_tenants = Path.Combine(shellOptions.Value.ShellsApplicationDataPath, OrchardCoreConstants.Shell.TenantsFileName);
}

public async Task AddSourcesAsync(IConfigurationBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public BlobShellConfigurationSources(

public async Task AddSourcesAsync(string tenant, IConfigurationBuilder builder)
{
var appSettings = IFileStoreExtensions.Combine(null, _container, tenant, "appsettings.json");
var appSettings = IFileStoreExtensions.Combine(null, _container, tenant, OrchardCoreConstants.Configuration.ApplicationSettingsFileName);
var fileInfo = await _shellsFileStore.GetFileInfoAsync(appSettings);

if (fileInfo == null && _blobOptions.MigrateFromFiles)
Expand All @@ -57,7 +57,7 @@ public async Task AddSourcesAsync(string tenant, IConfigurationBuilder builder)

public async Task SaveAsync(string tenant, IDictionary<string, string> data)
{
var appsettings = IFileStoreExtensions.Combine(null, _container, tenant, "appsettings.json");
var appsettings = IFileStoreExtensions.Combine(null, _container, tenant, OrchardCoreConstants.Configuration.ApplicationSettingsFileName);

var fileInfo = await _shellsFileStore.GetFileInfoAsync(appsettings);

Expand Down Expand Up @@ -94,7 +94,7 @@ public async Task SaveAsync(string tenant, IDictionary<string, string> data)

public async Task RemoveAsync(string tenant)
{
var appsettings = IFileStoreExtensions.Combine(null, _container, tenant, "appsettings.json");
var appsettings = IFileStoreExtensions.Combine(null, _container, tenant, OrchardCoreConstants.Configuration.ApplicationSettingsFileName);

var fileInfo = await _shellsFileStore.GetFileInfoAsync(appsettings);
if (fileInfo != null)
Expand All @@ -105,7 +105,7 @@ public async Task RemoveAsync(string tenant)

private async Task<bool> TryMigrateFromFileAsync(string tenant, string destFile)
{
var tenantFile = Path.Combine(_fileStoreContainer, tenant, "appsettings.json");
var tenantFile = Path.Combine(_fileStoreContainer, tenant, OrchardCoreConstants.Configuration.ApplicationSettingsFileName);
if (!File.Exists(tenantFile))
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace OrchardCore.Shells.Azure.Configuration
{
public class BlobShellsConfigurationSources : IShellsConfigurationSources
{
private static readonly string _appSettings =
Path.GetFileNameWithoutExtension(OrchardCoreConstants.Configuration.ApplicationSettingsFileName);

private readonly IShellsFileStore _shellsFileStore;
private readonly BlobShellStorageOptions _blobOptions;

Expand All @@ -27,28 +30,28 @@ IOptions<ShellOptions> shellOptions
_shellsFileStore = shellsFileStore;
_environment = hostingEnvironment.EnvironmentName;
_blobOptions = blobOptions;
_fileSystemAppSettings = Path.Combine(shellOptions.Value.ShellsApplicationDataPath, "appsettings");
_fileSystemAppSettings = Path.Combine(shellOptions.Value.ShellsApplicationDataPath, _appSettings);
}

public async Task AddSourcesAsync(IConfigurationBuilder builder)
{
var appSettingsFileInfo = await _shellsFileStore.GetFileInfoAsync("appsettings.json");
var appSettingsFileInfo = await _shellsFileStore.GetFileInfoAsync(OrchardCoreConstants.Configuration.ApplicationSettingsFileName);

if (appSettingsFileInfo == null && _blobOptions.MigrateFromFiles)
{
if (await TryMigrateFromFileAsync($"{_fileSystemAppSettings}.json", "appsettings.json"))
if (await TryMigrateFromFileAsync($"{_fileSystemAppSettings}.json", OrchardCoreConstants.Configuration.ApplicationSettingsFileName))
{
appSettingsFileInfo = await _shellsFileStore.GetFileInfoAsync("appsettings.json");
appSettingsFileInfo = await _shellsFileStore.GetFileInfoAsync(OrchardCoreConstants.Configuration.ApplicationSettingsFileName);
}
}

if (appSettingsFileInfo != null)
{
var stream = await _shellsFileStore.GetFileStreamAsync("appsettings.json");
var stream = await _shellsFileStore.GetFileStreamAsync(OrchardCoreConstants.Configuration.ApplicationSettingsFileName);
builder.AddTenantJsonStream(stream);
}

var environmentAppSettingsFileName = $"appsettings.{_environment}.json";
var environmentAppSettingsFileName = $"{_appSettings}.{_environment}.json";
var environmentAppSettingsFileInfo = await _shellsFileStore.GetFileInfoAsync(environmentAppSettingsFileName);
if (environmentAppSettingsFileInfo == null && _blobOptions.MigrateFromFiles)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace OrchardCore.Shells.Azure.Configuration
{
public class BlobShellsSettingsSources : IShellsSettingsSources
{
private const string TenantsBlobName = "tenants.json";

private readonly IShellsFileStore _shellsFileStore;
private readonly BlobShellStorageOptions _blobOptions;

Expand All @@ -27,17 +25,18 @@ public BlobShellsSettingsSources(IShellsFileStore shellsFileStore,
{
_shellsFileStore = shellsFileStore;
_blobOptions = blobOptions;
_tenantsFileSystemName = Path.Combine(shellOptions.Value.ShellsApplicationDataPath, TenantsBlobName);
_tenantsFileSystemName = Path.Combine(shellOptions.Value.ShellsApplicationDataPath, OrchardCoreConstants.Shell.TenantsFileName);
}

public async Task AddSourcesAsync(IConfigurationBuilder builder)
{
var fileInfo = await _shellsFileStore.GetFileInfoAsync(TenantsBlobName);
var fileInfo = await _shellsFileStore.GetFileInfoAsync(OrchardCoreConstants.Shell.TenantsFileName);

if (fileInfo == null && _blobOptions.MigrateFromFiles)
{
if (await TryMigrateFromFileAsync())
{
fileInfo = await _shellsFileStore.GetFileInfoAsync(TenantsBlobName);
fileInfo = await _shellsFileStore.GetFileInfoAsync(OrchardCoreConstants.Shell.TenantsFileName);
}
else
{
Expand All @@ -47,7 +46,7 @@ public async Task AddSourcesAsync(IConfigurationBuilder builder)

if (fileInfo != null)
{
var stream = await _shellsFileStore.GetFileStreamAsync(TenantsBlobName);
var stream = await _shellsFileStore.GetFileStreamAsync(OrchardCoreConstants.Shell.TenantsFileName);
builder.AddTenantJsonStream(stream);
}
}
Expand All @@ -58,11 +57,11 @@ public async Task SaveAsync(string tenant, IDictionary<string, string> data)
{
JsonObject tenantsSettings;

var fileInfo = await _shellsFileStore.GetFileInfoAsync(TenantsBlobName);
var fileInfo = await _shellsFileStore.GetFileInfoAsync(OrchardCoreConstants.Shell.TenantsFileName);

if (fileInfo != null)
{
using var stream = await _shellsFileStore.GetFileStreamAsync(TenantsBlobName);
using var stream = await _shellsFileStore.GetFileStreamAsync(OrchardCoreConstants.Shell.TenantsFileName);
tenantsSettings = await JObject.LoadAsync(stream);
}
else
Expand All @@ -88,17 +87,17 @@ public async Task SaveAsync(string tenant, IDictionary<string, string> data)
var tenantsSettingsString = tenantsSettings.ToJsonString(JOptions.Default);
using var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(tenantsSettingsString));

await _shellsFileStore.CreateFileFromStreamAsync(TenantsBlobName, memoryStream);
await _shellsFileStore.CreateFileFromStreamAsync(OrchardCoreConstants.Shell.TenantsFileName, memoryStream);
}

public async Task RemoveAsync(string tenant)
{
var fileInfo = await _shellsFileStore.GetFileInfoAsync(TenantsBlobName);
var fileInfo = await _shellsFileStore.GetFileInfoAsync(OrchardCoreConstants.Shell.TenantsFileName);

if (fileInfo != null)
{
JsonObject tenantsSettings;
using (var stream = await _shellsFileStore.GetFileStreamAsync(TenantsBlobName))
using (var stream = await _shellsFileStore.GetFileStreamAsync(OrchardCoreConstants.Shell.TenantsFileName))
{
tenantsSettings = await JObject.LoadAsync(stream);
}
Expand All @@ -108,7 +107,7 @@ public async Task RemoveAsync(string tenant)
var tenantsSettingsString = tenantsSettings.ToJsonString(JOptions.Default);
using var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(tenantsSettingsString));

await _shellsFileStore.CreateFileFromStreamAsync(TenantsBlobName, memoryStream);
await _shellsFileStore.CreateFileFromStreamAsync(OrchardCoreConstants.Shell.TenantsFileName, memoryStream);
}
}

Expand All @@ -120,7 +119,7 @@ private async Task<bool> TryMigrateFromFileAsync()
}

using var file = File.OpenRead(_tenantsFileSystemName);
await _shellsFileStore.CreateFileFromStreamAsync(TenantsBlobName, file);
await _shellsFileStore.CreateFileFromStreamAsync(OrchardCoreConstants.Shell.TenantsFileName, file);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ShellsSettingsSources : IShellsSettingsSources

public ShellsSettingsSources(IOptions<ShellOptions> shellOptions)
{
_tenants = Path.Combine(shellOptions.Value.ShellsApplicationDataPath, "tenants.json");
_tenants = Path.Combine(shellOptions.Value.ShellsApplicationDataPath, OrchardCoreConstants.Shell.TenantsFileName);
}

public Task AddSourcesAsync(IConfigurationBuilder builder)
Expand Down

0 comments on commit a2d529e

Please sign in to comment.