-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.cs
159 lines (137 loc) · 5.25 KB
/
Storage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PCLStorage;
using StoragePCL.Enums;
namespace StoragePCL
{
public class Storage
{
private List<Item> Items;
private string AppDirectory;
private IFolder rootFolder;
public Storage(IFolder root)
{
rootFolder = root;
//AppDirectory = dir;
Items = new List<Item>();
// Open and parse the file
// Load into Items or set it to a new.
}
public async Task LoadAsync()
{
Items = await Parser.ParseFileAsync(rootFolder) ?? new List<Item>();
}
// Add creates a new key
public async Task<EnumStorageStatusCode> InsertAsync(string key, string value, bool encryptValue = false)
{
if (string.IsNullOrWhiteSpace(key)) return EnumStorageStatusCode.KeyIsNull;
if (value == null) return EnumStorageStatusCode.ValueIsNull;
key = key.ToLower();
if (encryptValue)
{
// Encrypt the value here
value = Utilities.Security.Encrypt(value);
}
Items.Add(new Item(key, value, encryptValue));
// Save the file here
await Builder.BuildAndWriteFileAsync(Items, rootFolder);
return EnumStorageStatusCode.Success;
}
public async Task<EnumStorageStatusCode> UpdateAsync(string key, string value, bool isEncrypted = false)
{
if (string.IsNullOrWhiteSpace(key)) return EnumStorageStatusCode.KeyIsNull;
if (value == null) value = "";
key = key.ToLower();
var index = Items.FindIndex(x => x.Key == key);
if (index == -1) return EnumStorageStatusCode.KeyNotFound;
//var item = Items.Find(x => x.Key == key);
//if(item == null) return EnumStorageStatusCode.KeyNotFound;
if (isEncrypted)
{
Items[index].IsEncrypted = true;
// ENCRYPT THE VALUE
value = Utilities.Security.Encrypt(value);
}
Items[index].Value = value;
// save the file (async)
await Builder.BuildAndWriteFileAsync(Items, rootFolder);
return EnumStorageStatusCode.Success;
}
public async Task<EnumStorageStatusCode> InsertOrUpdateAsync(string key, string value, bool isEncrypted = false)
{
if (string.IsNullOrWhiteSpace(key)) return EnumStorageStatusCode.KeyIsNull;
if (value == null) value = "";
key = key.ToLower();
var index = Items.FindIndex(x => x.Key == key);
if (index == -1)
{
return await InsertAsync(key,value,isEncrypted);
}
//var item = Items.Find(x => x.Key == key);
//if(item == null) return EnumStorageStatusCode.KeyNotFound;
if (isEncrypted)
{
Items[index].IsEncrypted = true;
// ENCRYPT THE VALUE
value = Utilities.Security.Encrypt(value);
}
Items[index].Value = value;
// save the file (async)
await Builder.BuildAndWriteFileAsync(Items, rootFolder);
return EnumStorageStatusCode.Success;
}
public string Get(string key, out EnumStorageStatusCode statusCode)
{
if (string.IsNullOrWhiteSpace(key))
{
statusCode = EnumStorageStatusCode.KeyIsNull;
return null;
}
key = key.ToLower();
var index = Items.FindIndex(x => x.Key == key);
if (index == -1)
{
statusCode = EnumStorageStatusCode.KeyNotFound;
return null;
}
if (Items[index].IsEncrypted)
{
// decrypt the value
var decryptedValue = Utilities.Security.Decrypt(Items[index].Value);
statusCode = EnumStorageStatusCode.Success;
return decryptedValue;
}
else
{
statusCode = EnumStorageStatusCode.Success;
return Items[index].Value;
}
}
public string Get(string key)
{
EnumStorageStatusCode code;
var result = Get(key, out code);
return result;
}
public EnumStorageStatusCode Delete(string key)
{
if (string.IsNullOrWhiteSpace(key))
{
return EnumStorageStatusCode.KeyIsNull;
}
key = key.ToLower();
var index = Items.FindIndex(x => x.Key == key);
if (index == -1)
{
return EnumStorageStatusCode.KeyNotFound;
}
Items.RemoveAt(index);
// save the file (async)
Builder.BuildAndWriteFileAsync(Items, rootFolder).RunSynchronously();
return EnumStorageStatusCode.Success;
}
}
}