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-01 19:37:30 +02:00
|
|
|
|
public async Task Handle(OpenDatabaseQuery request)
|
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-06 20:20:45 +02:00
|
|
|
|
var file = await _file.OpenBinaryFile(request.FilePath);
|
|
|
|
|
await _database.Open(file, new Credentials
|
2020-03-26 19:04:51 +01:00
|
|
|
|
{
|
2020-04-07 12:48:18 +02:00
|
|
|
|
KeyFileContents = !string.IsNullOrEmpty(request.KeyFilePath) ? await _file.OpenBinaryFile(request.KeyFilePath): null,
|
2020-03-26 19:04:51 +01:00
|
|
|
|
Password = request.Password
|
|
|
|
|
});
|
2020-04-06 20:20:45 +02:00
|
|
|
|
_database.FileAccessToken = request.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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|