markus / License.DB / Database.cs @ master
이력 | 보기 | 이력해설 | 다운로드 (6.63 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.Runtime.Serialization; |
8 |
using System.Runtime.Serialization.Json; |
9 |
using System.Text; |
10 |
using System.Threading.Tasks; |
11 |
|
12 |
namespace License.DB |
13 |
{ |
14 |
public class DataBase : iDatabase |
15 |
{ |
16 |
const int initial = 10 * 8192; |
17 |
private readonly string ConnectionString; |
18 |
|
19 |
private readonly string DataBaseFoloder; |
20 |
private readonly string DataBaseFile; |
21 |
|
22 |
private readonly string LogConnectionString; |
23 |
private readonly string LogDataBaseFile; |
24 |
|
25 |
|
26 |
public DataBase() |
27 |
{ |
28 |
DataBaseFoloder = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataBase\\Active"); |
29 |
|
30 |
DataBaseFoloder = @"d:\temp\Active"; |
31 |
|
32 |
if (!System.IO.Directory.Exists(DataBaseFoloder)) |
33 |
{ |
34 |
System.IO.Directory.CreateDirectory(DataBaseFoloder); |
35 |
} |
36 |
|
37 |
DataBaseFile = System.IO.Path.Combine(DataBaseFoloder, $"{DateTime.Today.ToString("yyyyMMdd")}.db"); |
38 |
|
39 |
ConnectionString = $"Filename={DataBaseFile};Password=dof1073#;Connection=shared"; |
40 |
|
41 |
LogDataBaseFile = System.IO.Path.Combine(DataBaseFoloder, "Log.db"); |
42 |
|
43 |
LogConnectionString = $"Filename={LogDataBaseFile};Password=dof1073#;Connection=shared"; |
44 |
} |
45 |
|
46 |
public void Active(string MachineName, string Process, string UserName, string license) |
47 |
{ |
48 |
using (var db = new LiteDatabase(ConnectionString)) |
49 |
{ |
50 |
var collection = db.GetCollection<Active>("active"); |
51 |
|
52 |
var items = collection.Query().Where(x => x.Process == Process && x.MachineName == MachineName && x.UserName == UserName).FirstOrDefault(); |
53 |
|
54 |
System.Diagnostics.Debug.WriteLine("item : " + items?.UserName); |
55 |
|
56 |
System.Diagnostics.Debug.WriteLine(MachineName + " " + Process + " " + UserName); |
57 |
|
58 |
if (items == null) |
59 |
{ |
60 |
collection.Insert(new DB.Active(UserName, MachineName, Process, DateTime.Now, DateTime.MinValue)); |
61 |
} |
62 |
else |
63 |
{ |
64 |
items.UpdateTime = DateTime.Now; |
65 |
collection.Update(items); |
66 |
} |
67 |
} |
68 |
} |
69 |
|
70 |
public async Task<bool> UploadAsync(Uri uri,string SiteName) |
71 |
{ |
72 |
bool result = false; |
73 |
|
74 |
try |
75 |
{ |
76 |
var databasedir = new DirectoryInfo(DataBaseFoloder); |
77 |
|
78 |
if(databasedir.EnumerateFiles().Count() > 0) |
79 |
{ |
80 |
var items = databasedir.GetFiles().Where(x=>x.Name != $"{DateTime.Today.ToString("yyyyMMdd")}.db") |
81 |
.Select(x => new { key = int.Parse(x.Name.Replace(".db", "")), fileName = x.Name}) |
82 |
.OrderByDescending(x => x.key); |
83 |
|
84 |
foreach (var item in items) |
85 |
{ |
86 |
var fileSize = await FileSizeAsync(uri, SiteName, item.fileName); |
87 |
|
88 |
if(int.Parse(fileSize) < 0) |
89 |
{ |
90 |
var filepath = System.IO.Path.Combine(DataBaseFoloder, item.fileName); |
91 |
var uploadFilename = await UploadFileAsync(uri, SiteName, filepath); |
92 |
} |
93 |
else |
94 |
{ |
95 |
break; |
96 |
} |
97 |
} |
98 |
|
99 |
//using (var db = new LiteDatabase(LogConnectionString)) |
100 |
//{ |
101 |
// var collection = db.GetCollection<UploadItem>("Upload"); |
102 |
|
103 |
// var items = collection.Query().Where(x => x.FileName == filename).FirstOrDefault(); |
104 |
|
105 |
// if (items == null && File.Exists(filepath)) |
106 |
// { |
107 |
// var uploadFilename = await UploadFileAsync(uri,SiteName, filepath); |
108 |
// var size = await FileSizeAsync(uri, filename); |
109 |
|
110 |
// var localfileLength = new System.IO.FileInfo(filepath).Length; |
111 |
|
112 |
// if (long.Parse(size) == localfileLength) |
113 |
// { |
114 |
// collection.Insert(new UploadItem(filename, localfileLength)); |
115 |
// result = true; |
116 |
// } |
117 |
// } |
118 |
// else |
119 |
// { |
120 |
// result = true; |
121 |
// } |
122 |
//} |
123 |
} |
124 |
} |
125 |
catch (Exception) |
126 |
{ |
127 |
throw; |
128 |
} |
129 |
|
130 |
return result; |
131 |
} |
132 |
|
133 |
private async Task<string> UploadFileAsync(Uri _url,string SiteName, string filePath) |
134 |
{ |
135 |
try |
136 |
{ |
137 |
ServicePointManager.Expect100Continue = true; |
138 |
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
139 |
|
140 |
WebClient client = new WebClient(); |
141 |
client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache); |
142 |
client.Headers.Add("Cache-Control", "no-cache"); |
143 |
client.Encoding = Encoding.UTF8; |
144 |
|
145 |
var result = await client.UploadFileTaskAsync($"{_url}/Site={SiteName}", filePath); |
146 |
string reply = System.Text.Encoding.UTF8.GetString(result); |
147 |
|
148 |
return reply; |
149 |
} |
150 |
catch (Exception) |
151 |
{ |
152 |
throw; |
153 |
} |
154 |
} |
155 |
|
156 |
private async Task<string> FileSizeAsync(Uri _url, string SiteName, string filePath) |
157 |
{ |
158 |
try |
159 |
{ |
160 |
ServicePointManager.Expect100Continue = true; |
161 |
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; |
162 |
|
163 |
WebClient client = new WebClient(); |
164 |
client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache); |
165 |
client.Headers.Add("Cache-Control", "no-cache"); |
166 |
client.Encoding = Encoding.UTF8; |
167 |
|
168 |
var data = await client.DownloadStringTaskAsync($"{_url}/FileInfo/Site={SiteName}&file={filePath}"); |
169 |
return data; |
170 |
} |
171 |
catch (Exception) |
172 |
{ |
173 |
throw; |
174 |
} |
175 |
} |
176 |
|
177 |
private async Task<string> FileListAsync(Uri _url, string SiteName) |
178 |
{ |
179 |
try |
180 |
{ |
181 |
return ""; |
182 |
} |
183 |
catch (Exception) |
184 |
{ |
185 |
throw; |
186 |
} |
187 |
} |
188 |
} |
189 |
} |