2020-04-01 19:37:30 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
|
|
|
|
using ModernKeePass.Domain.Dtos;
|
|
|
|
|
using ModernKeePass.Domain.Exceptions;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Application.Database.Queries.OpenDatabase
|
|
|
|
|
{
|
2020-04-01 19:37:30 +02:00
|
|
|
|
public class OpenDatabaseQuery: IRequest
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-03-26 19:04:51 +01:00
|
|
|
|
public string FilePath { get; set; }
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
public string KeyFilePath { get; set; }
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-01 19:37:30 +02:00
|
|
|
|
public class OpenDatabaseQueryHandler : IAsyncRequestHandler<OpenDatabaseQuery>
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-03-26 19:04:51 +01:00
|
|
|
|
private readonly IDatabaseProxy _database;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
private readonly IFileProxy _file;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-06 20:20:45 +02:00
|
|
|
|
public OpenDatabaseQueryHandler(IDatabaseProxy database, IFileProxy file)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-03-26 19:04:51 +01:00
|
|
|
|
_database = database;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_file = file;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 15:27:40 +02:00
|
|
|
|
public async Task Handle(OpenDatabaseQuery message)
|
2020-03-24 13:01:14 +01:00
|
|
|
|
{
|
2020-03-26 19:04:51 +01:00
|
|
|
|
if (_database.IsOpen) throw new DatabaseOpenException();
|
2020-03-24 13:01:14 +01:00
|
|
|
|
|
2020-04-08 15:27:40 +02:00
|
|
|
|
var file = await _file.OpenBinaryFile(message.FilePath);
|
2020-04-06 20:20:45 +02:00
|
|
|
|
await _database.Open(file, new Credentials
|
2020-03-26 19:04:51 +01:00
|
|
|
|
{
|
2020-04-08 15:27:40 +02:00
|
|
|
|
KeyFileContents = !string.IsNullOrEmpty(message.KeyFilePath) ? await _file.OpenBinaryFile(message.KeyFilePath): null,
|
|
|
|
|
Password = message.Password
|
2020-03-26 19:04:51 +01:00
|
|
|
|
});
|
2020-04-08 15:27:40 +02:00
|
|
|
|
_database.FileAccessToken = message.FilePath;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
2020-04-01 12:48:36 +02:00
|
|
|
|
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|