markus / License.DB / Database.cs @ 1305c420
이력 | 보기 | 이력해설 | 다운로드 (5.16 KB)
1 |
using LiteDB; |
---|---|
2 |
using System; |
3 |
using System.Collections.Generic; |
4 |
using System.IO; |
5 |
using System.Linq; |
6 |
using System.Net; |
7 |
using System.Text; |
8 |
using System.Threading.Tasks; |
9 |
|
10 |
namespace License.DB |
11 |
{ |
12 |
public class DataBase : iDatabase |
13 |
{ |
14 |
const int initial = 10 * 8192; |
15 |
private readonly string ConnectionString; |
16 |
|
17 |
private readonly string DataBaseFoloder; |
18 |
private readonly string DataBaseFile; |
19 |
|
20 |
private readonly string LogConnectionString; |
21 |
private readonly string LogDataBaseFile; |
22 |
|
23 |
|
24 |
public DataBase() |
25 |
{ |
26 |
DataBaseFoloder = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataBase"); |
27 |
|
28 |
if (!System.IO.Directory.Exists(DataBaseFoloder)) |
29 |
{ |
30 |
System.IO.Directory.CreateDirectory(DataBaseFoloder); |
31 |
} |
32 |
|
33 |
DataBaseFile = System.IO.Path.Combine(DataBaseFoloder, $"{DateTime.Today.ToString("yyyyMMdd")}.db"); |
34 |
|
35 |
ConnectionString = $"Filename={DataBaseFile};Password=dof1073#;Connection=shared"; |
36 |
|
37 |
LogDataBaseFile = System.IO.Path.Combine(DataBaseFoloder, "Log.db"); |
38 |
|
39 |
LogConnectionString = $"Filename={LogDataBaseFile};Password=dof1073#;Connection=shared"; |
40 |
} |
41 |
|
42 |
public void Active(string MachineName, string Process, string UserName, string license) |
43 |
{ |
44 |
using (var db = new LiteDatabase(ConnectionString)) |
45 |
{ |
46 |
var collection = db.GetCollection<Active>("active"); |
47 |
|
48 |
var items = collection.Query().Where(x => x.Process == Process && x.MachineName == MachineName && x.UserName == UserName).FirstOrDefault(); |
49 |
|
50 |
System.Diagnostics.Debug.WriteLine("item : " + items?.UserName); |
51 |
|
52 |
System.Diagnostics.Debug.WriteLine(MachineName + " " + Process + " " + UserName); |
53 |
|
54 |
if (items == null) |
55 |
{ |
56 |
collection.Insert(new DB.Active(UserName, MachineName, Process, DateTime.Now, DateTime.MinValue)); |
57 |
} |
58 |
else |
59 |
{ |
60 |
items.UpdateTime = DateTime.Now; |
61 |
collection.Update(items); |
62 |
} |
63 |
} |
64 |
} |
65 |
|
66 |
public async Task<bool> UploadAsync(Uri uri) |
67 |
{ |
68 |
bool result = false; |
69 |
|
70 |
try |
71 |
{ |
72 |
var filename = $"{DateTime.Today.AddDays(-1).ToString("yyyyMMdd")}.db"; |
73 |
var filepath = System.IO.Path.Combine(DataBaseFoloder, filename); |
74 |
|
75 |
using (var db = new LiteDatabase(LogConnectionString)) |
76 |
{ |
77 |
var collection = db.GetCollection<UploadItem>("Upload"); |
78 |
|
79 |
var items = collection.Query().Where(x => x.FileName == filename).FirstOrDefault(); |
80 |
|
81 |
if (items == null && File.Exists(filepath)) |
82 |
{ |
83 |
var uploadFilename = await UploadFileAsync(uri, filepath); |
84 |
var size = await FileSizeAsync(uri, filename); |
85 |
|
86 |
var localfileLength = new System.IO.FileInfo(filepath).Length; |
87 |
|
88 |
if (long.Parse(size) == localfileLength) |
89 |
{ |
90 |
collection.Insert(new UploadItem(filename, localfileLength)); |
91 |
result = true; |
92 |
} |
93 |
} |
94 |
else |
95 |
{ |
96 |
result = true; |
97 |
} |
98 |
} |
99 |
} |
100 |
catch (Exception) |
101 |
{ |
102 |
throw; |
103 |
} |
104 |
|
105 |
return result; |
106 |
} |
107 |
|
108 |
private async Task<string> UploadFileAsync(Uri _url, string filePath) |
109 |
{ |
110 |
try |
111 |
{ |
112 |
ServicePointManager.Expect100Continue = true; |
113 |
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
114 |
|
115 |
WebClient client = new WebClient(); |
116 |
client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache); |
117 |
client.Headers.Add("Cache-Control", "no-cache"); |
118 |
client.Encoding = Encoding.UTF8; |
119 |
|
120 |
var result = await client.UploadFileTaskAsync(_url, filePath); |
121 |
string reply = System.Text.Encoding.UTF8.GetString(result); |
122 |
|
123 |
return reply; |
124 |
} |
125 |
catch (Exception) |
126 |
{ |
127 |
throw; |
128 |
} |
129 |
} |
130 |
|
131 |
private async Task<string> FileSizeAsync(Uri _url, string filePath) |
132 |
{ |
133 |
try |
134 |
{ |
135 |
ServicePointManager.Expect100Continue = true; |
136 |
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
137 |
|
138 |
WebClient client = new WebClient(); |
139 |
client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache); |
140 |
client.Headers.Add("Cache-Control", "no-cache"); |
141 |
client.Encoding = Encoding.UTF8; |
142 |
|
143 |
var data = await client.DownloadStringTaskAsync($"{_url}/FileInfo?filename={filePath}"); |
144 |
return data; |
145 |
} |
146 |
catch (Exception) |
147 |
{ |
148 |
throw; |
149 |
} |
150 |
} |
151 |
} |
152 |
} |