2020-03-24 13:01:14 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-04-25 22:34:50 +02:00
|
|
|
|
using System.IO;
|
2020-03-24 13:01:14 +01:00
|
|
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Windows.Storage;
|
|
|
|
|
using Windows.Storage.AccessCache;
|
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.Infrastructure.UWP
|
|
|
|
|
{
|
|
|
|
|
public class StorageFileClient: IFileProxy
|
|
|
|
|
{
|
|
|
|
|
public async Task<byte[]> OpenBinaryFile(string path)
|
|
|
|
|
{
|
2020-04-24 16:16:48 +02:00
|
|
|
|
var file = await GetFile(path);
|
2020-04-09 19:43:06 +02:00
|
|
|
|
var result = await FileIO.ReadBufferAsync(file).AsTask();
|
2020-03-24 13:01:14 +01:00
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IList<string>> OpenTextFile(string path)
|
|
|
|
|
{
|
2020-04-24 16:16:48 +02:00
|
|
|
|
var file = await GetFile(path);
|
2020-04-09 19:43:06 +02:00
|
|
|
|
var result = await FileIO.ReadLinesAsync(file).AsTask();
|
2020-03-24 13:01:14 +01:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReleaseFile(string path)
|
|
|
|
|
{
|
|
|
|
|
StorageApplicationPermissions.FutureAccessList.Remove(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task WriteBinaryContentsToFile(string path, byte[] contents)
|
|
|
|
|
{
|
2020-04-24 16:16:48 +02:00
|
|
|
|
var file = await GetFile(path);
|
2020-04-09 19:43:06 +02:00
|
|
|
|
await FileIO.WriteBytesAsync(file, contents).AsTask();
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
2020-04-24 16:16:48 +02:00
|
|
|
|
|
|
|
|
|
private async Task<StorageFile> GetFile(string token)
|
|
|
|
|
{
|
|
|
|
|
if (StorageApplicationPermissions.MostRecentlyUsedList.ContainsItem(token))
|
2020-04-25 22:34:50 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(token).AsTask();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
StorageApplicationPermissions.MostRecentlyUsedList.Remove(token);
|
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 16:16:48 +02:00
|
|
|
|
if (StorageApplicationPermissions.FutureAccessList.ContainsItem(token))
|
|
|
|
|
return await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token).AsTask();
|
2020-04-25 22:34:50 +02:00
|
|
|
|
throw new FileNotFoundException();
|
2020-04-24 16:16:48 +02:00
|
|
|
|
}
|
2020-03-24 13:01:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|