Cleanup code

This commit is contained in:
Geoffroy BONNEVILLE
2020-04-08 15:27:40 +02:00
parent 4863eb9fae
commit 009382ea03
27 changed files with 142 additions and 138 deletions

View File

@@ -1,4 +1,5 @@
using MediatR;
using System;
using MediatR;
using System.Threading.Tasks;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.Exceptions;
@@ -24,22 +25,34 @@ namespace ModernKeePass.Application.Database.Commands.SaveDatabase
{
if (!_database.IsOpen) throw new DatabaseClosedException();
byte[] contents;
if (string.IsNullOrEmpty(message.FilePath))
try
{
contents = await _database.SaveDatabase();
await _file.WriteBinaryContentsToFile(_database.FileAccessToken, contents);
byte[] contents;
if (string.IsNullOrEmpty(message.FilePath))
{
contents = await _database.SaveDatabase();
// Test DB integrity before writing changes to file
_database.CloseDatabase();
var file = await _file.OpenBinaryFile(_database.FileAccessToken);
await _database.ReOpen(file);
await _file.WriteBinaryContentsToFile(_database.FileAccessToken, contents);
}
else
{
var newFileContents = await _file.OpenBinaryFile(message.FilePath);
contents = await _database.SaveDatabase(newFileContents);
await _file.WriteBinaryContentsToFile(message.FilePath, contents);
_file.ReleaseFile(_database.FileAccessToken);
_database.FileAccessToken = message.FilePath;
}
}
else
catch (Exception exception)
{
var newFileContents = await _file.OpenBinaryFile(message.FilePath);
contents = await _database.SaveDatabase(newFileContents);
await _file.WriteBinaryContentsToFile(message.FilePath, contents);
_file.ReleaseFile(_database.FileAccessToken);
_database.FileAccessToken = message.FilePath;
throw new SaveException(exception);
}
}
}
}