void.cat/VoidCat/Services/Migrations/MetadataMigrator.cs

67 lines
2.2 KiB
C#
Raw Normal View History

2022-02-17 22:10:37 +00:00
using Newtonsoft.Json;
using VoidCat.Model;
namespace VoidCat.Services.Migrations;
public abstract class MetadataMigrator<TOld, TNew> : IMigration
{
private readonly VoidSettings _settings;
private readonly ILogger<MetadataMigrator<TOld, TNew>> _logger;
protected MetadataMigrator(VoidSettings settings, ILogger<MetadataMigrator<TOld, TNew>> logger)
{
_settings = settings;
_logger = logger;
}
2022-05-19 22:27:49 +00:00
2022-02-17 22:10:37 +00:00
public async ValueTask Migrate()
{
var newMeta = Path.Combine(_settings.DataDirectory, OldPath);
if (!Directory.Exists(newMeta))
{
Directory.CreateDirectory(newMeta);
}
2022-05-19 22:27:49 +00:00
2022-02-17 22:10:37 +00:00
foreach (var fe in Directory.EnumerateFiles(_settings.DataDirectory))
{
var filename = Path.GetFileNameWithoutExtension(fe);
if (!Guid.TryParse(filename, out var id)) continue;
var fp = MapOldMeta(id);
if (File.Exists(fp))
{
_logger.LogInformation("Migrating metadata for {file}", fp);
try
{
var oldJson = await File.ReadAllTextAsync(fp);
if (!ShouldMigrate(oldJson)) continue;
2022-05-19 22:27:49 +00:00
2022-02-17 22:10:37 +00:00
var old = JsonConvert.DeserializeObject<TOld>(oldJson);
2022-05-19 22:27:49 +00:00
if (old == null) continue;
2022-02-17 22:10:37 +00:00
var newObj = MigrateModel(old);
await File.WriteAllTextAsync(MapNewMeta(id), JsonConvert.SerializeObject(newObj));
2022-05-19 22:27:49 +00:00
2022-02-17 22:10:37 +00:00
// delete old metadata
File.Delete(fp);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error migrating metadata: {Message}", ex.Message);
}
}
}
}
protected abstract string OldPath { get; }
protected abstract string NewPath { get; }
protected abstract bool ShouldMigrate(string json);
protected abstract TNew MigrateModel(TOld old);
2022-05-19 22:27:49 +00:00
2022-02-17 22:10:37 +00:00
private string MapOldMeta(Guid id) =>
Path.ChangeExtension(Path.Join(_settings.DataDirectory, OldPath, id.ToString()), ".json");
2022-05-19 22:27:49 +00:00
2022-02-17 22:10:37 +00:00
private string MapNewMeta(Guid id) =>
Path.ChangeExtension(Path.Join(_settings.DataDirectory, NewPath, id.ToString()), ".json");
}