mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 08:11:10 +00:00
Falls back to the unstyled application views when a theme template is missing
This commit is contained in:
1 parent
f4b90a5aea
commit
3192b9c585
11 files changed
+366
-301
No files matched your search
@@ -147,41 +147,54 @@ internal sealed class ThemeDetectionSyncJob : BackgroundService
|
||||
|
||||
foreach (ThemeEntity brokenTheme in brokenThemes)
|
||||
{
|
||||
if (brokenTheme.InstallSource == ThemeInstallSource.Bundled)
|
||||
try
|
||||
{
|
||||
// the files of a bundled theme that was not deleted by the user are restored from its shipped archive
|
||||
Result<Success> restoreResult = await _themeService.RestoreBundledThemeAsync(brokenTheme.ThemeId, cancellationToken);
|
||||
if (restoreResult.IsSuccess)
|
||||
continue;
|
||||
|
||||
_logger.LogWarning("Failed to restore the files of bundled theme '{ThemeId}': {Error}", brokenTheme.ThemeId, restoreResult.FirstError.Description);
|
||||
|
||||
// at least one bundled theme must always remain available, so the application never ends up without any theme;
|
||||
// the last remaining bundled theme is kept even when its files cannot be recovered
|
||||
int availableBundledThemes = themes.Count(availableTheme => !availableTheme.IsDeleted && availableTheme.InstallSource == ThemeInstallSource.Bundled);
|
||||
if (availableBundledThemes <= 1)
|
||||
if (brokenTheme.InstallSource == ThemeInstallSource.Bundled)
|
||||
{
|
||||
_logger.LogWarning("Bundled theme '{ThemeId}' is the last remaining bundled theme and its files could not be restored, so it is kept.", brokenTheme.ThemeId);
|
||||
continue;
|
||||
}
|
||||
// the files of a bundled theme that was not deleted by the user are restored from its shipped archive
|
||||
Result<Success> restoreResult = await _themeService.RestoreBundledThemeAsync(brokenTheme.ThemeId, cancellationToken);
|
||||
if (restoreResult.IsSuccess)
|
||||
continue;
|
||||
|
||||
// a bundled theme whose files could not be recovered is soft deleted, so it is not shown anymore, but can be restored later
|
||||
brokenTheme.IsDeleted = true;
|
||||
brokenTheme.IsCurrent = null;
|
||||
brokenTheme.UpdatedOnUtc = DateTime.UtcNow;
|
||||
brokenTheme.UpdatedBy = default;
|
||||
Result<Updated> updateResult = await unitOfWork.ThemeRepository.UpdateAsync(brokenTheme, cancellationToken);
|
||||
if (updateResult.IsFailure)
|
||||
_logger.LogWarning("Failed to delete theme '{ThemeId}': {Error}", brokenTheme.ThemeId, updateResult.FirstError.Description);
|
||||
_logger.LogWarning("Failed to restore the files of bundled theme '{ThemeId}': {Error}", brokenTheme.ThemeId, restoreResult.FirstError.Description);
|
||||
|
||||
// at least one bundled theme must always remain available, so the application never ends up without any theme;
|
||||
// the last remaining bundled theme is kept even when its files cannot be recovered
|
||||
int availableBundledThemes = themes.Count(availableTheme => !availableTheme.IsDeleted && availableTheme.InstallSource == ThemeInstallSource.Bundled);
|
||||
if (availableBundledThemes <= 1)
|
||||
{
|
||||
_logger.LogWarning("Bundled theme '{ThemeId}' is the last remaining bundled theme and its files could not be restored, so it is kept.", brokenTheme.ThemeId);
|
||||
continue;
|
||||
}
|
||||
|
||||
// a bundled theme whose files could not be recovered is soft deleted, so it is not shown anymore, but can be restored later
|
||||
brokenTheme.IsDeleted = true;
|
||||
brokenTheme.IsCurrent = null;
|
||||
brokenTheme.UpdatedOnUtc = DateTime.UtcNow;
|
||||
brokenTheme.UpdatedBy = default;
|
||||
Result<Updated> updateResult = await unitOfWork.ThemeRepository.UpdateAsync(brokenTheme, cancellationToken);
|
||||
if (updateResult.IsFailure)
|
||||
_logger.LogWarning("Failed to delete theme '{ThemeId}': {Error}", brokenTheme.ThemeId, updateResult.FirstError.Description);
|
||||
}
|
||||
else
|
||||
{
|
||||
// a user theme has no shipped archive to restore from, so damage is permanent and the theme is removed entirely
|
||||
_logger.LogWarning("The stored files of user theme '{ThemeId}' are missing, so the theme is deleted.", brokenTheme.ThemeId);
|
||||
brokenTheme.IsCurrent = null;
|
||||
Result<Deleted> deleteResult = await unitOfWork.ThemeRepository.DeleteByIdAsync(brokenTheme.Id, cancellationToken);
|
||||
if (deleteResult.IsFailure)
|
||||
_logger.LogWarning("Failed to delete theme '{ThemeId}': {Error}", brokenTheme.ThemeId, deleteResult.FirstError.Description);
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// a user theme has no shipped archive to restore from, so damage is permanent and the theme is removed entirely
|
||||
_logger.LogWarning("The stored files of user theme '{ThemeId}' are missing, so the theme is deleted.", brokenTheme.ThemeId);
|
||||
brokenTheme.IsCurrent = null;
|
||||
Result<Deleted> deleteResult = await unitOfWork.ThemeRepository.DeleteByIdAsync(brokenTheme.Id, cancellationToken);
|
||||
if (deleteResult.IsFailure)
|
||||
_logger.LogWarning("Failed to delete theme '{ThemeId}': {Error}", brokenTheme.ThemeId, deleteResult.FirstError.Description);
|
||||
throw;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
// repairing a broken theme is best effort: an unexpected failure on one theme must not crash the host,
|
||||
// so it is logged and the remaining broken themes are still processed
|
||||
_logger.LogWarning(exception, "Failed to repair the broken theme '{ThemeId}'.", brokenTheme.ThemeId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,15 +158,49 @@ public sealed class ThemeService : IThemeService
|
||||
_logger.LogWarning(exception, "An uploaded theme archive could not be read.");
|
||||
return Errors.Themes.ThemeArchiveNotReadable;
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
// the extracted pack could not be moved into place; the install is best effort, so the caller receives an error
|
||||
// instead of an unhandled exception that would crash the host or fail the request
|
||||
_logger.LogWarning(exception, "The theme pack could not be installed.");
|
||||
return Errors.Themes.ThemeFilesUnreadable;
|
||||
}
|
||||
catch (UnauthorizedAccessException exception)
|
||||
{
|
||||
_logger.LogWarning(exception, "The theme pack could not be installed.");
|
||||
return Errors.Themes.ThemeFilesUnreadable;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(temporaryArchivePath))
|
||||
File.Delete(temporaryArchivePath);
|
||||
try
|
||||
{
|
||||
if (File.Exists(temporaryArchivePath))
|
||||
File.Delete(temporaryArchivePath);
|
||||
|
||||
if (Directory.Exists(extractionPath))
|
||||
Directory.Delete(extractionPath, recursive: true);
|
||||
if (Directory.Exists(extractionPath))
|
||||
Directory.Delete(extractionPath, recursive: true);
|
||||
}
|
||||
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
// a locked staging file must not turn the install into an unhandled exception; the stale staging
|
||||
// directory is removed on the next install attempt
|
||||
_logger.LogTrace(exception, "Failed to clean up the theme staging directory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
// a file system failure before or during the install (for example a locked storage or staging directory)
|
||||
// must not escape as an unhandled exception, which would crash the host or fail the request; the install
|
||||
// is best effort and returns an error instead
|
||||
_logger.LogWarning(exception, "The theme pack could not be installed.");
|
||||
return Errors.Themes.ThemeFilesUnreadable;
|
||||
}
|
||||
catch (UnauthorizedAccessException exception)
|
||||
{
|
||||
_logger.LogWarning(exception, "The theme pack could not be installed.");
|
||||
return Errors.Themes.ThemeFilesUnreadable;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_mutationGate.Release();
|
||||
@@ -242,7 +276,7 @@ public sealed class ThemeService : IThemeService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the sanitized content of the template selected by a page key, falling back to the default template when the key is missing.
|
||||
/// Gets the sanitized content of the template selected by a page key, returning an error when the theme provides no template for the page.
|
||||
/// </summary>
|
||||
/// <param name="themeId">The manifest id of the theme.</param>
|
||||
/// <param name="pageKey">The page key that selects the template.</param>
|
||||
@@ -259,7 +293,10 @@ public sealed class ThemeService : IThemeService
|
||||
string? templatePath = manifest.Templates.TryGetValue(pageKey, out string? explicitPath)
|
||||
? explicitPath
|
||||
: ResolveMirroredTemplatePath(themePath, pageKey);
|
||||
templatePath ??= manifest.Templates["default"]; // the manifest validation guarantees a default template
|
||||
|
||||
// a template is used for a page only when the theme provides one explicitly or as a mirrored file
|
||||
if (templatePath is null)
|
||||
return Errors.Themes.ThemeTemplateNotFound;
|
||||
|
||||
Result<string> fullPathResult = ResolveContainedPath(themePath, templatePath);
|
||||
if (fullPathResult.IsFailure)
|
||||
@@ -607,8 +644,8 @@ public sealed class ThemeService : IThemeService
|
||||
if (!s_versionPattern.IsMatch(manifest.Version))
|
||||
return PackageInvalid("Theme version must use semantic version form, for example 1.0.0.");
|
||||
|
||||
if (manifest.Templates is null || manifest.Templates.Count == 0 || manifest.Templates.Count > 32)
|
||||
return PackageInvalid("A theme must declare between 1 and 32 templates.");
|
||||
if (manifest.Templates is null || manifest.Templates.Count > 32)
|
||||
return PackageInvalid("A theme must declare at most 32 templates.");
|
||||
|
||||
Dictionary<string, string> normalizedTemplates = new(StringComparer.OrdinalIgnoreCase);
|
||||
foreach ((string key, string path) in manifest.Templates)
|
||||
@@ -634,9 +671,6 @@ public sealed class ThemeService : IThemeService
|
||||
normalizedTemplates[key] = normalizedPath;
|
||||
}
|
||||
|
||||
if (!normalizedTemplates.ContainsKey("default"))
|
||||
return PackageInvalid("The manifest must define a 'default' template fallback.");
|
||||
|
||||
manifest.Templates = normalizedTemplates;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(manifest.Preview))
|
||||
@@ -796,7 +830,7 @@ public sealed class ThemeService : IThemeService
|
||||
else
|
||||
File.Delete(path);
|
||||
}
|
||||
catch (IOException exception)
|
||||
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
_logger.LogDebug(exception, "Could not remove stale theme staging path {StagingPath}.", path);
|
||||
}
|
||||
@@ -829,7 +863,7 @@ public sealed class ThemeService : IThemeService
|
||||
Directory.Move(extractionPath, destination);
|
||||
return;
|
||||
}
|
||||
catch (IOException) when (attempt < MAX_ATTEMPTS)
|
||||
catch (Exception exception) when (attempt < MAX_ATTEMPTS && exception is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
await Task.Delay(100 * attempt, cancellationToken);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user