Files

43 lines
1.6 KiB
C#
Raw Permalink Normal View History

2020-04-01 19:37:30 +02:00
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
{
2020-04-01 19:37:30 +02:00
public class OpenDatabaseQuery: IRequest
{
public string FilePath { get; set; }
public string Password { get; set; }
public string KeyFilePath { get; set; }
2020-04-01 19:37:30 +02:00
public class OpenDatabaseQueryHandler : IAsyncRequestHandler<OpenDatabaseQuery>
{
private readonly IDatabaseProxy _database;
private readonly IFileProxy _file;
public OpenDatabaseQueryHandler(IDatabaseProxy database, IFileProxy file)
{
_database = database;
_file = file;
}
2020-04-08 15:27:40 +02:00
public async Task Handle(OpenDatabaseQuery message)
{
if (_database.IsDirty) throw new DatabaseOpenException();
var file = await _file.ReadBinaryFile(message.FilePath);
var hasKeyFile = !string.IsNullOrEmpty(message.KeyFilePath);
await _database.Open(file, new Credentials
{
KeyFileContents = hasKeyFile ? await _file.ReadBinaryFile(message.KeyFilePath): null,
2020-04-08 15:27:40 +02:00
Password = message.Password
});
if (hasKeyFile) _file.ReleaseFile(message.KeyFilePath);
_database.Size = file.Length;
2020-04-08 15:27:40 +02:00
_database.FileAccessToken = message.FilePath;
}
}
}
}