Files
modernkeepass/ModernKeePass.Application/Database/Queries/OpenDatabase/OpenDatabaseQuery.cs
Geoffroy BONNEVILLE eacb3b182e Database files are added to the mru instead of the fal
Simplify opening database workflow
Corrected opening from recent bug
2020-04-24 13:58:30 +02:00

45 lines
1.7 KiB
C#

using System.Threading.Tasks;
using MediatR;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.Dtos;
using ModernKeePass.Domain.Exceptions;
namespace ModernKeePass.Application.Database.Queries.OpenDatabase
{
public class OpenDatabaseQuery: IRequest
{
public string FilePath { get; set; }
public string Password { get; set; }
public string KeyFilePath { get; set; }
public class OpenDatabaseQueryHandler : IAsyncRequestHandler<OpenDatabaseQuery>
{
private readonly IDatabaseProxy _database;
private readonly IFileProxy _file;
private readonly IRecentProxy _recent;
public OpenDatabaseQueryHandler(IDatabaseProxy database, IFileProxy file, IRecentProxy recent)
{
_database = database;
_file = file;
_recent = recent;
}
public async Task Handle(OpenDatabaseQuery message)
{
if (_database.IsDirty) throw new DatabaseOpenException();
var file = await _recent.Get(message.FilePath);
var hasKeyFile = !string.IsNullOrEmpty(message.KeyFilePath);
await _database.Open(file, new Credentials
{
KeyFileContents = hasKeyFile ? await _file.OpenBinaryFile(message.KeyFilePath): null,
Password = message.Password
});
if (hasKeyFile) _file.ReleaseFile(message.KeyFilePath);
_database.Size = file.Length;
_database.FileAccessToken = message.FilePath;
}
}
}
}