Files
Wino-Mail/Wino.Mail.WinUI/Services/ReleaseLocalAccountDataCleanupService.cs
T

119 lines
4.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2026-04-25 16:12:49 +02:00
using System.Linq;
using System.Threading.Tasks;
using Serilog;
using Wino.Core.Domain.Interfaces;
namespace Wino.Mail.WinUI.Services;
public sealed class ReleaseLocalAccountDataCleanupService
{
private const string CleanupCompletedSettingKey = "ReleaseLocalAccountDataCleanup_v1_Completed";
private const string LegacyDatabaseFileName = "Wino180.db";
private readonly IConfigurationService _configurationService;
private readonly IApplicationConfiguration _applicationConfiguration;
2026-04-25 16:12:49 +02:00
private readonly INotificationBuilder _notificationBuilder;
private readonly ILogger _logger = Log.ForContext<ReleaseLocalAccountDataCleanupService>();
public ReleaseLocalAccountDataCleanupService(IConfigurationService configurationService,
2026-04-25 16:12:49 +02:00
IApplicationConfiguration applicationConfiguration,
INotificationBuilder notificationBuilder)
{
_configurationService = configurationService;
_applicationConfiguration = applicationConfiguration;
2026-04-25 16:12:49 +02:00
_notificationBuilder = notificationBuilder;
}
public async Task RunIfNeededAsync()
{
if (_configurationService.Get(CleanupCompletedSettingKey, false))
return;
var localFolderPath = _applicationConfiguration.ApplicationDataFolderPath;
var publisherPath = _applicationConfiguration.PublisherSharedFolderPath;
if (string.IsNullOrWhiteSpace(localFolderPath) || !Directory.Exists(localFolderPath))
{
_configurationService.Set(CleanupCompletedSettingKey, true);
return;
}
var cleanupTargets = new List<string>
{
Path.Combine(localFolderPath, "Mime"),
Path.Combine(localFolderPath, "contacts"),
Path.Combine(localFolderPath, "CalendarAttachments"),
Path.Combine(publisherPath, LegacyDatabaseFileName)
};
2026-04-25 16:12:49 +02:00
var hadLegacyData = false;
foreach (var targetPath in cleanupTargets)
{
2026-04-25 16:12:49 +02:00
hadLegacyData |= await DeletePathIfExistsAsync(targetPath, localFolderPath, publisherPath).ConfigureAwait(false);
}
_configurationService.Set(CleanupCompletedSettingKey, true);
2026-04-25 16:12:49 +02:00
if (hadLegacyData)
{
_notificationBuilder.CreateReleaseMigrationNotification();
}
_logger.Information("Completed one-time local account data cleanup for release migration.");
}
2026-04-25 16:12:49 +02:00
private async Task<bool> DeletePathIfExistsAsync(string targetPath, params string[] allowedRootPaths)
{
try
{
var fullTargetPath = Path.GetFullPath(targetPath);
2026-04-25 16:12:49 +02:00
if (!allowedRootPaths.Any(rootPath => IsPathUnderAllowedRoot(fullTargetPath, rootPath)))
{
2026-04-25 16:12:49 +02:00
_logger.Warning("Skipped startup cleanup for path outside allowed roots: {TargetPath}", fullTargetPath);
return false;
}
2026-04-25 16:12:49 +02:00
var targetExists = Directory.Exists(fullTargetPath) || File.Exists(fullTargetPath);
if (Directory.Exists(fullTargetPath))
{
await Task.Run(() => Directory.Delete(fullTargetPath, recursive: true)).ConfigureAwait(false);
_logger.Information("Deleted legacy startup cleanup directory {TargetPath}", fullTargetPath);
2026-04-25 16:12:49 +02:00
return true;
}
if (File.Exists(fullTargetPath))
{
File.Delete(fullTargetPath);
_logger.Information("Deleted legacy startup cleanup file {TargetPath}", fullTargetPath);
2026-04-25 16:12:49 +02:00
return true;
}
2026-04-25 16:12:49 +02:00
return targetExists;
}
catch (Exception ex)
{
_logger.Warning(ex, "Failed to delete legacy startup cleanup path {TargetPath}", targetPath);
}
2026-04-25 16:12:49 +02:00
return false;
}
private static bool IsPathUnderAllowedRoot(string fullTargetPath, string rootPath)
{
if (string.IsNullOrWhiteSpace(rootPath))
return false;
var fullRootPath = Path.GetFullPath(rootPath);
var relativePath = Path.GetRelativePath(fullRootPath, fullTargetPath);
return relativePath != "." &&
!relativePath.StartsWith("..", StringComparison.Ordinal) &&
!Path.IsPathRooted(relativePath);
}
}