-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
FileDatabaseProvider.cs
291 lines (269 loc) · 8.85 KB
/
FileDatabaseProvider.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DWSIM.Interfaces;
using LiteDB;
namespace DWSIM.FileStorage
{
/// <summary>
/// File Database Provider for DWSIM, based on LiteDB.
/// </summary>
public class FileDatabaseProvider : IFileDatabaseProvider
{
private MemoryStream DBMem;
private LiteDatabase DB;
private static MemoryStream ReadStream(string path)
{
using (var temp = new MemoryStream(File.ReadAllBytes(path)))
{
var ms = new MemoryStream();
temp.CopyTo(ms);
return ms;
}
}
private bool IsDBLoaded = false;
/// <summary>
/// Checks if the database is loaded.
/// </summary>
public bool IsDatabaseLoaded { get { return IsDBLoaded; } }
/// <summary>
/// Checks if the given file exists in the database.
/// </summary>
/// <param name="filename">Filename to search.</param>
/// <returns>True if the file exists.</returns>
public bool CheckIfExists(string filename)
{
var file = DB.FileStorage.FindById(Path.GetFileName(filename));
return file != null;
}
/// <summary>
/// Exports the embedded database to a file.
/// </summary>
/// <param name="filepath">Filename to export the database to.</param>
public void ExportDatabase(string filepath)
{
DB.Checkpoint();
File.WriteAllBytes(filepath, DBMem.ToArray());
}
/// <summary>
/// Exports a file/item from the database to a file in the filesystem.
/// </summary>
/// <param name="filename">file item to export.</param>
/// <param name="exportpath">Path with filename to save the item to.</param>
public void ExportFile(string filename, string exportpath)
{
var file = DB.FileStorage.FindById(Path.GetFileName(filename));
if (file != null)
{
if (!File.Exists(exportpath)) file.SaveAs(exportpath);
}
else
{
throw new Exception("File not found");
}
}
/// <summary>
/// Returns a stored image file as a System.Drawing.Image object.
/// </summary>
/// <param name="filename">Image file name in the database.</param>
/// <returns>A System.Drawing.Image object.</returns>
public System.Drawing.Image GetFileAsImage(string filename)
{
var fname = Path.GetFileName(filename);
var file = DB.FileStorage.FindById(fname);
if (file != null)
{
using (var ms = new MemoryStream())
{
file.CopyTo(ms);
ms.Position = 0;
var image = System.Drawing.Image.FromStream(ms);
return image;
}
}
else
{
return null;
}
}
/// <summary>
/// Returns a stored image file as an Eto.Drawing.Bitmap object.
/// </summary>
/// <param name="filename">Image file name in the database.</param>
/// <returns>An Eto.Drawing.Bitmap object.</returns>
public Eto.Drawing.Bitmap GetFileAsEtoBitmap(string filename)
{
var fname = Path.GetFileName(filename);
var file = DB.FileStorage.FindById(fname);
if (file != null)
{
using (var ms = new MemoryStream())
{
file.CopyTo(ms);
ms.Position = 0;
var image = new Eto.Drawing.Bitmap(ms);
return image;
}
}
else
{
return null;
}
}
/// <summary>
/// Returns the file contents as plain text.
/// </summary>
/// <param name="filename">File/Item name in the database.</param>
/// <returns>The file contents as plain text.</returns>
public string GetFileAsText(string filename)
{
var file = DB.FileStorage.FindById(Path.GetFileName(filename));
if (file != null)
{
var tmpfile = DWSIM.SharedClasses.Utility.GetTempFileName();
file.SaveAs(tmpfile);
var text = File.ReadAllText(tmpfile);
File.Delete(tmpfile);
return text;
}
else
{
return null;
}
}
/// <summary>
/// Gets a list of all files in the database.
/// </summary>
/// <returns></returns>
public List<string> GetFiles()
{
var files = DB.FileStorage.FindAll();
return files.Select(f => f.Filename).ToList();
}
/// <summary>
/// Gets the contents of a file as a memory stream.
/// </summary>
/// <param name="filename">File/Item name</param>
/// <returns>A MemoryStream object containing the item.</returns>
public MemoryStream GetFileStream(string filename)
{
var file = DB.FileStorage.FindById(Path.GetFileName(filename));
if (file != null)
{
var ms = new MemoryStream();
file.CopyTo(ms);
ms.Position = 0;
return ms;
}
else
{
return null;
}
}
/// <summary>
/// For internal use only.
/// </summary>
/// <param name="dbpath"></param>
public void LoadDatabase(string dbpath)
{
ReleaseDatabase();
DBMem = ReadStream(dbpath);
DBMem.Position = 0;
DB = new LiteDatabase(DBMem);
IsDBLoaded = true;
}
/// <summary>
/// Stores a file in the database.
/// </summary>
/// <param name="filepath">Path of the file to import.</param>
public void PutFile(string filepath)
{
DB.FileStorage.Upload(Path.GetFileName(filepath), filepath);
DB.Checkpoint();
}
/// <summary>
/// Stores a file in the database.
/// </summary>
/// <param name="filepath">Path of the file to import.</param>
/// <param name="internalfilename">Internal name of the stored file.</param>
public void PutFile(string filepath, string internalfilename)
{
DB.FileStorage.Upload(internalfilename, filepath);
DB.Checkpoint();
}
/// <summary>
/// Stores a file in the database.
/// </summary>
/// <param name="stream"></param>
/// <param name="filename"></param>
public void PutFile(Stream stream, string filename)
{
DB.FileStorage.Upload(filename, filename, stream);
DB.Checkpoint();
}
/// <summary>
/// For internal use only.
/// </summary>
public void ReleaseDatabase()
{
if (DB != null) DB?.Dispose();
if (DBMem != null) DBMem?.Dispose();
IsDBLoaded = false;
}
/// <summary>
/// Removes a file from the database.
/// </summary>
/// <param name="filename">File/Item name.</param>
public void DeleteFile(string filename)
{
var file = DB.FileStorage.FindById(Path.GetFileName(filename));
if (file != null)
{
DB.FileStorage.Delete(file.Id);
DB.Rebuild();
DB.Checkpoint();
}
else
{
throw new Exception("File not found");
}
}
/// <summary>
/// For internal use only.
/// </summary>
public void CreateDatabase()
{
ReleaseDatabase();
DBMem = new MemoryStream();
DB = new LiteDatabase(DBMem);
IsDBLoaded = true;
}
/// <summary>
/// Gets the database size in kilobytes.
/// </summary>
/// <returns>DB size in KB.</returns>
public int GetSizeinKB()
{
return (int)DBMem.Length / 1024;
}
/// <summary>
///
/// </summary>
/// <param name="filename"></param>
/// <param name="stream"></param>
public void ExportFile(string filename, MemoryStream stream)
{
var file = DB.FileStorage.FindById(Path.GetFileName(filename));
if (file != null)
{
file.CopyTo(stream);
stream.Position = 0;
}
else
{
throw new Exception("File not found");
}
}
}
}