106 lines
3.4 KiB
C#
106 lines
3.4 KiB
C#
using System.Linq;
|
|
using SQLite;
|
|
|
|
namespace ParadoxSaveParser.WebAPI.Database;
|
|
|
|
public class DatabaseConnector
|
|
{
|
|
private readonly int _uploadsLifetimeDays;
|
|
private readonly SQLiteAsyncConnection _db;
|
|
private readonly ContextLogger _logger;
|
|
|
|
public DatabaseConnector(string databasePath, int uploadsLifetimeDays, ILogger logger)
|
|
{
|
|
_uploadsLifetimeDays = uploadsLifetimeDays;
|
|
_db = new SQLiteAsyncConnection(databasePath);
|
|
_logger = new ContextLogger("Database", logger);
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
await _db.CreateTableAsync<SaveFileMetadata>();
|
|
}
|
|
|
|
public async Task<SaveFileMetadata?> GetMetadata(string id)
|
|
{
|
|
return (await _db.QueryAsync<SaveFileMetadata>(
|
|
"select * from SaveFileMetadata where id = ?", id))
|
|
.FirstOrDefault();
|
|
}
|
|
|
|
public async Task<SaveFileMetadata> CreateMetadata(Game game)
|
|
{
|
|
var meta = new SaveFileMetadata
|
|
{
|
|
id = Guid.NewGuid().ToString(),
|
|
game = game,
|
|
status = SaveFileProcessingStatus.Initialized,
|
|
uploadDateTime = DateTime.Now
|
|
};
|
|
await _db.InsertAsync(meta);
|
|
return meta;
|
|
}
|
|
|
|
public async Task UpdateMetadataStatus(SaveFileMetadata meta, SaveFileProcessingStatus status)
|
|
{
|
|
meta.status = status;
|
|
await _db.UpdateAsync(meta);
|
|
}
|
|
|
|
public async Task SetMetadataError(SaveFileMetadata meta, string errorMessage)
|
|
{
|
|
meta.errorMessage = errorMessage;
|
|
await UpdateMetadataStatus(meta, SaveFileProcessingStatus.Error);
|
|
TryDeleteAssociatedFiles(meta);
|
|
}
|
|
|
|
public async Task DeleteMetadata(SaveFileMetadata meta, string reason)
|
|
{
|
|
_logger.LogDebug($"Deleting save file (id: {meta.id} reason: {reason}" +
|
|
$"uploadDate: {meta.uploadDateTime:yyyy/MM/dd})");
|
|
await _db.DeleteAsync(meta);
|
|
TryDeleteAssociatedFiles(meta);
|
|
}
|
|
|
|
/// <summary>
|
|
/// WARNING: Call this method only when no parsing operations are running.
|
|
/// </summary>
|
|
public async Task DeleteInvalidData()
|
|
{
|
|
_logger.LogInfo("Deleting invalid data...");
|
|
|
|
var expirationDate = DateTime.Now.AddDays(-_uploadsLifetimeDays);
|
|
var metadataTable = _db.Table<SaveFileMetadata>();
|
|
var metadataList = await metadataTable.ToListAsync();
|
|
int deleteCount = 0;
|
|
foreach (var meta in metadataList)
|
|
{
|
|
string deletionReason;
|
|
if(meta.status != SaveFileProcessingStatus.Done)
|
|
deletionReason = $"invalid status ({meta.status})";
|
|
else if (meta.uploadDateTime < expirationDate)
|
|
deletionReason = "expired";
|
|
else if(!meta.AssociatedFilesExist())
|
|
deletionReason = "files not found";
|
|
else continue;
|
|
|
|
deleteCount++;
|
|
await DeleteMetadata(meta, deletionReason);
|
|
}
|
|
|
|
_logger.LogInfo($"Deleted {deleteCount} invalid records");
|
|
}
|
|
|
|
private void TryDeleteFile(IOPath file)
|
|
{
|
|
if (!File.Exists(file)) return;
|
|
_logger.LogDebug($"Deleting file '{file}'");
|
|
File.Delete(file);
|
|
}
|
|
|
|
private void TryDeleteAssociatedFiles(SaveFileMetadata meta)
|
|
{
|
|
TryDeleteFile(meta.GetSaveFilePath());
|
|
TryDeleteFile(meta.GetParsedDataPath());
|
|
}
|
|
} |