feat(10-01): create logo models, BrandingRepository, and repository tests
- Add LogoData record with Base64 and MimeType init properties - Add BrandingSettings class with nullable MspLogo property - Extend TenantProfile with nullable ClientLogo property (additive) - Add BrandingRepository mirroring SettingsRepository pattern (write-then-replace) - Add BrandingRepositoryTests: 5 tests covering load defaults, round-trip, dir creation, and TenantProfile serialization
This commit is contained in:
6
SharepointToolbox/Core/Models/BrandingSettings.cs
Normal file
6
SharepointToolbox/Core/Models/BrandingSettings.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace SharepointToolbox.Core.Models;
|
||||
|
||||
public class BrandingSettings
|
||||
{
|
||||
public LogoData? MspLogo { get; set; }
|
||||
}
|
||||
7
SharepointToolbox/Core/Models/LogoData.cs
Normal file
7
SharepointToolbox/Core/Models/LogoData.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SharepointToolbox.Core.Models;
|
||||
|
||||
public record LogoData
|
||||
{
|
||||
public string Base64 { get; init; } = string.Empty;
|
||||
public string MimeType { get; init; } = string.Empty;
|
||||
}
|
||||
@@ -5,4 +5,5 @@ public class TenantProfile
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string TenantUrl { get; set; } = string.Empty;
|
||||
public string ClientId { get; set; } = string.Empty;
|
||||
public LogoData? ClientLogo { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using SharepointToolbox.Core.Models;
|
||||
|
||||
namespace SharepointToolbox.Infrastructure.Persistence;
|
||||
|
||||
public class BrandingRepository
|
||||
{
|
||||
private readonly string _filePath;
|
||||
private readonly SemaphoreSlim _writeLock = new(1, 1);
|
||||
|
||||
public BrandingRepository(string filePath)
|
||||
{
|
||||
_filePath = filePath;
|
||||
}
|
||||
|
||||
public async Task<BrandingSettings> LoadAsync()
|
||||
{
|
||||
if (!File.Exists(_filePath))
|
||||
return new BrandingSettings();
|
||||
|
||||
string json;
|
||||
try
|
||||
{
|
||||
json = await File.ReadAllTextAsync(_filePath, Encoding.UTF8);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new InvalidDataException($"Failed to read branding file: {_filePath}", ex);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var settings = JsonSerializer.Deserialize<BrandingSettings>(json,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
return settings ?? new BrandingSettings();
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new InvalidDataException($"Branding file contains invalid JSON: {_filePath}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveAsync(BrandingSettings settings)
|
||||
{
|
||||
await _writeLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(settings,
|
||||
new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
|
||||
var tmpPath = _filePath + ".tmp";
|
||||
var dir = Path.GetDirectoryName(_filePath);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
Directory.CreateDirectory(dir);
|
||||
|
||||
await File.WriteAllTextAsync(tmpPath, json, Encoding.UTF8);
|
||||
|
||||
// Validate round-trip before replacing
|
||||
JsonDocument.Parse(await File.ReadAllTextAsync(tmpPath, Encoding.UTF8)).Dispose();
|
||||
|
||||
File.Move(tmpPath, _filePath, overwrite: true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_writeLock.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user