mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 15:40:18 -04:00
Cleanup code
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user