forked from mchidk/BinaryRage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.cs
108 lines (86 loc) · 2.47 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
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
namespace BinaryRage
{
internal static class Storage
{
private const string DB_EXTENTION = ".odb";
private static string createDirectoriesBasedOnKeyAndFilelocation(string filelocation,string key)
{
try
{
if (!Directory.Exists(filelocation))
Directory.CreateDirectory(filelocation);
}
catch (Exception)
{
}
var keyArray = Key.Splitkey(key);
string keypath = "";
foreach (var k in keyArray)
{
try
{
keypath += @"\" + k;
if (!Directory.Exists(filelocation + keypath))
Directory.CreateDirectory(filelocation + keypath);
}
catch (Exception)
{
}
}
return filelocation + keypath;
}
public static void WritetoStorage(string key, byte[] value, string filelocation)
{
//create folders
string dirstructure = createDirectoriesBasedOnKeyAndFilelocation(filelocation, key);
//Write the file to it's location
try
{
File.WriteAllBytes(dirstructure + @"\" + key + DB_EXTENTION, value);
//remove object from cache
SimpleObject tmpSimpleObject;
Cache.CacheDic.TryRemove(filelocation + key, out tmpSimpleObject);
}
catch (Exception)
{
}
Interlocked.Decrement(ref Cache.counter);
//Calculate pause based on amount of bytes
Thread.Sleep(value.Length / 100);
}
public static byte[] GetFromStorage(string key, string filelocation)
{
var keyArray = Key.Splitkey(key);
string dirstructure = "";
foreach (var k in keyArray)
dirstructure += @"\" + k;
byte[] bytes = File.ReadAllBytes(filelocation + @"\" + dirstructure + @"\" + key + DB_EXTENTION);
return bytes;
}
public static bool ExistingStorageCheck(string key, string filelocation)
{
var keyArray = Key.Splitkey(key);
string dirstructure = "";
foreach (var k in keyArray)
dirstructure += @"\" + k;
return File.Exists(filelocation + @"\" + dirstructure + @"\" + key + DB_EXTENTION);
}
public static string GetExactFileLocation(string key, string filelocation)
{
var keyArray = Key.Splitkey(key);
string dirstructure = "";
foreach (var k in keyArray)
dirstructure += @"\" + k;
return filelocation + @"\" + dirstructure + @"\" + key + DB_EXTENTION;
}
public static byte[] GetFromStorageWithKnownFileLocation(string filelocation)
{
byte[] bytes = File.ReadAllBytes(filelocation);
return bytes;
}
}
}