-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataLite.cs
121 lines (107 loc) · 3.83 KB
/
DataLite.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
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using EncryptedStorage.Data;
using EncryptedStorage.Data.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SQLite;
namespace EncryptedStorage.Service
{
public class DataLite : IDataBase
{
private readonly HttpContext context;
private readonly ILogger logger;
private SQLiteConnection Connection;
public EntityAccount Accounts { get; private set; }
public EntityFile Files { get; private set; }
public DataLite(IHttpContextAccessor context,
ILoggerFactory loggerFactory)
{
this.logger = loggerFactory.CreateLogger("RequestInfoLogger");
this.context = context.HttpContext;
Initializing(this.context.Session.GetString("StorageName"));
Accounts = new EntityAccount(Connection);
Files = new EntityFile(Connection);
}
public void Initializing(string storage)
{
if (storage == null || storage.Length < 1)
return;
try
{
string nameSpace = "EncryptedStorage.Data.Models";
string user = context.User?.Identity.Name;
var dir = "wwwroot/Users/" + user + "/Storages/";
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
dir += storage + ".db3";
Connection = new SQLiteConnection(dir);
Assembly assembly = typeof(AccountModel).Assembly;
Type[] tablesList = assembly.GetExportedTypes().Where(t => t.Namespace == nameSpace).ToArray();
foreach (Type type in tablesList)
{
Connection.CreateTable(type);
}
}
catch(Exception ex)
{
logger.LogInformation(ex.Message + " # " + ex.StackTrace);
}
}
public void Delete(string name)
{
string user = context.User.Identity?.Name;
if (user == null)
return;
string storage = "wwwroot/Users/" + user + "/Storages/" + name + ".db3";
string files = "wwwroot/Users/" + user + "/Files/" + name;
Close();
if (File.Exists(storage))
File.Delete(storage);
if (Directory.Exists(files))
Directory.Delete(files, true);
}
public void DeleteAll()
{
string user = context.User.Identity?.Name;
if (user == null)
return;
var storages = "wwwroot/Users/" + user + "/Storages";
var files = "wwwroot/Users/" + user + "/Files/";
Close();
if (Directory.Exists(storages))
Directory.Delete(storages, true);
if (Directory.Exists(files))
Directory.Delete(files, true);
}
public FileResult Download(string name)
{
string user = context.User.Identity?.Name;
if (user == null)
return null;
var path = "wwwroot/Users/" + user + "/Storages/" + name + ".db3";
Close();
using (FileStream file = new FileStream(path, FileMode.Open))
{
return new FileContentResult(new byte[file.ReadByte()], "application/octet")
{
FileDownloadName = "storage.db3"
};
}
}
public void Close()
{
if(Connection != null)
{
Connection.Close();
GC.Collect();
GC.WaitForPendingFinalizers();
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}